validation

package
Version: v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2019 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 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(request *http.Request, data map[string]interface{}, rules RuleSet, language string) Errors

Validate the given request with the given rule set If all validation rules pass, returns nil

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