govalidator

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2024 License: Apache-2.0 Imports: 12 Imported by: 7

README

govalidator (Flexible validator for GoLang)

GoReportCard GoDoc

This library is compatible with Go 1.17+

Please refer to CHANGELOG.md if you encounter breaking changes.

Motivation

The goal of this library is to provide flexible go validator for both regular struct and struct with presence set. Presence set if a special part of go struct holding flags about what struct field has been set. For example to use go struct for patch operation, user may only set subset of the whole struct, thus validator has to have ability to discriminate fields that need to be validated dynamically.

Other great project for validating go struct.

Usage

validator := govalidator.New()
validation, err := validator.Validate(someStruct)
The following check have been implemented
  • required
  • alpha
  • alphanum
  • alphaunicode
  • alphaunicodenumeric
  • numeric
  • number
  • hexadecimal
  • hexcolor
  • rgb
  • rgb
  • hsl
  • hsla
  • e164
  • localphone
  • phone
  • email
  • base64
  • base64url
  • isbn10
  • isbn13
  • uuid3
  • uuid4
  • uuid5
  • uuid
  • uuid3rfc4122
  • uuid4rfc4122
  • uuid5rfc4122
  • uuidrfc4122
  • ulid
  • md4
  • md5
  • sha256
  • sha384
  • sha512
  • ripemd128
  • ripemd160
  • tiger128
  • tiger160
  • tiger192
  • ascii
  • printableascii
  • multibyte
  • datauri
  • latitude
  • longitude
  • ssn
  • hostnamerfc952
  • hostnamerfc1123
  • fqdnrfc1123
  • btcaddress
  • urlencoded
  • htmlencoded
  • html
  • jwt
  • bic
  • dnsregexrfc1035label
  • iabCategory
  • iabCategories
  • domain
  • wwwdomain
  • nonwwwDomain
  • gt(N)
  • ge(N)
  • lt(N)
  • le(N)
  • choice(coma separated list of allowed int or string values)
Additional tag
  • omitempty
  • skipPath - remove path from location
  • presence - presence field

Validation option

  • WithShallow - shallow check
  • WithPresence - check only field set

Contributing to govalidator

govalidator is an open source project and contributors are welcome!

See TODO list

Credits and Acknowledgements

Library Author: Adrian Witas

Documentation

Index

Constants

View Source
const (
	//PathKindRoot defines root path kind
	PathKindRoot = PathKind(iota)
	//PathKinField defines field path kind
	PathKinField
	//PathKindKey defines key path kind
	PathKindKey
	//PathKindIndex defines index path kind
	PathKindIndex
)

Variables

View Source
var SessionKey string

SessionKey represents a session key

Functions

func Alias

func Alias(check string) []string

Alias returns tag aliases

func NewChoice added in v0.1.8

func NewChoice() func(field *Field, check *Check) (IsValid, error)

NewChoice creates choice/enum value checks

func NewGt

func NewGt() func(field *Field, check *Check) (IsValid, error)

NewGt creates greater than validation check

func NewGte

func NewGte() func(field *Field, check *Check) (IsValid, error)

NewGte creates greater or equal than validation check

func NewLt

func NewLt() func(field *Field, check *Check) (IsValid, error)

NewLt creates less than validation check

func NewLte

func NewLte() func(field *Field, check *Check) (IsValid, error)

NewLte creates less or equal than validation check

func NewNotRegExprCheck

func NewNotRegExprCheck(expr *regexp.Regexp) func(field *Field, check *Check) (IsValid, error)

func NewRegExprCheck

func NewRegExprCheck(expr *regexp.Regexp) func(field *Field, check *Check) (IsValid, error)

NewRegExprCheck creates a regexpr based validation check

func NewRegExprCheckWithMinLength

func NewRegExprCheckWithMinLength(expr *regexp.Regexp, minLen int) func(field *Field, check *Check) (IsValid, error)

NewRegExprCheckWithMinLength creates a regexpr based validation check with min Length

func NewRepeatedRegExprCheck

func NewRepeatedRegExprCheck(expr *regexp.Regexp, separator string) func(field *Field, check *Check) (IsValid, error)

func Register

func Register(check string, fn NewIsValid)

Register register tag

func RegisterAlias

func RegisterAlias(check string, checks ...string)

RegisterAlias register tag alias

func SessionContext

func SessionContext(ctx context.Context, session *Session) context.Context

SessionContext creates a context with session

Types

type CanUseMarkerProvider added in v0.3.0

type CanUseMarkerProvider func(v interface{}) bool

