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 StoreConstraint(key string, constraint validation.Constraint) 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 StoreConstraint ¶
func StoreConstraint(key string, constraint validation.Constraint) error
StoreConstraint can be used to store a constraint in an internal validator store. It can later be used by the ValidateBy method. This can be useful for passing custom or prepared constraints to Validatable.
If the constraint already exists, a ConstraintAlreadyStoredError is returned.
Due to the fact that the store is a state element, it is strongly recommended to fill the store during application initialization to avoid unexpected problems.
Example
err := validator.StoreConstraint("isTagExists", isTagExistsConstraint)
if err != nil {
log.Fatal(err)
}
s := "
err = validator.ValidateString(&s, validator.ValidateBy("isTagExists"))
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 StoreConstraint method.
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.