Skip to content
Robin edited this page May 16, 2015 · 9 revisions
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.

Minimum (min)

The lowest boundary of a type. I.e. the lowest a number or the length of a string can be.

Examples

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.

Supports

Strings, integers, floats, maps, arrays and slices.

Maximum (max)

The highest boundary of a type. I.e. the highest a number or the length of a string can be.

Examples

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.

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.

Examples

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.

Supports

Strings, integers, floats, maps, arrays and slices.

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.

Examples

Value *string               `validate:"empty|numeric"` // 

Supports

Strings, integers, floats, maps, arrays and slices.

Not documented yet...

  • nil
  • empty
  • equal
  • contain
  • lower_case
  • upper_case
  • numeric
  • time
  • regexp
  • func

Custom Validators

Global

Register a global validator by calling validator.Register(name string, validator ValidatorFn).

validator.Register("validator_name", func (context *validator.Context, options []string) error {
    // ...
})

On structure

Use the func validator tag to assign a custom validator method on your structure.