type Check

type Check struct {
	Name       string
	Parameters []string
	Message    string
}

Check represents validation check

type Checks

type Checks struct {
	Type         reflect.Type
	Fields       []*FieldCheck
	Slices       []*Field
	Structs      []*Field
	SimpleSlices []*Field
	// contains filtered or unexported fields
}

Checks represents struct checks

func NewChecks

func NewChecks(t reflect.Type) (*Checks, error)

NewChecks returns new checks

type Choice added in v0.1.8

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

type Field

type Field struct {
	*Tag
	*xunsafe.Field
	FieldCheck *FieldCheck
}

type FieldCheck

type FieldCheck struct {
	*Field
	Owner   reflect.Type
	IsValid []IsValid
}

FieldCheckPos represents field checks

type IsValid

type IsValid func(ctx context.Context, value interface{}) (bool, error)

IsValid represents a validation function

type NewIsValid

type NewIsValid func(field *Field, check *Check) (IsValid, error)

NewIsValid function to create IsValid

func Lookup

func Lookup(check string) NewIsValid

Lookup returns tag NewIsValid

func LookupAll

func LookupAll(check string) NewIsValid

LookupAll returns tag NewIsValids

type Numeric

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

type Option

type Option func(c *Options)

func WithCanUseMarkerProvider added in v0.3.0

func WithCanUseMarkerProvider(provider CanUseMarkerProvider) Option

WithCanUseMarkerProvider creates with marker provider option

func WithPath

func WithPath(path *Path) Option

WithPath creates with path option

func WithPreservePointer added in v0.2.3

func WithPreservePointer(flag bool) Option

WithPreservePointer creates with preserve pointer option

func WithSetMarker added in v0.2.1

func WithSetMarker() Option

WithSetMarker creates with marker option

func WithShallow

func WithShallow(flag bool) Option

WithShallow creates with shallow option

type Options

type Options struct {
	UseMarker       bool
	PreservePointer bool

	Shallow              bool
	Path                 *Path
	CanUseMarkerProvider CanUseMarkerProvider
	// contains filtered or unexported fields
}

type Path

type Path struct {
	Kind  PathKind    `json:",omitempty"`
	Path  *Path       `json:",omitempty"`
	Name  string      `json:",omitempty"`
	Index int         `json:",omitempty"`
	Key   interface{} `json:",omitempty"`
}

Path represents an arbitrary data structure path

func NewPath added in v0.3.0

func NewPath() *Path

NewPath creates a new path

func (*Path) Element

func (p *Path) Element(index int) *Path

Element adds slice element node

func (*Path) Entry

func (p *Path) Entry(name string) *Path

Entry adds map entry node

func (*Path) Field

func (p *Path) Field(name string) *Path

Field add fields node

func (*Path) String

func (p *Path) String() string

String stringifies a path

type PathKind

type PathKind int

PathKind defines patch kind

type RegExprCheck

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

func (*RegExprCheck) IsValidString

func (r *RegExprCheck) IsValidString(ctx context.Context, value interface{}) (bool, error)

func (*RegExprCheck) IsValidStringPtr

func (r *RegExprCheck) IsValidStringPtr(ctx context.Context, value interface{}) (bool, error)

type Service

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

Service represents a service

func New

func New() *Service

func (*Service) Validate

func (s *Service) Validate(ctx context.Context, any interface{}, opts ...Option) (*Validation, error)

type Session

type Session struct {
	Path        *Path
	Field       *Field
	ParentValue interface{}
}

Session represents validation session

func (*Session) Set

func (s *Session) Set(path *Path, field *Field, parentValue interface{})

type Tag

type Tag struct {
	Checks    []Check
	Omitempty bool
	Required  bool
	SkipPath  bool
}

Tag represents validation tag

func ParseTag

func ParseTag(tagString string) *Tag

ParseTag parses rule

type Validation

type Validation struct {
	Violations []*Violation
	Failed     bool
}

func (*Validation) AddViolation added in v0.1.3

func (v *Validation) AddViolation(field string, value interface{}, check string, msg string)

func (*Validation) Append

func (v *Validation) Append(path *Path, field string, value interface{}, check string, msg string)

func (*Validation) Error

func (e *Validation) Error() string

func (*Validation) String

func (e *Validation) String() string

type Violation

type Violation struct {
	Location string
	Field    string
	Value    interface{}
	Message  string
	Check    string
}

type Zeroable

type Zeroable interface {
	IsZero() bool
}

Jump to

Keyboard shortcuts

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