validation

package
v0.0.0-...-947b840 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TAG_NAME      = "validate"
	TAG_OPTION_ID = "id"
)

Variables

View Source
var (
	ErrorAppendIncompatibleError    = fmt.Errorf("validation.ValidationErrors.Append: Attempt to subAppend incompatible error.")
	ErrorSubAppendIncompatibleError = fmt.Errorf("validation.ValidationErrors.SubAppend: Attempt to subAppend incompatible error.")
)
View Source
var (
	ErrorMissingStructPointer = fmt.Errorf("validation.Struct: Struct pointer is missing!")
	ErrorInvalidStructPointer = fmt.Errorf("validation.Struct: First argument must be a pointer to a struct!")
	ErrorStructFieldNotFound  = fmt.Errorf("validation.Struct: Struct field not found int the struct!")
	ErrorFieldIsNotPointer    = fmt.Errorf("validation.Field: First argument must be a pointer to a struct field!")
)
View Source
var ErrorExportInterface = fmt.Errorf("validators.extra_UniqueField: Cannot export private field!")
View Source
var ErrorFieldNotFound = fmt.Errorf("validators.extra_UniqueField: Field not found!")

Functions

func ClearCustomValidators

func ClearCustomValidators()

RegisterCustomValidator registers custom validator with custom key.

func RegisterCustomValidator

func RegisterCustomValidator(key string, v Validator)

RegisterCustomValidator registers custom validator with custom key.

func RemoveCustomValidator

func RemoveCustomValidator(key string)

RemoveCustomValidator removes custom validator from a list.

func SetTagName

func SetTagName(tagName string)

SetTagName sets primary name tag from which validation options are extracted.

func Struct

func Struct(sPtr interface{}, validators ...FieldValidator) error

Struct validates every given field against corresponding validators.

It panics if a provided value is not a pointer to a struct or if a field cannot be found within the structure.

func TopParent

func TopParent() interface{}

TopParent returns first struct in validation stream.

func Var

func Var(value interface{}, validators ...Validator) error

Var validates a variable against the provided validators. Validation will dive deeper, if variable is validatable struct, map or slice.

Types

type Action

type Action string
const (
	UNKNOWN   Action = ""
	SKIP      Action = "SKIP"
	FAIL      Action = "FAIL"
	OMITEMPTY Action = "OMITEMPTY"
	NOT_EMPTY Action = "NOT_EMPTY"
)

type FieldValidator

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

FieldValidator contains a pointer to a struct field and corresponding validators.

func Field

func Field(fPtr interface{}, validators ...Validator) FieldValidator

Field returns new FieldValidator.

type Validatable

type Validatable interface {
	Validate() error
}

Structure is validatable if it contains Validate method that returns an error.

type ValidationError

type ValidationError struct {
	Valid           bool
	Namespace       string
	Field           string
	StructNamespace string
	StructField     string
	Tag             string
	ActualTag       string
	Kind            reflect.Kind
	Type            reflect.Type
	Param           string
	Value           interface{}
	Err             string
	RealErr         string
}

Custom validation error.

func (ValidationError) Error

func (e ValidationError) Error() string

Error returns validation error as a string. It also populates the variables in the error message.

type ValidationErrors

type ValidationErrors []ValidationError

func (ValidationErrors) Error

func (es ValidationErrors) Error() string

Error returns all validation errors as a string.

type Validator

type Validator struct {
	Tags string
	Err  string
	// contains filtered or unexported fields
}

Validator represents a validation rule.

var None Validator = Validator{}

None is an empty validator that does nothing (is skipped). It is useful for custom validators.

func Alpha

func Alpha() Validator

Alpha checks whether the field contains only ASCII alpha characters.

func AlphaNumeric

func AlphaNumeric() Validator

AlphaNumeric checks whether the field contains only alphanumeric characters. Validation fails for integers and floats.

func AlphaNumericHyp

func AlphaNumericHyp() Validator

AlphaNumericDash checks whether the field contains only alphanumeric characters (a-Z0-9) and hyphen (-). Validation fails non string values.

func AlphaNumericHypUS

func AlphaNumericHypUS() Validator

