validator

package module
v0.0.0-...-602bf33 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2021 License: MIT Imports: 21 Imported by: 0

README

go-validator

Go struct validation with code generation

Compatibility table

✔ required ❌ required_if ❌ required_unless ❌ required_with ❌ required_with_all ❌ required_without ❌ required_without_all ❌ excluded_with ❌ excluded_with_all ❌ excluded_without ❌ excluded_without_all ❌ isdefault ❌ len ✔ min ✔ max ❌ eq ❌ ne ❌ lt ❌ lte ❌ gt ❌ gte ❌ eqfield ❌ eqcsfield ❌ necsfield ❌ gtcsfield ❌ gtecsfield ❌ ltcsfield ❌ ltecsfield ❌ nefield ❌ gtefield ❌ gtfield ❌ ltefield ❌ ltfield ❌ fieldcontains ❌ fieldexcludes ❌ alpha ❌ alphanum ❌ alphaunicode ❌ alphanumunicode ❌ numeric ❌ number ❌ hexadecimal ❌ hexcolor ❌ rgb ❌ rgba ❌ hsl ❌ hsla ❌ e164 ✔ email ❌ url ❌ uri ❌ urn_rfc2141 ❌ file ❌ base64 ❌ base64url ❌ contains ❌ containsany ❌ containsrune ❌ excludes ❌ excludesall ❌ excludesrune ❌ startswith ❌ endswith ❌ startsnotwith ❌ endsnotwith ❌ isbn ❌ isbn10 ❌ isbn13 ❌ eth_addr ❌ btc_addr ❌ btc_addr_bech32 ❌ uuid ❌ uuid3 ❌ uuid4 ❌ uuid5 ❌ uuid_rfc4122 ❌ uuid3_rfc4122 ❌ uuid4_rfc4122 ❌ uuid5_rfc4122 ❌ ascii ❌ printascii ❌ multibyte ❌ datauri ❌ latitude ❌ longitude ❌ ssn ❌ ipv4 ❌ ipv6 ❌ ip ❌ cidrv4 ❌ cidrv6 ❌ cidr ❌ tcp4_addr ❌ tcp6_addr ❌ tcp_addr ❌ udp4_addr ❌ udp6_addr ❌ udp_addr ❌ ip4_addr ❌ ip6_addr ❌ ip_addr ❌ unix_addr ❌ mac ❌ hostname ❌ hostname_rfc1123 ❌ fqdn ❌ unique ❌ oneof ❌ html ❌ html_encoded ❌ url_encoded ❌ dir ❌ json ❌ hostname_port ❌ lowercase ❌ uppercase ❌ datetime ❌ timezone ❌ iso3166_1_alpha2 ❌ iso3166_1_alpha3 ❌ iso3166_1_alpha_numeric

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrRequired      = errors.New("field required")
	ErrIsDefault     = errors.New("field must have default value")
	ErrInvalidFormat = errors.New("invalid format")
	ErrEmail         = errors.Wrap(ErrInvalidFormat, "invalid email")
	ErrMin           = errors.New("min validation not satisfied")
	ErrMax           = errors.New("max validation not satisfied")
)
View Source
var (
	AlphaRegex                 = regexp.MustCompile(alphaRegexString)
	AlphaNumericRegex          = regexp.MustCompile(alphaNumericRegexString)
	AlphaUnicodeRegex          = regexp.MustCompile(alphaUnicodeRegexString)
	AlphaUnicodeNumericRegex   = regexp.MustCompile(alphaUnicodeNumericRegexString)
	NumericRegex               = regexp.MustCompile(numericRegexString)
	NumberRegex                = regexp.MustCompile(numberRegexString)
	HexadecimalRegex           = regexp.MustCompile(hexadecimalRegexString)
	HexcolorRegex              = regexp.MustCompile(hexcolorRegexString)
	RgbRegex                   = regexp.MustCompile(rgbRegexString)
	RgbaRegex                  = regexp.MustCompile(rgbaRegexString)
	HslRegex                   = regexp.MustCompile(hslRegexString)
	HslaRegex                  = regexp.MustCompile(hslaRegexString)
	E164Regex                  = regexp.MustCompile(e164RegexString)
	EmailRegex                 = regexp.MustCompile(emailRegexString)
	Base64Regex                = regexp.MustCompile(base64RegexString)
	Base64URLRegex             = regexp.MustCompile(base64URLRegexString)
	ISBN10Regex                = regexp.MustCompile(iSBN10RegexString)
	ISBN13Regex                = regexp.MustCompile(iSBN13RegexString)
	UUID3Regex                 = regexp.MustCompile(uUID3RegexString)
	UUID4Regex                 = regexp.MustCompile(uUID4RegexString)
	UUID5Regex                 = regexp.MustCompile(uUID5RegexString)
	UUIDRegex                  = regexp.MustCompile(uUIDRegexString)
	UUID3RFC4122Regex          = regexp.MustCompile(uUID3RFC4122RegexString)
	UUID4RFC4122Regex          = regexp.MustCompile(uUID4RFC4122RegexString)
	UUID5RFC4122Regex          = regexp.MustCompile(uUID5RFC4122RegexString)
	UUIDRFC4122Regex           = regexp.MustCompile(uUIDRFC4122RegexString)
	ASCIIRegex                 = regexp.MustCompile(aSCIIRegexString)
	PrintableASCIIRegex        = regexp.MustCompile(printableASCIIRegexString)
	MultibyteRegex             = regexp.MustCompile(multibyteRegexString)
	DataURIRegex               = regexp.MustCompile(dataURIRegexString)
	LatitudeRegex              = regexp.MustCompile(latitudeRegexString)
	LongitudeRegex             = regexp.MustCompile(longitudeRegexString)
	SSNRegex                   = regexp.MustCompile(sSNRegexString)
	HostnameRegexRFC952        = regexp.MustCompile(hostnameRegexStringRFC952)
	HostnameRegexRFC1123       = regexp.MustCompile(hostnameRegexStringRFC1123)
	FqdnRegexRFC1123           = regexp.MustCompile(fqdnRegexStringRFC1123)
	BtcAddressRegex            = regexp.MustCompile(btcAddressRegexString)
	BtcUpperAddressRegexBech32 = regexp.MustCompile(btcAddressUpperRegexStringBech32)
	BtcLowerAddressRegexBech32 = regexp.MustCompile(btcAddressLowerRegexStringBech32)
	EthAddressRegex            = regexp.MustCompile(ethAddressRegexString)
	EthaddressRegexUpper       = regexp.MustCompile(ethAddressUpperRegexString)
	EthAddressRegexLower       = regexp.MustCompile(ethAddressLowerRegexString)
	URLEncodedRegex            = regexp.MustCompile(uRLEncodedRegexString)
	HTMLEncodedRegex           = regexp.MustCompile(hTMLEncodedRegexString)
	HTMLRegex                  = regexp.MustCompile(hTMLRegexString)
)
View Source
var (
	ErrFunctionAlreadyRegistered = errors.New("function already registered")
)

