Documentation
¶
Overview ¶
Package validator contains Validator service singleton. It can be used in a custom application to perform the validation process.
Index ¶
- func AtIndex(index int) *validation.Validator
- func AtProperty(name string) *validation.Validator
- func BuildViolation(code, message string) *validation.ViolationBuilder
- func Reset()
- func SetOptions(options ...validation.ValidatorOption) error
- func Validate(arguments ...validation.Argument) error
- func ValidateBool(value *bool, options ...validation.Option) error
- func ValidateBy(constraintKey string) validation.Constraint
- func ValidateCountable(count int, options ...validation.Option) error
- func ValidateEach(value interface{}, options ...validation.Option) error
- func ValidateEachString(strings []string, options ...validation.Option) error
- func ValidateIterable(value interface{}, options ...validation.Option) error
- func ValidateNumber(value interface{}, options ...validation.Option) error
- func ValidateString(value *string, options ...validation.Option) error
- func ValidateTime(value *time.Time, options ...validation.Option) error
- func ValidateValidatable(validatable validation.Validatable, options ...validation.Option) error
- func ValidateValue(value interface{}, options ...validation.Option) error
- func WithContext(ctx context.Context) *validation.Validator
- func WithLanguage(tag language.Tag) *validation.Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AtIndex ¶
func AtIndex(index int) *validation.Validator
AtIndex method creates a new scoped validator with injected array index element to scope property path.
func AtProperty ¶
func AtProperty(name string) *validation.Validator
AtProperty method creates a new scoped validator with injected property name element to scope property path.
func BuildViolation ¶
func BuildViolation(code, message string) *validation.ViolationBuilder
BuildViolation can be used to build a custom violation on the client-side.
Example
err := validator.BuildViolation("", ""). AddParameter("key", "value"). CreateViolation()
func Reset ¶
func Reset()
Reset function recreates singleton validator. Generally, it can be used in tests.
func SetOptions ¶
func SetOptions(options ...validation.ValidatorOption) error
SetOptions can be used to set up a singleton validator. Make sure you call this function once at the initialization of your application.
func Validate ¶
func Validate(arguments ...validation.Argument) error
Validate is the main validation method. It accepts validation arguments. Arguments can be used to tune up the validation process or to pass values of a specific type.
func ValidateBool ¶
func ValidateBool(value *bool, options ...validation.Option) error
ValidateBool is an alias for validating a single boolean value.
func ValidateBy ¶
func ValidateBy(constraintKey string) validation.Constraint
ValidateBy is used to get the constraint from the internal validator store. If the constraint does not exist, then the validator will return a ConstraintNotFoundError during the validation process. For storing a constraint you should use the validation.StoredConstraint option.
func ValidateCountable ¶
func ValidateCountable(count int, options ...validation.Option) error
ValidateCountable is an alias for validating a single countable value (an array, slice, or map).
func ValidateEach ¶
func ValidateEach(value interface{}, options ...validation.Option) error
ValidateEach is an alias for validating each value of an iterable (an array, slice, or map).
func ValidateEachString ¶
func ValidateEachString(strings []string, options ...validation.Option) error
ValidateEachString is an alias for validating each value of a strings slice.
func ValidateIterable ¶
func ValidateIterable(value interface{}, options ...validation.Option) error
ValidateIterable is an alias for validating a single iterable value (an array, slice, or map).
func ValidateNumber ¶
func ValidateNumber(value interface{}, options ...validation.Option) error
ValidateNumber is an alias for validating a single numeric value (integer or float).
func ValidateString ¶
func ValidateString(value *string, options ...validation.Option) error
ValidateString is an alias for validating a single string value.
func ValidateTime ¶
func ValidateTime(value *time.Time, options ...validation.Option) error
ValidateTime is an alias for validating a single time value.
func ValidateValidatable ¶
func ValidateValidatable(validatable validation.Validatable, options ...validation.Option) error
ValidateValidatable is an alias for validating value that implements the Validatable interface.
func ValidateValue ¶
func ValidateValue(value interface{}, options ...validation.Option) error
ValidateValue is an alias for validating a single value of any supported type.
func WithContext ¶
func WithContext(ctx context.Context) *validation.Validator
Example
err := validator.WithContext(request.Context()).Validate( String(&s, it.IsNotBlank()), // now all called constraints will use passed context in their methods )
func WithLanguage ¶
func WithLanguage(tag language.Tag) *validation.Validator
WithLanguage method creates a new scoped validator with a given language tag. All created violations will be translated into this language.
Example
err := validator.WithLanguage(language.Russian).Validate( validation.ValidateString(&s, it.IsNotBlank()), // violation from this constraint will be translated )
Types ¶
This section is empty.