Documentation
¶
Index ¶
- 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 Errors
- type Field
- type FieldMap
- type Placeholder
- type Rule
- type RuleDefinition
- type RuleFunc
- type RuleSet
- type Ruler
- type Rules
Constants ¶
This section is empty.
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 "filesystem.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 Errors ¶
Errors is a map of validation errors with the field name as a key.
type Field ¶
type Field struct { 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 ¶ added in v3.11.0
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 FieldMap ¶
FieldMap is an alias to shorten verbose validation rules declaration. Maps a field name (key) with a Field struct (value).
type Placeholder ¶
Placeholder function defining a placeholder in a validation message. This function should return the value to replace the placeholder with.
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 ¶ added in v3.8.0
IsType returns true if the rule definition is a type rule. See RuleDefinition.IsType
func (*Rule) IsTypeDependent ¶ added in v3.8.0
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 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 // 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 ¶ added in v3.11.0
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.