valid

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeleteRule

func DeleteRule(rules ...string)

DeleteRule deletes custom defined validation one or more rules and associated functions from global package.

func GetRegisteredRuleMap

func GetRegisteredRuleMap() map[string]RuleFunc

GetRegisteredRuleMap returns all the custom registered rules and associated functions.

func GetTags

func GetTags() []string

GetTags returns the validation tags.

func ParseTagValue

func ParseTagValue(tag string) (field, rule, msg string)

ParseTagValue parses one sequence tag to field, rule and error message. The sequence tag is like: [alias@]rule[...#msg...]

func RegisterRule

func RegisterRule(rule string, f RuleFunc)

RegisterRule registers custom validation rule and function for package.

func RegisterRuleByMap

func RegisterRuleByMap(m map[string]RuleFunc)

RegisterRuleByMap registers custom validation rules using map for package.

func Trim

func Trim(str string, characterMask ...string) string

Trim strips whitespace (or other characters) from the beginning and end of a string. The optional parameter `characterMask` specifies the additional stripped characters.

func ValidateStruct

func ValidateStruct(s interface{}) error

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 interface {
	Code() gcode.Code
	Current() error
	Error() string
	FirstItem() (key string, messages map[string]error)
	FirstRule() (rule string, err error)
	FirstError() (err error)
	Items() (items []map[string]map[string]error)
	Map() map[string]error
	Maps() map[string]map[string]error
	String() string
	Strings() (errs []string)
}

Error is the validation error for validation result.

type RuleFunc

type RuleFunc func(ctx context.Context, in RuleFuncInput) error

RuleFunc is the custom function for data validation.

type RuleFuncInput

type RuleFuncInput struct {
	// Rule specifies the validation rule string, like "required", "between:1,100", etc.
	Rule string

	// Message specifies the custom error message or configured i18n message for this rule.
	Message string

	// Field specifies the field for this rule to validate.
	Field string

	// ValueType specifies the type of the value, which might be nil.
	ValueType reflect.Type

	// Value specifies the value for this rule to validate.
	Value *any

	// Data specifies the `data` which is passed to the Validator. It might be a type of map/struct or a nil value.
	// You can ignore the parameter `Data` if you do not really need it in your custom validation rule.
	Data *any
}

RuleFuncInput holds the input parameters that passed to custom rule function RuleFunc.

type StrSet

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

func NewStrSet

func NewStrSet(safe ...bool) *StrSet

NewStrSet create and returns a new set, which contains un-repeated items. The parameter `safe` is used to specify whether using set in concurrent-safety, which is false in default.

func (*StrSet) AddIfNotExist

func (set *StrSet) AddIfNotExist(item string) bool

AddIfNotExist checks whether item exists in the set, it adds the item to set and returns true if it does not exist in the set, or else it does nothing and returns false.

func (*StrSet) Contains

func (set *StrSet) Contains(item string) bool

Contains checks whether the set contains `item`.

type Validator

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

Validator is the validation manager for chaining operations.

func New

func New() *Validator

New creates and returns a new Validator.

func (*Validator) Assoc

func (v *Validator) Assoc(assoc interface{}) *Validator

Assoc is a chaining operation function, which sets associated validation data for current operation. The optional parameter `assoc` is usually type of map, which specifies the parameter map used in union validation. Calling this function with `assoc` also sets `useAssocInsteadOfObjectAttributes` true

func (*Validator) Bail

func (v *Validator) Bail() *Validator

Bail sets the mark for stopping validation after the first validation error.

func (*Validator) Ci

func (v *Validator) Ci() *Validator

Ci sets the mark for Case-Insensitive for those rules that need value comparison.

func (*Validator) Clone

func (v *Validator) Clone() *Validator

Clone creates and returns a new Validator which is a shallow copy of current one.

func (*Validator) Data

func (v *Validator) Data(data interface{}) *Validator

Data is a chaining operation function, which sets validation data for current operation.

func (*Validator) Foreach

func (v *Validator) Foreach() *Validator

Foreach tells the next validation using current value as an array and validates each of its element. Note that this decorating rule takes effect just once for next validation rule, specially for single value validation.

func (*Validator) Messages

func (v *Validator) Messages(messages interface{}) *Validator

Messages is a chaining operation function, which sets custom error messages for current operation. The parameter `messages` can be type of string/[]string/map[string]string. It supports sequence in error result if `rules` is type of []string.

func (*Validator) RuleFunc

func (v *Validator) RuleFunc(rule string, f RuleFunc) *Validator

RuleFunc registers one custom rule function to current Validator.

func (*Validator) RuleFuncMap

func (v *Validator) RuleFuncMap(m map[string]RuleFunc) *Validator

RuleFuncMap registers multiple custom rule functions to current Validator.

func (*Validator) Rules

func (v *Validator) Rules(rules interface{}) *Validator

Rules is a chaining operation function, which sets custom validation rules for current operation.

func (*Validator) Run

func (v *Validator) Run(ctx context.Context) error

Run starts validating the given data with rules and messages.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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