Documentation ¶
Index ¶
- Constants
- func AddRule(name string, rule *RuleDefinition)
- func ClearRegexCache()
- func GetFieldFromName(name string, data map[string]interface{}) (string, interface{}, map[string]interface{}, bool)
- func GetFieldType(value interface{}) string
- func SetPlaceholder(placeholderName string, replacer Placeholder)
- type ArrayErrors
- type Context
- type Errors
- type Field
- type FieldErrors
- type FieldMap
- type FieldMapApplier
- type List
- type Placeholder
- type PostValidationHook
- type Rule
- type RuleDefinition
- type RuleFunc
- type RuleSet
- type RuleSetApplier
- type Ruler
- type Rules
- type StructList
Constants ¶
const ( // CurrentElement special key for field name in composite rule sets. // Use it if you want to apply rules to the current object element. // You cannot apply rules on the root element, these rules will only // apply if the rule set is used with composition. CurrentElement = "" )
Variables ¶
This section is empty.
Functions ¶
func AddRule ¶
func AddRule(name string, rule *RuleDefinition)
AddRule register a validation rule. The rule will be usable in request validation by using the given rule name.
Type-dependent messages let you define a different message for numeric, string, arrays and files. The language entry used will be "validation.rules.rulename.type"
func ClearRegexCache ¶
func ClearRegexCache()
ClearRegexCache empties the validation regex cache. Note that if validation.Validate is subsequently called, regex will need to be recompiled.
func GetFieldFromName ¶
func GetFieldFromName(name string, data map[string]interface{}) (string, interface{}, map[string]interface{}, bool)
GetFieldFromName find potentially nested field by it's dot-separated path in the given object. Returns the name without its prefix, the value, its parent object and a bool indicating if it has been found or not.
func GetFieldType ¶
func GetFieldType(value interface{}) string
GetFieldType returns the non-technical type of the given "value" interface. This is used by validation rules to know if the input data is a candidate for validation or not and is especially useful for type-dependent rules.
- "numeric" if the value is an int, uint or a float
- "string" if the value is a string
- "array" if the value is a slice
- "file" if the value is a slice of "fsutil.File"
- "unsupported" otherwise
func SetPlaceholder ¶
func SetPlaceholder(placeholderName string, replacer Placeholder)
SetPlaceholder sets the replacer function for the given placeholder. If a placeholder with this name already exists, the latter will be overridden.
validation.SetPlaceholder("min", func(field string, rule string, parameters []string, language string) string { return parameters[0] // Replace ":min" by the first parameter in the rule definition })
Types ¶
type ArrayErrors ¶
type ArrayErrors map[int]*FieldErrors
ArrayErrors structure representing the errors associated with an element of an array. The key is the index of the element in the array, or -1 if the element doesn't exist.
type Context ¶
type Context struct { Data map[string]interface{} Extra map[string]interface{} Value interface{} Parent interface{} Field *Field Rule *Rule Now time.Time // The name of the field under validation Name string // contains filtered or unexported fields }
Context validation context for RuleFunc. Contains all the information needed for validation rules.
func NewContext ¶ added in v4.4.10
func NewContext() *Context
NewContext returns a new valid context. Used for testing.
type Errors ¶
type Errors map[string]*FieldErrors
Errors structure representing errors associated with an element of the validated data. The element may represent the root object or the fields of a nested object. The key is the name of the field.
func Validate ¶
Validate the given data with the given rule set. If all validation rules pass, returns nil. Third parameter tells the function if the data comes from a JSON request. Last parameter sets the language of the validation error messages.
func ValidateWithExtra ¶ added in v4.4.0
func ValidateWithExtra(data map[string]interface{}, rules Ruler, isJSON bool, language string, extra map[string]interface{}) Errors
ValidateWithExtra the given data with the given rule set. If all validation rules pass, returns nil. Third parameter tells the function if the data comes from a JSON request. The fourth parameter sets the language of the validation error messages. The last parameter is a map of extra information given to validation rules via `validation.Context.Extra`. This map is copied for each validation rule. One copy of the extra map is therefore scoped to a single validation rule function call and the resulting validation error message / placeholder.
type Field ¶
type Field struct { Path *walk.Path Elements *Field // If the field is an array, the field representing its elements, or nil // Maybe use the same concept for objects too? Rules []*Rule // contains filtered or unexported fields }
Field is a component of route validation. A Field is a value in a Rules map, the key being the name of the field.
func (*Field) Check ¶
func (f *Field) Check()
Check if rules meet the minimum parameters requirement and update the isRequired, isNullable and isArray fields.
func (*Field) IsNullable ¶
IsNullable check if a field has the "nullable" rule
func (*Field) IsRequired ¶
IsRequired check if a field has the "required" rule
type FieldErrors ¶
type FieldErrors struct { Fields Errors `json:"fields,omitempty"` Elements ArrayErrors `json:"elements,omitempty"` Errors []string `json:"errors,omitempty"` }
FieldErrors structure representing the errors associated with an element. If the element is an object (`map[string]interface{}`), `Fields` represents the errors associated with this object's fields. The key is the name of the field. `Fields` may be `nil`. If the element is a slice, `Elements` represents the errors associated with each element of the array. See `ArrayErrors` for more details.
func (*FieldErrors) Add ¶
func (e *FieldErrors) Add(path *walk.Path, message string)
Add an error message to the element identified by the given path. If a step in the path of type `PathTypeArray` doesn't provide an index, -1 will be used to indicate that the element doesn't exist. Creates all missing elements in the path.
type FieldMap ¶
type FieldMap map[string]FieldMapApplier
FieldMap is an alias to shorten verbose validation rules declaration. Maps a field name (key) with a Field struct (value).
type FieldMapApplier ¶
type FieldMapApplier interface {
// contains filtered or unexported methods
}
FieldMapApplier types implementing this interface define their behavior when they're applied to a FieldMap. This enables verbose syntax rule sets composition.
type List ¶
type List []string
List of rules string representation. e.g.: `validation.List{"required", "min:10"}`
type Placeholder ¶
Placeholder function defining a placeholder in a validation message. This function should return the value to replace the placeholder with.
type PostValidationHook ¶ added in v4.3.0
PostValidationHook executed after the whole validation process is over, no matter if the validation passes or not. The errors parameter is never nil (but can be empty). A post validation hook can process additional checks on the data and alter the resulting "validation.Errors" as needed. These hooks always return a "validation.Errors", typically the same as the one they received as a paramater, but it is possible to return an entirely different instance of "validation.Errors".
type Rule ¶
Rule is a component of rule sets for route validation. Each validated fields has one or multiple validation rules. The goal of this struct is to gather information about how to use a rule definition for this field. This inludes the rule name (referring to a RuleDefinition), the parameters and the array dimension for array validation.
func (*Rule) IsType ¶
IsType returns true if the rule definition is a type rule. See RuleDefinition.IsType
func (*Rule) IsTypeDependent ¶
IsTypeDependent returns true if the rule definition is a type-dependent rule. See RuleDefinition.IsTypeDependent
type RuleDefinition ¶
type RuleDefinition struct { // The Function field is the function that will be executed Function RuleFunc // The minimum amount of parameters RequiredParameters int // A type rule is a rule that checks if a field has a certain type // and can convert the raw value to a value fitting. For example, the UUID // rule is a type rule because it takes a string as input, checks if it's a // valid UUID and converts it to a "uuid.UUID". // The "array" rule is an exception. It does convert the value to a new slice of // the correct type if provided, but is not considered a type rule to avoid being // able to be used as parameter for itself ("array:array"). IsType bool // Type-dependent rules are rules that can be used with different field types // (numeric, string, arrays and files) and have a different validation messages // depending on the type. // The language entry used will be "validation.rules.rulename.type" IsTypeDependent bool // ComparesFields is true when the rule compares the value of the field under // validation with another field. A field containing at least one rule with // ComparesFields = true will be executed later in the validation process to // ensure conversions are properly executed prior. ComparesFields bool }
RuleDefinition is the definition of a rule, containing the information related to the behavior executed on validation-time.
type RuleFunc ¶
RuleFunc function defining a validation rule. Passing rules should return true, false otherwise.
Rules can modifiy the validated value if needed. For example, the "numeric" rule converts the data to float64 if it's a string.
type RuleSet ¶
type RuleSet map[string]RuleSetApplier
RuleSet is a request rules definition. Each entry is a field in the request.
type RuleSetApplier ¶
type RuleSetApplier interface {
// contains filtered or unexported methods
}
RuleSetApplier types implementing this interface define their behavior when they're applied to a RuleSet. This enables rule sets composition.
type Ruler ¶
type Ruler interface {
AsRules() *Rules
}
Ruler adapter interface for method dispatching between RuleSet and Rules at route registration time. Allows to input both of these types as parameters of the Route.Validate method.
type Rules ¶
type Rules struct { Fields FieldMap PostValidationHooks []PostValidationHook // contains filtered or unexported fields }
Rules is a component of route validation and maps a field name (key) with a Field struct (value).
func (*Rules) Check ¶
func (r *Rules) Check()
Check all rules in this set. This function will panic if any of the rules doesn't refer to an existing RuleDefinition, doesn't meet the parameters requirement, or if the rule cannot be used in array validation while ArrayDimension is not equal to 0. Also processes composition. After calling this function, you can safely assume all `Fields` elements are of type `*Field`.
type StructList ¶
type StructList []*Rule
StructList of rules struct representation. e.g.:
validation.StructList{ {Name: "required"}, {Name: "min", Params: []string{"3"}}, }