Documentation ¶
Index ¶
- func AddFilter(name string, v FilterFunction)
- func AddValidator(name string, v ValidationFunction)
- func CopyOptions(opts *ValidationOptions)
- func IsAfterToday(ctx *ValidationContext) bool
- func IsAlphaNumeric(ctx *ValidationContext) bool
- func IsBeforeToday(ctx *ValidationContext) bool
- func IsEmail(ctx *ValidationContext) bool
- func IsEnum(ctx *ValidationContext) bool
- func IsMax(ctx *ValidationContext) bool
- func IsMin(ctx *ValidationContext) bool
- func IsNotToday(ctx *ValidationContext) bool
- func IsOrAfterToday(ctx *ValidationContext) bool
- func IsOrBeforeToday(ctx *ValidationContext) bool
- func IsRequired(ctx *ValidationContext) bool
- func IsToday(ctx *ValidationContext) bool
- func IsUuid1(ctx *ValidationContext) bool
- func IsUuid2(ctx *ValidationContext) bool
- func IsUuid3(ctx *ValidationContext) bool
- func IsUuid4(ctx *ValidationContext) bool
- func NullIfEmpty(ctx *ValidationContext) reflect.Value
- func SetupOptions(configCallback func(*ValidationOptions))
- func Trim(ctx *ValidationContext) reflect.Value
- type Comparator
- type ComparatorDescription
- type FieldError
- type FilterFunction
- type Stack
- type ValidationContext
- func (vc ValidationContext) ArgCount() int
- func (vc ValidationContext) GetValue() reflect.Value
- func (vc ValidationContext) IsValueOfKind(kind ...reflect.Kind) bool
- func (vc *ValidationContext) IsValueOfType(i interface{}) bool
- func (vc *ValidationContext) MustGetIntArg(position int) int64
- func (vc *ValidationContext) MustGetUintArg(position int) uint64
- func (vc *ValidationContext) ValueMustBeOfKind(kind ...reflect.Kind)
- type ValidationError
- type ValidationFlag
- type ValidationFunction
- type ValidationOptions
- type ValidationResult
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddFilter ¶
func AddFilter(name string, v FilterFunction)
AddFilter adds the given filter function to the list of filters
The backed storage containing the list of filters is not thread safe and so this function must be called once during package or application initialization.
You cannot replace filter functions that have already been added to the list, so the function will panic if the name already exists.
func AddValidator ¶
func AddValidator(name string, v ValidationFunction)
AddValidator adds the given validator function to the list of validators
The backed storage containing the list of validators is not thread safe and so this function must be called once during package or application initialization.
You cannot replace validator functions that have already been added to the list, so the function will panic if the name already exists.
func CopyOptions ¶
func CopyOptions(opts *ValidationOptions)
CopyOptions CopyOptions Copies the default global options into the specified destination. Useful when you want to have localized validation options
func IsAfterToday ¶ added in v0.0.2
func IsAfterToday(ctx *ValidationContext) bool
IsAfterToday tests whether the given date is after today.
If the time layout is not specified, '2006-01-02' will be used
func IsAlphaNumeric ¶
func IsAlphaNumeric(ctx *ValidationContext) bool
IsAlphaNumeric verifies that the given string is alphanumeric
func IsBeforeToday ¶ added in v0.0.2
func IsBeforeToday(ctx *ValidationContext) bool
IsBeforeToday tests whether the given date is before today.
If the time layout is not specified, '2006-01-02' will be used
func IsEmail ¶
func IsEmail(ctx *ValidationContext) bool
IsEmail tests if the input value matches an email format.
The validation rules used here do not conform to RFC and only allow only a few latin character set values. Therefore this function could be considered as very strict.
func IsEnum ¶
func IsEnum(ctx *ValidationContext) bool
IsEnum tests if the input value matches any of the values passed in the arguments
func IsMax ¶
func IsMax(ctx *ValidationContext) bool
IsMax tests if the given input (string, integer, list) contains at least the given number of elements
func IsMin ¶
func IsMin(ctx *ValidationContext) bool
IsMin tests if the given input (string, integer, list) contains at least the given number of elements
func IsNotToday ¶ added in v0.0.2
func IsNotToday(ctx *ValidationContext) bool
IsNotToday tests whether the given date is not today.
If the time layout is not specified, '2006-01-02' will be used
func IsOrAfterToday ¶ added in v0.0.2
func IsOrAfterToday(ctx *ValidationContext) bool
IsOrAfterToday tests whether the given date is today or after today.
If the time layout is not specified, '2006-01-02' will be used
func IsOrBeforeToday ¶ added in v0.0.2
func IsOrBeforeToday(ctx *ValidationContext) bool
IsBeforeToday tests whether the given date is today or before today.
If the time layout is not specified, '2006-01-02' will be used
func IsRequired ¶
func IsRequired(ctx *ValidationContext) bool
IsRequired check if the required field has values.
For literal values, the function always returns true because the values are present and can subsequnetly be validated appropriately.
For pointer types, the function will return false if the pointer is null or true if the pointer is not null
func IsToday ¶ added in v0.0.2
func IsToday(ctx *ValidationContext) bool
IsToday tests whether the given date is today.
If the time layout is not specified, '2006-01-02' will be used
func IsUuid1 ¶
func IsUuid1(ctx *ValidationContext) bool
func IsUuid2 ¶
func IsUuid2(ctx *ValidationContext) bool
func IsUuid3 ¶
func IsUuid3(ctx *ValidationContext) bool
func IsUuid4 ¶
func IsUuid4(ctx *ValidationContext) bool
func NullIfEmpty ¶ added in v0.0.4
func NullIfEmpty(ctx *ValidationContext) reflect.Value
NullIfEmpty Sets the given string pointer's value to null if the string is empty
func SetupOptions ¶
func SetupOptions(configCallback func(*ValidationOptions))
SetupOptions SetupOptions allows you to configure the global validation options.
func Trim ¶
func Trim(ctx *ValidationContext) reflect.Value
Types ¶
type Comparator ¶ added in v0.0.2
type Comparator string
const ( EQUALS Comparator = "=" NOT_EQUAL Comparator = "!=" LESS_THAN Comparator = "<" GREATER_THAN Comparator = ">" LESS_THAN_OR_EQUAL Comparator = "<=" GREATER_THAN_OR_EQUAL Comparator = ">=" )
func (Comparator) NumericDescription ¶ added in v0.0.2
func (c Comparator) NumericDescription() string
func (Comparator) TemporalDescription ¶ added in v0.0.2
func (c Comparator) TemporalDescription() string
type ComparatorDescription ¶ added in v0.0.2
type ComparatorDescription byte
const ( NUMERICAL ComparatorDescription = 0 TEMPORAL ComparatorDescription = 1 )
type FieldError ¶
func (FieldError) Error ¶
func (e FieldError) Error() string
type FilterFunction ¶
type FilterFunction func(ctx *ValidationContext) reflect.Value
FilterFunction FilterFunction is used to manipulate input values. This function may manipulate the value in place or return a completely new value.
However, the contract is that they must always return a value depending on the input value and logic contained therein.
type ValidationContext ¶
type ValidationContext struct { // The resolved type of the input value ValueType reflect.Type // If the input value is a pointer IsPointer bool // If the input value is a pointer and the point is null IsNull bool // Validation options Options *ValidationOptions // Arguments passed to the validation or filter function Args []string // Containst the validation error message ErrorMessage string // An error that may have occurred during validation AdditionalError error // contains filtered or unexported fields }
func (ValidationContext) ArgCount ¶
func (vc ValidationContext) ArgCount() int
func (ValidationContext) GetValue ¶
func (vc ValidationContext) GetValue() reflect.Value
GetValue GetValue Returns the underlying value, resolving pointers if necessary
func (ValidationContext) IsValueOfKind ¶
func (vc ValidationContext) IsValueOfKind(kind ...reflect.Kind) bool
func (*ValidationContext) IsValueOfType ¶ added in v0.0.2
func (vc *ValidationContext) IsValueOfType(i interface{}) bool
func (*ValidationContext) MustGetIntArg ¶
func (vc *ValidationContext) MustGetIntArg(position int) int64
func (*ValidationContext) MustGetUintArg ¶
func (vc *ValidationContext) MustGetUintArg(position int) uint64
func (*ValidationContext) ValueMustBeOfKind ¶
func (vc *ValidationContext) ValueMustBeOfKind(kind ...reflect.Kind)
ValueMustBeOfKind ValueMustBeOfKind tests if the resolved kind of the input value matches any of the given kinds.
If there is no match, the function panics.
type ValidationError ¶
func (ValidationError) Error ¶
func (e ValidationError) Error() string
type ValidationFlag ¶ added in v0.0.4
type ValidationFlag string
Flags used by the validation engine before calling validations and filters.
Flags will alter behavior of the validator towards each field being evaluated.
const ( // If a value contains zero value, allow the value to pass through by skipping // validation since there's nothing to validate or filter. AllowZero ValidationFlag = "allow_zero" )
type ValidationFunction ¶
type ValidationFunction func(ctx *ValidationContext) bool
ValidationFunction ValidationFunction is used to validate input. Validator functions return a boolean indicating whether the input is valid or not.
type ValidationOptions ¶
type ValidationOptions struct { // FilterTagName specifies the tag to use when looking up filter functions // // default: 'filter' FilterTagName string // TriggerTagName specifies the tag to use when looking up activation triggers. // // Activation triggers allow evaluating fields selectively. The default activation trigger is 'all'. // // The following example shows a struct that will have the '.Age' field evaluated everytime the struct is validate // and '.Id' only when updating. // // type UserRequest struct { // Id int `validator:"min(100)" trigger:"update"` // Age int `validator:"min(10)" trigger:"all"` // } // // default: 'trigger' TriggerTagName string // ValidatorTagName specifies the tag to use when looking up validation functions // // default: 'validator' ValidatorTagName string // MessageTagName specifies the tag to use when looking up error message template // // default: 'message' MessageTagName string // LabelTagName specifies the tag to use when looking up the fields label // // default: 'label' LabelTagName string // StringAutoTrim specifies whether to automatically trim all strings // // default: false StringAutoTrim bool // StopOnFirstError specifies whether to stop validation upon encountering the first validation error // // default: false StopOnFirstError bool // ExposeValidatorNames specifies whether to expose validator function names in default error messages // when neither a validator nor a struct tag has specified an error message. // // Exposing validator names can provide technically meaningful error messages but may not be suitable for // client side presentation. // // default: false ExposeValidatorNames bool // NoPanicOnFunctionConflict specifies whether or not to panic upon encountering an existing validation or filter function // when adding custom validators and filters. // // default: false NoPanicOnFunctionConflict bool // ExposeEnumValues specifies whether to list all enum values in the default error message. // // default: false ExposeEnumValues bool // FlagTagName specifies the name of tag to use when looking up flags // // default: 'flags' FlagTagName string }
type ValidationResult ¶
type ValidationResult struct { // Error the top level error summarizing what the hell happened. May not necessarily come from validating the passed struct // Error *ValidationError // // FieldErrors FieldErrors struct field validation errors FieldErrors []FieldError // contains filtered or unexported fields }
ValidationResult contains validation status, a general error, and field errors
func Validate ¶
func Validate(structPtr interface{}, trigger ...string) (res *ValidationResult)
Validate validates the given struct
Parameters ¶
structPtr : Pointer to a struct
trigger : Activation trigger - Specifies a unique value that will trigger activation of fields that have been taggeed with the same value.
func (ValidationResult) IsValid ¶
func (r ValidationResult) IsValid() bool