Functions

func MustRegisterValidation

func MustRegisterValidation(name string, r *ValidationRegistration)

func RegisterValidation

func RegisterValidation(name string, r *ValidationRegistration) error

Types

type CustomValidation

type CustomValidation interface {
	CustomValidate() error
}

CustomValidation is used to implement custom validations that are not directly supported by the library.

The generate `Validate` method will call the `CustomValidation` automatically, if the model implements it.

type FieldContext

type FieldContext struct {
	Tag    string
	Params []string
	Value  interface{}
	Source interface{}
}

FieldContext is the information that is passed to the validation function `Func`.

type FieldData

type FieldData struct {
	// Field       *myasthurts.Field
	Name        string
	Identifier  string
	IsPointer   bool
	IsArray     bool
	IsNumeric   bool
	IsBoolean   bool
	IsString    bool
	Validations []*ValidationPair
}

func NewFieldData

func NewFieldData(field *myasthurts.Field) *FieldData

type FieldError

type FieldError struct {
	Field string
	Rule  string
	Value interface{}
	// contains filtered or unexported fields
}

func NewFieldError

func NewFieldError(err error, rule, field string, value interface{}) *FieldError

func (*FieldError) Error

func (err *FieldError) Error() string

func (*FieldError) Unwrap

func (err *FieldError) Unwrap() error

type Func

type Func func(fieldContext FieldContext) bool

Func accepts a FieldContext struct for all validation needs. The return value should be true when validation succeeds.

type FuncCtx

type FuncCtx func(ctx context.Context, fieldContext FieldContext) bool

FuncCtx accepts a context.Context and FieldContext interface for all validation needs. The return value should be true when validation succeeds.

type ImportData

type ImportData struct {
	Name string
	Path string
}

type StructData

type StructData struct {
	Struct *myasthurts.Struct
	Fields []*FieldData
}

type Validable

type Validable interface {
	// Validate validates the struct
	Validate() error
}

Validable abstracts the behavior of a struct that can be validated.

type ValidateValidationField

type ValidateValidationField = func(s *StructData, field *FieldData, validation *ValidationData) error

type ValidationData

type ValidationData struct {
	Params []string
}

type ValidationErrors

type ValidationErrors []*FieldError

func (ValidationErrors) Error

func (err ValidationErrors) Error() string

type ValidationGenerationFunc

type ValidationGenerationFunc = func(w *quicktemplate.Writer, s *StructData, field *FieldData, validation *ValidationData)

type ValidationPair

type ValidationPair struct {
	Validation *ValidationRegistration
	Data       *ValidationData
}

type ValidationRegistration

type ValidationRegistration struct {
	Func          ValidationGenerationFunc
	Imports       []*ImportData
	ValidateField ValidateValidationField
}

Directories

Path Synopsis
cli
validator-gen command
Code generated by validator-gen.
Code generated by validator-gen.

Jump to

Keyboard shortcuts

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