validation

package
v2.10.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2020 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddRule

func AddRule(name string, typeDependentMessage bool, rule Rule)

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 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 RequireParametersCount

func RequireParametersCount(rule string, params []string, count int)

RequireParametersCount checks if the given parameters slice has at least "count" elements. If this is not the case, panics.

Use this to make sure your validation rules are correctly used.

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 RuleSet, 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 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 func(string, interface{}, []string, map[string]interface{}) bool

Rule 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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL