Documentation ¶
Index ¶
- Constants
- Variables
- func ClearCustomValidators()
- func RegisterCustomValidator(key string, v Validator)
- func RemoveCustomValidator(key string)
- func SetTagName(tagName string)
- func Struct(sPtr interface{}, validators ...FieldValidator) error
- func TopParent() interface{}
- func Var(value interface{}, validators ...Validator) error
- type Action
- type FieldValidator
- type Validatable
- type ValidationError
- type ValidationErrors
- type Validator
- func Alpha() Validator
- func AlphaNumeric() Validator
- func AlphaNumericHyp() Validator
- func AlphaNumericHypUS() Validator
- func CIDR() Validator
- func CIDRv4() Validator
- func CIDRv6() Validator
- func Custom(key string) Validator
- func Fail() Validator
- func FileExists() Validator
- func IP() Validator
- func IPInRange(cidr string) Validator
- func IPv4() Validator
- func IPv6() Validator
- func Len(value int) Validator
- func Lowercase() Validator
- func MAC() Validator
- func Max(value int) Validator
- func MaxLen(value int) Validator
- func Min(value int) Validator
- func MinLen(value int) Validator
- func NotEmpty() Validator
- func Numeric() Validator
- func OmitEmpty() Validator
- func OneOf[T any](values ...T) Validator
- func RegexAll(regex ...string) Validator
- func RegexAny(regex ...string) Validator
- func Required() Validator
- func SemVer() Validator
- func Skip() Validator
- func Tags(tags string) Validator
- func URL() Validator
- func Unique() Validator
- func UniqueField(field string) Validator
- func Uppercase() Validator
- func VSemVer() Validator
Constants ¶
const ( TAG_NAME = "validate" TAG_OPTION_ID = "id" )
Variables ¶
var ( ErrorAppendIncompatibleError = fmt.Errorf("validation.ValidationErrors.Append: Attempt to subAppend incompatible error.") ErrorSubAppendIncompatibleError = fmt.Errorf("validation.ValidationErrors.SubAppend: Attempt to subAppend incompatible error.") )
var ( ErrorMissingStructPointer = fmt.Errorf("validation.Struct: Struct pointer is missing!") ErrorInvalidStructPointer = fmt.Errorf("validation.Struct: First argument must be a pointer to a struct!") ErrorStructFieldNotFound = fmt.Errorf("validation.Struct: Struct field not found int the struct!") ErrorFieldIsNotPointer = fmt.Errorf("validation.Field: First argument must be a pointer to a struct field!") )
var ErrorExportInterface = fmt.Errorf("validators.extra_UniqueField: Cannot export private field!")
var ErrorFieldNotFound = fmt.Errorf("validators.extra_UniqueField: Field not found!")
Functions ¶
func ClearCustomValidators ¶
func ClearCustomValidators()
RegisterCustomValidator registers custom validator with custom key.
func RegisterCustomValidator ¶
RegisterCustomValidator registers custom validator with custom key.
func RemoveCustomValidator ¶
func RemoveCustomValidator(key string)
RemoveCustomValidator removes custom validator from a list.
func SetTagName ¶
func SetTagName(tagName string)
SetTagName sets primary name tag from which validation options are extracted.
func Struct ¶
func Struct(sPtr interface{}, validators ...FieldValidator) error
Struct validates every given field against corresponding validators.
It panics if a provided value is not a pointer to a struct or if a field cannot be found within the structure.
Types ¶
type FieldValidator ¶
type FieldValidator struct {
// contains filtered or unexported fields
}
FieldValidator contains a pointer to a struct field and corresponding validators.
func Field ¶
func Field(fPtr interface{}, validators ...Validator) FieldValidator
Field returns new FieldValidator.
type Validatable ¶
type Validatable interface {
Validate() error
}
Structure is validatable if it contains Validate method that returns an error.
type ValidationError ¶
type ValidationError struct { Valid bool Namespace string Field string StructNamespace string StructField string Tag string ActualTag string Kind reflect.Kind Type reflect.Type Param string Value interface{} Err string RealErr string }
Custom validation error.
func (ValidationError) Error ¶
func (e ValidationError) Error() string
Error returns validation error as a string. It also populates the variables in the error message.
type ValidationErrors ¶
type ValidationErrors []ValidationError
func (ValidationErrors) Error ¶
func (es ValidationErrors) Error() string
Error returns all validation errors as a string.
type Validator ¶
Validator represents a validation rule.
None is an empty validator that does nothing (is skipped). It is useful for custom validators.
func Alpha ¶
func Alpha() Validator
Alpha checks whether the field contains only ASCII alpha characters.
func AlphaNumeric ¶
func AlphaNumeric() Validator
AlphaNumeric checks whether the field contains only alphanumeric characters. Validation fails for integers and floats.
func AlphaNumericHyp ¶
func AlphaNumericHyp() Validator
AlphaNumericDash checks whether the field contains only alphanumeric characters (a-Z0-9) and hyphen (-). Validation fails non string values.
func AlphaNumericHypUS ¶
func AlphaNumericHypUS() Validator
AlphaNumericDash checks whether the field contains only alphanumeric characters (a-Z0-9), hyphen (-) and underscore (_). Validation fails non string values.
func CIDRv4 ¶
func CIDRv4() Validator
CIDRv4 checks whether the field value is a valid v4 CIDR address.
func CIDRv6 ¶
func CIDRv6() Validator
CIDRv6 checks whether the field value is a valid v6 CIDR address.
func FileExists ¶
func FileExists() Validator
File checks whether the field is a valid file path and whether file exists.
func Lowercase ¶
func Lowercase() Validator
Lowercase checks whether the field contains only lowercase characters.
func Max ¶
Max checks whether the field value is less than or equal to the specified value. In case of strings, slices, arrays and maps the length is checked.
func Min ¶
Min checks whether the field value is greater than or equal to the specified value. In case of strings, slices, arrays and maps the length is checked.
func MinLen ¶
MinLen checks whether the field length is greater than or equal to the specified value.
func NotEmpty ¶
func NotEmpty() Validator
NotEmpty validator checks whether value is not blank or with zero length.
func Numeric ¶
func Numeric() Validator
Numeric checks whether the field contains only numeric characters. Validation fails for integers and floats.
func OmitEmpty ¶
func OmitEmpty() Validator
OmitEmpty prevents further validation of the field, if the field is empty.
func OneOf ¶
OneOf checks whether the field value equals one of the specified values. If no value is provided, the validation always fails.
func Tags ¶
Tags returns a new validator with the given tags. It is a generic validator that allows use of any validation rule from 'github.com/go-playground/validator' library.
func Unique ¶
func Unique() Validator
Unique validator checks whether all elements within array, slice or map are unique.
func UniqueField ¶
Unique validator checks whether the given field is unique for all elements within a slice of struct.
func Uppercase ¶
func Uppercase() Validator
Uppercase checks whether the field contains only uppercase characters.
func VSemVer ¶
func VSemVer() Validator
VSemVer checks whether the field is a valid semantic version and is prefixed with 'v'.
func (Validator) Error ¶
Error overwrites the validator's default error message with the user-defined error and returns the modified validator.
func (Validator) Errorf ¶
Errorf overwrites the validator's default error message with the formatted user-defined error and returns the modified validator.
func (Validator) ToError ¶
func (v Validator) ToError() ValidationErrors