Documentation

Index

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

type Errors map[string][]string

Errors is a map of validation errors with the field name as a key.

func Validate

func Validate(data map[string]interface{}, rules Ruler, isJSON bool, language string) Errors

Validate the given data with the given rule set. If all validation rules pass, returns an empty "validation.Errors". Third parameter tells the function if the data comes from a JSON request. Last parameter sets the language of the validation error messages.

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) IsArray

func (v *Field) IsArray() bool

IsArray check if a field has the "array" rule

func (*Field) IsNullable

func (v *Field) IsNullable() bool

IsNullable check if a field has the "nullable" rule

func (*Field) IsRequired

func (v *Field) IsRequired() bool

IsRequired check if a field has the "required" rule

type FieldMap

type FieldMap map[string]*Field

FieldMap is an alias to shorten verbose validation rules declaration. Maps a field name (key) with a Field struct (value).

type Placeholder

type Placeholder func(string, string, []string, string) string

Placeholder function defining a placeholder in a validation message. This function should return the value to replace the placeholder with.

type Rule

type Rule struct {
	Name           string
	Params         []string
	ArrayDimension uint8
}

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.

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
}

RuleDefinition is the definition of a rule, containing the information related to the behavior executed on validation-time.

type RuleFunc

type RuleFunc func(string, interface{}, []string, map[string]interface{}) bool

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][]string

RuleSet is a request rules definition. Each entry is a field in the request.

func (RuleSet) AsRules

func (r RuleSet) AsRules() *Rules

AsRules parses and checks this RuleSet and returns it as Rules.

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 map[string]*Field
	// 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) AsRules

func (r *Rules) AsRules() *Rules

AsRules performs the checking and returns the same Rules instance.