Documentation ¶
Index ¶
- Constants
- Variables
- func AddRule(name string, typeDependentMessage bool, rule Rule)
- func GetFieldType(value interface{}) string
- func RequireParametersCount(rule string, params []string, count int)
- func SetPlaceholder(placeholderName string, replacer Placeholder)
- type Errors
- type Placeholder
- type Rule
- type RuleSet
Constants ¶
const ( FormatDate = "2006-01-02" FormatDateTime = "2006-01-02T15:04:05" )
List of all formats that's possible to use to parse date, datetime, and etc.
Variables ¶
var ( // ErrCantParseDate shows that the date string cannot be parsed ErrCantParseDate = errors.New("Date string value cannot be parsed") // ErrCantParseDateInOtherField shows that the date can't be parsed in other field. ErrCantParseDateInOtherField = errors.New("Cannot parse date in other field") // ErrCantParseDateInParams shows that the date can't be parsed in other params ErrCantParseDateInParams = errors.New("Cannot parse date in params") // ErrValueIsNotADate shows that the current value is not a date. ErrValueIsNotADate = errors.New("Value is not a date") )
var ( // ErrInvalidDateNotString is an error for date when the expected type is not string. ErrInvalidDateNotString = errors.New("Date is not a string so cannot be parsed") )
Functions ¶
func AddRule ¶
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 ¶
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 ¶
Errors is a map of validation errors with the field name as a key.
type Placeholder ¶
Placeholder function defining a placeholder in a validation message. This function should return the value to replace the placeholder with.