-
Notifications
You must be signed in to change notification settings - Fork 1
Validators
Name | Tag | Description |
---|---|---|
Minimum | min | The lowest boundary of a type. I.e. the lowest a number or the length of a string can be. |
Maximum | max | The highest boundary of a type. I.e. the highest a number or the length of a string can be. |
Not empty | not_empty | Assert that a type is not empty. I.e. that a number or the length of a string is not 0, or that the value of a pointer is not nil . |
Empty | empty | Assert that a type is empty. I.e. that a number or the length of a string is 0, or that the value of a pointer is nil . |
The lowest boundary of a type. I.e. the lowest a number or the length of a string can be.
Value int `validate:"min(5)"` // Value cannot be less than 5.
Value string `validate:"min(5)"` // Value cannot contain less than 5 characters.
Value []string `validate:"min(5)"` // Value cannot contain less than 5 items.
Value map[string]string `validate:"min(5)"` // Value cannot contain less than 5 keys.
Strings, integers, floats, maps, arrays and slices.
The highest boundary of a type. I.e. the highest a number or the length of a string can be.
Value int `validate:"max(5)"` // Value cannot be greater than 5.
Value string `validate:"max(5)"` // Value cannot be longer than 5 characters.
Value []string `validate:"max(5)"` // Value cannot contain more than 5 items.
Value map[string]string `validate:"max(5)"` // Value cannot contain more than 5 keys.
Assert that a type is not empty. I.e. that a number or the length of a string is not 0, or that the value of a pointer is not nil
.
Value int `validate:"not_empty"` // Value cannot be empty.
Value *string `validate:"not_empty"` // Value cannot be empty.
Value []string `validate:"not_empty"` // Value cannot be empty.
Value map[string]string `validate:"not_empty"` // Value cannot be empty.
Strings, integers, floats, maps, arrays and slices.
Assert that a type is empty. I.e. that a number or the length of a string is 0, or that the value of a pointer is nil
.
Value *string `validate:"empty|numeric"` //
Strings, integers, floats, maps, arrays and slices.
- nil
- empty
- equal
- contain
- lower_case
- upper_case
- numeric
- time
- regexp
- func
Register a global validator by calling validator.Register(name string, validator ValidatorFn)
.
validator.Register("validator_name", func (context *validator.Context, options []string) error {
// ...
})
Use the func
validator tag to assign a custom validator method on your structure.