gvalid

package
v1.13.3-0...-1915ff6 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2020 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

Package gvalid implements powerful and useful data/form validation functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterRule

func RegisterRule(rule string, f RuleFunc) error

RegisterRule registers custom validation rule and function for package. It returns error if there's already the same rule registered previously.

Types

type CustomMsg

type CustomMsg = map[string]interface{}

CustomMsg is the custom error message type, like: map[field] => string|map[rule]string

type Error

type Error struct {
	// contains filtered or unexported fields
}

Error is the validation error for validation result.

func Check

func Check(value interface{}, rules string, messages interface{}, params ...interface{}) *Error

Check checks single value with specified rules. It returns nil if successful validation.

The parameter <value> can be any type of variable, which will be converted to string for validation. The parameter <rules> can be one or more rules, multiple rules joined using char '|'. The parameter <messages> specifies the custom error messages, which can be type of: string/map/struct/*struct. The optional parameter <params> specifies the extra validation parameters for some rules like: required-*、same、different, etc.

func CheckMap

func CheckMap(params interface{}, rules interface{}, messages ...CustomMsg) *Error

CheckMap validates map and returns the error result. It returns nil if with successful validation.

The parameter <rules> can be type of []string/map[string]string. It supports sequence in error result if <rules> is type of []string. The optional parameter <messages> specifies the custom error messages for specified keys and rules.

func CheckStruct

func CheckStruct(object interface{}, rules interface{}, messages ...CustomMsg) *Error

CheckStruct validates strcut and returns the error result.

The parameter <object> should be type of struct/*struct. The parameter <rules> can be type of []string/map[string]string. It supports sequence in error result if <rules> is type of []string. The optional parameter <messages> specifies the custom error messages for specified keys and rules.

func (*Error) Error

func (e *Error) Error() string

Error implements interface of error.Error.

func (*Error) FirstItem

func (e *Error) FirstItem() (key string, messages map[string]string)

FirstItem returns the field name and error messages for the first validation rule error.

func (*Error) FirstRule

func (e *Error) FirstRule() (rule string, err string)

FirstRule returns the first error rule and message string.

func (*Error) FirstString

func (e *Error) FirstString() (err string)

FirstString returns the first error message as string. Note that the returned message might be different if it has no sequence.

func (*Error) Map

func (e *Error) Map() map[string]string

Map returns the first error message as map.

func (*Error) Maps

func (e *Error) Maps() ErrorMap

Maps returns all error messages as map.

func (*Error) String

func (e *Error) String() string

String returns all error messages as string, multiple error messages joined using char ';'.

func (*Error) Strings

func (e *Error) Strings() (errs []string)

Strings returns all error messages as string array.

type ErrorMap

type ErrorMap map[string]map[string]string

ErrorMap is the validation error map: map[field]map[rule]message

type RuleFunc

type RuleFunc func(value interface{}, message string, params map[string]interface{}) error

RuleFunc is the custom function for data validation. The parameter <value> specifies the value for this rule to validate. The parameter <message> specifies the custom error message or configured i18n message for this rule. The parameter <params> specifies all the parameters that needs .

Jump to

Keyboard shortcuts

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