Generates validation code for different types including nested structs, maps, slices.
Returns errors as formatted []string: field_snake_case::is_required, field_snake_case::min_length_is
See examples for more info
go get -v github.com/serajam/check-gen
It is possible to validate:
int
,*int
int8
,*int8
int16
,*int16
int32
,*int32
int64
,*int64
[]int
,[]*int
[]int8
,[]*int8
[]int16
,[]*int16
[]int32
,[]*int32
[]int64
,[]*int64
float32
,*float32
float64
,*float64
[]float32
,[]*float32
[]float64
,[]*float64
string
,*string
*bool
[]byte
,[]*byte
struct
,*struct
[]struct
,[]*struct
map[string]type
custom types
- Add tags
check:""
for every field that's needs checks. For example:check:"required,min=1"
See available tags below - Run
check-gen
with path as last argument. Also, you can add--copyright=<string>
or-c <string>
for add a copyright to generated files - You should see
validation.go
with validation methods for every struct in every package that was specified fo/r validation generator UseValidate()
method to check your struct data
required
- checks if field is set to non default value (not 0, not nil, not "", len > 0 for slices)min
- checks field min valuemax
- checks field max valuelen
- checks field lencheck
- add call toValidate
method of nested structdeep
- checks every slice value callingValidate
method for structs and generates validation for values ofint, string
types specified by tags afterdeep
. For examplerequired,min=1,deep,len=100
will check if slice has minimum len of 1 and also will check if every element of that slice has len exactly 100. You can usecheck
withdeep
, for examplerequired,min=1,deep,check
will check if slice maximum len of 1 and add call toValidate
method for each of these structs.digit
will check if string contains only numberword
will check if string contains only letters, numbers, punctuation and whitespacesuuid
will check if string is uuidpassword
will check if string is password with len check. For example:min=8,password
phone
will check if string is satisfy phone format with len check. Checks only digits. Returns no errors with empty string, if not need this addrequired
tag. For example:phone=7~15
Please see examples
for full list of examples