Documentation ¶
Overview ¶
Package validation provides configurable and extensible rules for validating data of various types.
Index ¶
- Variables
- func EnsureString(value interface{}) (string, int)
- func Indirect(value interface{}) (interface{}, bool)
- func IsEmpty(value interface{}) bool
- func LengthOfValue(value interface{}) (int, int)
- func MultipleOf(threshold interface{}) *multipleOfRule
- func StringOrBytes(value interface{}) (isString bool, str string, isBytes bool, bs []byte)
- func ToFloat(value interface{}) (float64, error)
- func ToInt(value interface{}) (int64, error)
- func ToUint(value interface{}) (uint64, error)
- func Validate(value interface{}, rules ...Rule) goerr.IError
- func ValidateStruct(structPtr interface{}, fields ...*FieldRules) goerr.IError
- type DateRule
- type ErrFieldNotFound
- type ErrFieldPointer
- type FieldRules
- type InRule
- type LengthRule
- type MatchRule
- type NotInRule
- type Rule
- type RuleFunc
- type StringRule
- type ThresholdRule
- type Validatable
Constants ¶
This section is empty.
Variables ¶
var ( // ErrorTag is the struct tag name used to customize the error field name for a struct field. ErrorTag = "json" // Skip is a special validation rule that indicates all rules following it should be skipped. Skip = &skipRule{} )
var ( // ErrStructPointer is the error that a struct being validated is not specified as a pointer. ErrStructPointer = verror.NewGoErr(1001) )
var NilOrNotEmpty = &requiredRule{skipNil: true, code: 1202}
NilOrNotEmpty checks if a value is a nil pointer or a value that is not empty. NilOrNotEmpty differs from Required in that it treats a nil pointer as valid.
var NotNil = ¬NilRule{code: 1201}
NotNil is a validation rule that checks if a value is not nil. NotNil only handles types including interface, pointer, slice, and map. All other types are considered valid.
var Required = &requiredRule{skipNil: false, code: 1202}
Required is a validation rule that checks if a value is not empty. A value is considered not empty if - integer, float: not zero - bool: true - string, array, slice, map: len() > 0 - interface, pointer: not nil and the referenced value is not empty - any other types
Functions ¶
func EnsureString ¶
EnsureString ensures the given value is a string. If the value is a byte slice, it will be typecast into a string. An error is returned otherwise.
func Indirect ¶
func Indirect(value interface{}) (interface{}, bool)
Indirect returns the value that the given interface or pointer references to. If the value implements driver.Valuer, it will deal with the value returned by the Value() method instead. A boolean value is also returned to indicate if the value is nil or not (only applicable to interface, pointer, map, and slice). If the value is neither an interface nor a pointer, it will be returned back.
func IsEmpty ¶
func IsEmpty(value interface{}) bool
IsEmpty checks if a value is empty or not. A value is considered empty if - integer, float: zero - bool: false - string, array: len() == 0 - slice, map: nil or len() == 0 - interface, pointer: nil or the referenced value is empty
func LengthOfValue ¶
LengthOfValue returns the length of a value that is a string, slice, map, or array. An error is returned for all other types.
func MultipleOf ¶
func MultipleOf(threshold interface{}) *multipleOfRule
func StringOrBytes ¶
StringOrBytes typecasts a value into a string or byte slice. Boolean flags are returned to indicate if the typecasting succeeds or not.
func ToFloat ¶
ToFloat converts the given value to a float64. An error is returned for all incompatible types.
func ToInt ¶
ToInt converts the given value to an int64. An error is returned for all incompatible types.
func ToUint ¶
ToUint converts the given value to an uint64. An error is returned for all incompatible types.
func Validate ¶
func Validate(value interface{}, rules ...Rule) goerr.IError
Validate validates the given value and returns the validation error, if any.
Validate performs validation using the following steps: - validate the value against the rules passed in as parameters - if the value is a map and the map values implement `Validatable`, call `Validate` of every map value - if the value is a slice or array whose values implement `Validatable`, call `Validate` of every element
func ValidateStruct ¶
func ValidateStruct(structPtr interface{}, fields ...*FieldRules) goerr.IError
Types ¶
type DateRule ¶
type DateRule struct {
// contains filtered or unexported fields
}
func Date ¶
Date returns a validation rule that checks if a string value is in a format that can be parsed into a date. The format of the date should be specified as the layout parameter which accepts the same value as that for time.Parse. For example,
validation.Date(time.ANSIC) validation.Date("02 Jan 06 15:04 MST") validation.Date("2006-01-02")
By calling Min() and/or Max(), you can let the Date rule to check if a parsed date value is within the specified date range.
An empty value is considered valid. Use the Required rule to make sure a value is not empty.
func (*DateRule) Max ¶
Max sets the maximum date range. A zero value means skipping the maximum range validation.
type ErrFieldNotFound ¶
type ErrFieldNotFound int
ErrFieldNotFound is the error that a field cannot be found in the struct.
func (ErrFieldNotFound) GetCode ¶ added in v1.0.3
func (e ErrFieldNotFound) GetCode() int
type ErrFieldPointer ¶
type ErrFieldPointer int
ErrFieldPointer is the error that a field is not specified as a pointer.
func (ErrFieldPointer) GetCode ¶ added in v1.0.3
func (e ErrFieldPointer) GetCode() int
type FieldRules ¶
type FieldRules struct {
// contains filtered or unexported fields
}
FieldRules represents a rule set associated with a struct field.
func Field ¶
func Field(fieldPtr interface{}, rules ...Rule) *FieldRules
Field specifies a struct field and the corresponding validation rules. The struct field must be specified as a pointer to it.
type InRule ¶
type InRule struct {
// contains filtered or unexported fields
}
func In ¶
func In(values ...interface{}) *InRule
In returns a validation rule that checks if a value can be found in the given list of values. Note that the value being checked and the possible range of values must be of the same type. An empty value is considered valid. Use the Required rule to make sure a value is not empty.
type LengthRule ¶
type LengthRule struct {
// contains filtered or unexported fields
}
func Length ¶
func Length(min, max int) *LengthRule
Length returns a validation rule that checks if a value's length is within the specified range. If max is 0, it means there is no upper bound for the length. This rule should only be used for validating strings, slices, maps, and arrays. An empty value is considered valid. Use the Required rule to make sure a value is not empty.
func RuneLength ¶
func RuneLength(min, max int) *LengthRule
RuneLength returns a validation rule that checks if a string's rune length is within the specified range. If max is 0, it means there is no upper bound for the length. This rule should only be used for validating strings, slices, maps, and arrays. An empty value is considered valid. Use the Required rule to make sure a value is not empty. If the value being validated is not a string, the rule works the same as Length.
func (*LengthRule) Error ¶
func (v *LengthRule) Error(message string) *LengthRule
Error sets the error message for the rule.
func (*LengthRule) Validate ¶
func (v *LengthRule) Validate(value interface{}) (code int, args []interface{})
Validate checks if the given value is valid or not.
type MatchRule ¶
type MatchRule struct {
// contains filtered or unexported fields
}
func Match ¶
Match returns a validation rule that checks if a value matches the specified regular expression. This rule should only be used for validating strings and byte slices, or a validation error will be reported. An empty value is considered valid. Use the Required rule to make sure a value is not empty.
type NotInRule ¶
type NotInRule struct {
// contains filtered or unexported fields
}
func NotIn ¶
func NotIn(values ...interface{}) *NotInRule
NotIn returns a validation rule that checks if a value os absent from, the given list of values. Note that the value being checked and the possible range of values must be of the same type. An empty value is considered valid. Use the Required rule to make sure a value is not empty.
type Rule ¶
type Rule interface { // Validate validates a value and returns a value if validation fails. Validate(value interface{}) (code int, args []interface{}) }
Rule represents a validation rule.
type RuleFunc ¶
type RuleFunc func(value interface{}) (code int, args []interface{})
RuleFunc represents a validator function. You may wrap it as a Rule by calling By().
type StringRule ¶
type StringRule struct {
// contains filtered or unexported fields
}
StringRule is a rule that checks a string variable using a specified stringValidator.
func NewStringRule ¶
func NewStringRule(validator stringValidator, code int) *StringRule
NewStringRule creates a new validation rule using a function that takes a string value and returns a bool. The rule returned will use the function to check if a given string or byte slice is valid or not. An empty value is considered to be valid. Please use the Required rule to make sure a value is not empty.
func (*StringRule) Error ¶
func (v *StringRule) Error(message string, code int) *StringRule
Error sets the error message for the rule.
func (*StringRule) Validate ¶
func (v *StringRule) Validate(value interface{}) (code int, args []interface{})
Validate checks if the given value is valid or not.
type ThresholdRule ¶
type ThresholdRule struct {
// contains filtered or unexported fields
}
func Max ¶
func Max(max interface{}) *ThresholdRule
Max is a validation rule that checks if a value is less or equal than the specified value. By calling Exclusive, the rule will check if the value is strictly less than the specified value. Note that the value being checked and the threshold value must be of the same type. Only int, uint, float and time.Time types are supported. An empty value is considered valid. Please use the Required rule to make sure a value is not empty.
func Min ¶
func Min(min interface{}) *ThresholdRule
Min is a validation rule that checks if a value is greater or equal than the specified value. By calling Exclusive, the rule will check if the value is strictly greater than the specified value. Note that the value being checked and the threshold value must be of the same type. Only int, uint, float and time.Time types are supported. An empty value is considered valid. Please use the Required rule to make sure a value is not empty.
func (*ThresholdRule) Exclusive ¶
func (r *ThresholdRule) Exclusive() *ThresholdRule
Exclusive sets the comparison to exclude the boundary value.
func (*ThresholdRule) Validate ¶
func (r *ThresholdRule) Validate(value interface{}) (code int, args []interface{})
Validate checks if the given value is valid or not.
type Validatable ¶
type Validatable interface { // Validate validates the data and returns an error if validation fails. Validate() (code int, args []interface{}) }
Validatable is the interface indicating the type implementing it supports data validation.