validate

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 12, 2022 License: MIT Imports: 7 Imported by: 0

README

Validate functions generator

Basic model required, which can be generated with one of the model generators

Use validation sub-command to execute generator:

bungen validation -h

First create your database and tables in it

create table "projects"
(
    "projectId" serial not null,
    "name"      text   not null,

    primary key ("projectId")
);

create table "users"
(
    "userId"    serial      not null,
    "email"     varchar(64) not null,
    "activated" bool        not null default false,
    "name"      varchar(128),
    "countryId" integer,

    primary key ("userId")
);

create schema "geo";
create table geo."countries"
(
    "countryId" serial     not null,
    "code"      varchar(3) not null,
    "coords"    integer[],

    primary key ("countryId")
);

alter table "users"
    add constraint "fk_user_country"
        foreign key ("countryId")
            references geo."countries" ("countryId") on update restrict on delete restrict;

Run generator

bungen validation -c postgres://user:password@localhost:5432/yourdb -o ~/output/model.go -t public.* -f

You should get following functions on model package:

//lint:file-ignore U1000 ignore unused code, it's generated
package model

import (
    "unicode/utf8"
)

const (
	ErrEmptyValue = "empty"
	ErrMaxLength  = "len"
	ErrWrongValue = "value"
)


func (m User) Validate() (errors map[string]string, valid bool) {
	errors = map[string]string{}
	
	if utf8.RuneCountInString(m.Email) > 64 {
		errors[Columns.User.Email] = ErrMaxLength
	}
	
	if m.Name != nil && utf8.RuneCountInString(*m.Name) > 128 {
		errors[Columns.User.Name] = ErrMaxLength
	}
	
	if m.CountryID != nil && *m.CountryID == 0 {
		errors[Columns.User.CountryID] = ErrEmptyValue
	}

	return errors, len(errors) == 0
}

func (m GeoCountry) Validate() (errors map[string]string, valid bool) {
	errors = map[string]string{}

	if utf8.RuneCountInString(m.Code) > 3 {
		errors[Columns.GeoCountry.Code] = ErrMaxLength
	}

	return errors, len(errors) == 0
}


Try it

package model

import (
	"fmt"
	"testing"
)

func TestModel(t *testing.T) {
    code := "should fail on length"
    country := GeoCountry{
    	Code: code,
    }
    errors, valid := country.Validate()

	fmt.Printf("%#v\n", errors)
	fmt.Printf("%#v\n", valid)
}

Documentation

Index

Constants

View Source
const (
	// Nil is nil check types
	Nil = "nil"
	// Zero is 0 check types
	Zero = "zero"
	// PZero is 0 check types for pointers
	PZero = "pzero"
	// Len is length check types
	Len = "len"
	// PLen is length check types for pointers
	PLen = "plen"
	// Enum is allowed values check types
	Enum = "enum"
	// PEnum is allowed values check types for pointers
	PEnum = "penum"
)
View Source
const Template = `` /* 1527-byte string literal not displayed */

Variables

This section is empty.

Functions

func CreateCommand

func CreateCommand() *cobra.Command

CreateCommand creates generator command

Types

type Options

type Options struct {
	base.Options

	// Package sets package name for model
	// Works only with SchemaPackage = false
	Package string

	// Do not replace primary key name to ID
	KeepPK bool
}

Options for generator

func (*Options) Def

func (o *Options) Def()

Def fills default values of an options

type TemplateColumn

type TemplateColumn struct {
	model.Column

	Check string
	Enum  template.HTML

	Import string
}

TemplateColumn stores column info

func NewTemplateColumn

func NewTemplateColumn(column model.Column, options Options) TemplateColumn

NewTemplateColumn creates a column for template

type TemplateEntity

type TemplateEntity struct {
	model.Entity

	Columns []TemplateColumn
	Imports []string
}

TemplateEntity stores struct info

func NewTemplateEntity

func NewTemplateEntity(entity model.Entity, options Options) TemplateEntity

NewTemplateEntity creates an entity for template

type TemplatePackage

type TemplatePackage struct {
	Package string

	HasImports bool
	Imports    []string

	Entities []TemplateEntity
}

TemplatePackage stores package info

func NewTemplatePackage

func NewTemplatePackage(entities []model.Entity, options Options) TemplatePackage

NewTemplatePackage creates a package for template

type Validate

type Validate struct {
	// contains filtered or unexported fields
}

Validate represents validate generator

func New

func New() *Validate

New creates generator

func (*Validate) AddFlags

func (g *Validate) AddFlags(command *cobra.Command)

AddFlags adds flags to command

func (*Validate) Generate

func (g *Validate) Generate() error

Generate runs whole generation process

func (*Validate) Options

func (g *Validate) Options() *Options

Options gets options

func (*Validate) Packer

func (g *Validate) Packer() base.Packer

Packer returns packer function for compile entities into package

func (*Validate) ReadFlags

func (g *Validate) ReadFlags(command *cobra.Command) error

ReadFlags read flags from command

func (*Validate) SetOptions

func (g *Validate) SetOptions(options Options)

SetOptions sets options

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL