Documentation ¶
Index ¶
- Variables
- func By(f RuleFunc) validate.Rule
- func EnsureString(value interface{}) (string, error)
- func Indirect(value interface{}) (interface{}, bool)
- func IsEmpty(value interface{}) bool
- func LengthOfValue(value interface{}) (int, error)
- 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)
- type Error
- type ErrorObject
- func (e ErrorObject) AddParam(name string, value interface{}) Error
- func (e ErrorObject) Code() string
- func (e ErrorObject) Error() string
- func (e ErrorObject) Message() string
- func (e ErrorObject) Params() map[string]interface{}
- func (e ErrorObject) SetCode(code string) Error
- func (e ErrorObject) SetMessage(message string) Error
- func (e ErrorObject) SetParams(params map[string]interface{}) Error
- type LengthRule
- type MatchRule
- type RequiredRule
- type RuleFunc
Constants ¶
This section is empty.
Variables ¶
var ( // ErrLengthTooLong is the error that returns in case of too long length. ErrLengthTooLong = NewError("validation_length_too_long", "the length must be no more than {{.max}}") // ErrLengthTooShort is the error that returns in case of too short length. ErrLengthTooShort = NewError("validation_length_too_short", "the length must be no less than {{.min}}") // ErrLengthInvalid is the error that returns in case of an invalid length. ErrLengthInvalid = NewError("validation_length_invalid", "the length must be exactly {{.min}}") // ErrLengthOutOfRange is the error that returns in case of out of range length. ErrLengthOutOfRange = NewError("validation_length_out_of_range", "the length must be between {{.min}} and {{.max}}") // ErrLengthEmptyRequired is the error that returns in case of non-empty value. ErrLengthEmptyRequired = NewError("validation_length_empty_required", "the value must be empty") )
var ( // ErrRequired is the error that returns when a value is required. ErrRequired = NewError("validation_required", "cannot be blank") // ErrNilOrNotEmpty is the error that returns when a value is not nil and is empty. ErrNilOrNotEmpty = NewError("validation_nil_or_not_empty_required", "cannot be blank") )
var ErrMatchInvalid = NewError("validation_match_invalid", "must be in a valid format")
ErrMatchInvalid is the error that returns in case of invalid format.
var NilOrNotEmpty = RequiredRule{/* contains filtered or unexported fields */}
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 Required = RequiredRule{/* contains filtered or unexported fields */}
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 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.
Types ¶
type Error ¶
type Error interface { Error() string Code() string Message() string SetMessage(string) Error Params() map[string]interface{} SetParams(map[string]interface{}) Error }
Error interface represents an validation error
type ErrorObject ¶
type ErrorObject struct {
// contains filtered or unexported fields
}
ErrorObject is the default validation error that implements the Error interface.
func (ErrorObject) AddParam ¶
func (e ErrorObject) AddParam(name string, value interface{}) Error
AddParam add parameter to the error's parameters.
func (ErrorObject) Message ¶
func (e ErrorObject) Message() string
Message return the error's message.
func (ErrorObject) Params ¶
func (e ErrorObject) Params() map[string]interface{}
Params returns the error's params.
func (ErrorObject) SetCode ¶
func (e ErrorObject) SetCode(code string) Error
SetCode set the error's translation code.
func (ErrorObject) SetMessage ¶
func (e ErrorObject) SetMessage(message string) Error
SetMessage set the error's message.
func (ErrorObject) SetParams ¶
func (e ErrorObject) SetParams(params map[string]interface{}) Error
SetParams set the error's params.
type LengthRule ¶
type LengthRule struct {
// contains filtered or unexported fields
}
LengthRule is a validation rule that checks if a value's length is within the specified range.
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 (r LengthRule) Error(message string) LengthRule
Error sets the error message for the rule.
func (LengthRule) ErrorObject ¶
func (r LengthRule) ErrorObject(err Error) LengthRule
ErrorObject sets the error struct for the rule.
func (LengthRule) Validate ¶
func (r LengthRule) Validate(value interface{}) error
Validate checks if the given value is valid or not.
type MatchRule ¶
type MatchRule struct {
// contains filtered or unexported fields
}
MatchRule is a validation rule that checks if a value matches the specified regular expression.
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.
func (MatchRule) ErrorObject ¶
ErrorObject sets the error struct for the rule.
type RequiredRule ¶
type RequiredRule struct {
// contains filtered or unexported fields
}
RequiredRule is a rule that checks if a value is not empty.
func (RequiredRule) Error ¶
func (r RequiredRule) Error(message string) RequiredRule
Error sets the error message for the rule.
func (RequiredRule) ErrorObject ¶
func (r RequiredRule) ErrorObject(err Error) RequiredRule
ErrorObject sets the error struct for the rule.
func (RequiredRule) Validate ¶
func (r RequiredRule) Validate(value interface{}) error
Validate checks if the given value is valid or not.
func (RequiredRule) When ¶
func (r RequiredRule) When(condition bool) RequiredRule
When sets the condition that determines if the validation should be performed.