AlphaNumericDash checks whether the field contains only alphanumeric characters (a-Z0-9), hyphen (-) and underscore (_). Validation fails non string values.

func CIDR

func CIDR() Validator

CIDR checks whether the field value is a valid CIDR address.

func CIDRv4

func CIDRv4() Validator

CIDRv4 checks whether the field value is a valid v4 CIDR address.

func CIDRv6

func CIDRv6() Validator

CIDRv6 checks whether the field value is a valid v6 CIDR address.

func Custom

func Custom(key string) Validator

Custom returns custom validator registered with the given key.

func Fail

func Fail() Validator

Fail triggers validation error.

func FileExists

func FileExists() Validator

File checks whether the field is a valid file path and whether file exists.

func IP

func IP() Validator

IP checks whether the field value is a valid IP address.

func IPInRange

func IPInRange(cidr string) Validator

IPInRange checks whether the field value is contained within the specified CIDR.

func IPv4

func IPv4() Validator

IPv4 checks whether the field value is a valid v4 IP address.

func IPv6

func IPv6() Validator

IPv6 checks whether the field value is a valid v6 IP address.

func Len

func Len(value int) Validator

Len checks if the field length matches the specified value.

func Lowercase

func Lowercase() Validator

Lowercase checks whether the field contains only lowercase characters.

func MAC

func MAC() Validator

MAC checks whether the field value is a valid MAC address.

func Max

func Max(value int) Validator

Max checks whether the field value is less than or equal to the specified value. In case of strings, slices, arrays and maps the length is checked.

func MaxLen

func MaxLen(value int) Validator

MaxLen checks whether the field length is less than or equal to the specified value.

func Min

func Min(value int) Validator

Min checks whether the field value is greater than or equal to the specified value. In case of strings, slices, arrays and maps the length is checked.

func MinLen

func MinLen(value int) Validator

MinLen checks whether the field length is greater than or equal to the specified value.

func NotEmpty

func NotEmpty() Validator

NotEmpty validator checks whether value is not blank or with zero length.

func Numeric

func Numeric() Validator

Numeric checks whether the field contains only numeric characters. Validation fails for integers and floats.

func OmitEmpty

func OmitEmpty() Validator

OmitEmpty prevents further validation of the field, if the field is empty.

func OneOf

func OneOf[T any](values ...T) Validator

OneOf checks whether the field value equals one of the specified values. If no value is provided, the validation always fails.

func RegexAll

func RegexAll(regex ...string) Validator

Regex checks whether the field matches all regex expressions.

func RegexAny

func RegexAny(regex ...string) Validator

Regex checks whether the field matches any regex expression.

func Required

func Required() Validator

Required validator verifies that value is provided.

func SemVer

func SemVer() Validator

SemVer checks whether the field is a valid semantic version.

func Skip

func Skip() Validator

Skip prevents further validation of the field.

func Tags

func Tags(tags string) Validator

Tags returns a new validator with the given tags. It is a generic validator that allows use of any validation rule from 'github.com/go-playground/validator' library.

func URL

func URL() Validator

URL checks whether the field is a valid URL.

func Unique

func Unique() Validator

Unique validator checks whether all elements within array, slice or map are unique.

func UniqueField

func UniqueField(field string) Validator

Unique validator checks whether the given field is unique for all elements within a slice of struct.

func Uppercase

func Uppercase() Validator

Uppercase checks whether the field contains only uppercase characters.

func VSemVer

func VSemVer() Validator

VSemVer checks whether the field is a valid semantic version and is prefixed with 'v'.

func (Validator) Error

func (v Validator) Error(err string) Validator

Error overwrites the validator's default error message with the user-defined error and returns the modified validator.

func (Validator) Errorf

func (v Validator) Errorf(err string, opt ...interface{}) Validator

Errorf overwrites the validator's default error message with the formatted user-defined error and returns the modified validator.

func (Validator) ToError

func (v Validator) ToError() ValidationErrors

func (Validator) When

func (v Validator) When(condition bool) Validator

When allows validator to be applied only when the given condition is met.

Jump to

Keyboard shortcuts

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