validator

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package validator contains Validator service singleton. It can be used in a custom application to perform the validation process.

Index

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(ctx context.Context, code, message string) *validation.ViolationBuilder

BuildViolation can be used to build a custom violation on the client-side.

Example

err := validator.BuildViolation(context.Background(), "", "").
    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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, 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(ctx context.Context, strings []string, options ...validation.Option) error

ValidateEachString is an alias for validating each value of a strings slice.

func ValidateIterable

func ValidateIterable(ctx context.Context, 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(ctx context.Context, value interface{}, options ...validation.Option) error

ValidateNumber is an alias for validating a single numeric value (integer or float).

func ValidateString

func ValidateString(ctx context.Context, value string, options ...validation.Option) error

ValidateString is an alias for validating a single string value.

func ValidateStrings added in v0.4.0

func ValidateStrings(ctx context.Context, values []string, options ...validation.Option) error

ValidateStrings is an alias for validating slice of strings.

func ValidateTime

func ValidateTime(ctx context.Context, value time.Time, options ...validation.Option) error

ValidateTime is an alias for validating a single time value.

func ValidateValidatable

func ValidateValidatable(ctx context.Context, validatable validation.Validatable, options ...validation.Option) error

ValidateValidatable is an alias for validating value that implements the Validatable interface.

func ValidateValue

func ValidateValue(ctx context.Context, value interface{}, options ...validation.Option) error

ValidateValue is an alias for validating a single value of any supported type.

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.

Jump to

Keyboard shortcuts

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