README
¶
struct-validator
Verify the values of struct fields using tags
Example code
type Test1 struct {
FirstName string `validation:"req lenmin:5 lenmax:25"`
LastName string `validation:"req lenmin:2 lenmax:50"`
Age int `validation:"req valmin:18 valmax:150"`
Price int `validation:"req valmin:0 valmax:9999"`
PostCode string `validation:"req" validation_regexp:"^[0-9][0-9]-[0-9][0-9][0-9]$"`
Email string `validation:"req email"`
BelowZero int `validation:"valmin:-6 valmax:-2"`
DiscountPrice int `validation:"valmin:0 valmax:8000"`
Country string `validation_regexp:"^[A-Z][A-Z]$"`
County string `validation:"lenmax:40"`
}
s := &Test1{
FirstName: "Name that is way too long and certainly not valid",
...
}
o := structvalidator.&ValidationOptions{
RestrictFields: map[string]bool{
"FirstName": true,
"LastName": true,
...
},
...
}
isValid, fieldsWithInvalidValue := structvalidator.Validate(s, &o)
Documentation
¶
Index ¶
Constants ¶
const Email = 16
const FailEmail = 128
const FailEmpty = 32
const FailLenMax = 4
const FailLenMin = 2
values for invalid field flags
const FailRegexp = 64
const FailValMax = 16
const FailValMin = 8
const FailZero = 256
const Required = 8
const TypeEmail = 4
const TypePassword = 3
const TypeText = 1
const TypeTextarea = 2
const VERSION = "0.4.7"
const ValMaxNotNil = 4
const ValMinNotNil = 2
values used with flags
Variables ¶
This section is empty.
Functions ¶
func GenerateHTML ¶
func GenerateHTML(obj interface{}, options *HTMLOptions) map[string]string
GenerateHTMLInput takes a struct and generates HTML inputs for each of the fields, eg. <input> or <textarea>
func Validate ¶
func Validate(obj interface{}, options *ValidationOptions) (bool, map[string]int)
Validate validates fields of a struct. Currently only fields which are string or int (any) are validated. Func returns boolean value that determines whether value is true or false, and a map of fields that failed validation. See Fail* constants for the values.
Types ¶
type FieldValidation ¶
type FieldValidation struct {
// contains filtered or unexported fields
}
type HTMLOptions ¶
type HTMLOptions struct { RestrictFields map[string]bool ExcludeFields map[string]bool OverwriteFieldTags map[string]map[string]string OverwriteTagName string ValidateWhenSuffix bool IDPrefix string NamePrefix string OverwriteValues map[string]string FieldValues bool }
Optional configuration for validation: * RestrictFields defines what struct fields should be generated * ExcludeFields defines fields that should be skipped (also from RestrictFields) * OverwriteFieldTags can be used to overwrite tags for specific fields * OverwriteTagName sets tag used to define validation (default is "validation") * ValidateWhenSuffix will validate certain fields based on their name, eg. "PrimaryEmail" field will need to be a valid email * OverwriteFieldValues is to use overwrite values for fields, so these values are validated not the ones in struct * IDPrefix - if added, an element will contain an 'id' attribute in form of prefix + field name * NamePrefix - use this to put a prefix in the 'name' attribute * OverwriteValues - fill inputs with the specified values * FieldValues - when true then fill inputs with struct instance values
type ValidationOptions ¶
type ValidationOptions struct { RestrictFields map[string]bool OverwriteFieldTags map[string]map[string]string OverwriteTagName string ValidateWhenSuffix bool OverwriteFieldValues map[string]interface{} }
Optional configuration for validation: * RestrictFields defines what struct fields should be validated * OverwriteFieldTags can be used to overwrite tags for specific fields * OverwriteTagName sets tag used to define validation (default is "validation") * ValidateWhenSuffix will validate certain fields based on their name, eg. "PrimaryEmail" field will need to be a valid email * OverwriteFieldValues is to use overwrite values for fields, so these values are validated not the ones in struct