JsonValidator

package
v1.3.13 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2025 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Alpha3Currency

func Alpha3Currency(context *FieldValidationContext) (string, bool)

Types

type Children added in v1.3.10

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

func (*Children) All added in v1.3.10

func (children *Children) All() []*FieldCache

func (*Children) Append added in v1.3.10

func (children *Children) Append(field ...*FieldCache)

type ErrorBag

type ErrorBag struct {
	Errors map[string][]string
}

func (*ErrorBag) AddError

func (v *ErrorBag) AddError(path string, description string)

func (*ErrorBag) CountErrors

func (v *ErrorBag) CountErrors() int

func (*ErrorBag) Error

func (v *ErrorBag) Error() string

func (*ErrorBag) GetErrorsForKey

func (v *ErrorBag) GetErrorsForKey(key string) []string

func (*ErrorBag) HasFailedKeyAndRule

func (v *ErrorBag) HasFailedKeyAndRule(key string, rule string) bool

HasFailedKeyAndRule Is used for testing. So performance is not critical.

func (*ErrorBag) IsInvalid

func (v *ErrorBag) IsInvalid() bool

func (*ErrorBag) IsValid

func (v *ErrorBag) IsValid() bool

type FieldCache

type FieldCache struct {
	Parent        *FieldCache
	Children      *Children
	Reflection    reflect.Type
	JsonKey       string
	StructKey     string
	ValidationTag *ValidationTag
	IsStruct      bool
	IsSlice       bool
	IsMap         bool
}

func (*FieldCache) GetChildByName

func (fieldCache *FieldCache) GetChildByName(name string) *FieldCache

type FieldValidationContext

type FieldValidationContext struct {
	Validation *ValidationContext
	Params     []string
	RuleName   string
}

func (*FieldValidationContext) GetFloatParam

func (context *FieldValidationContext) GetFloatParam(index int) float64

func (*FieldValidationContext) GetIntParam

func (context *FieldValidationContext) GetIntParam(index int) int

func (*FieldValidationContext) GetParam

func (context *FieldValidationContext) GetParam(index int) string

type JsonContext

type JsonContext struct {
	Path       string // The JSON path to the key under validation
	KeyPresent bool   // True if the key was present in the json
	EmptyValue bool   // True if the value is either null, 0, "", {}, [], or false
	IsNull     bool   // True if the value is null
	Value      any    // The raw json parsed value for the key. Will be nil if KeyPresent=false
}

type JsonTag

type JsonTag struct {
	JsonKey string
}

type Rule

type Rule struct {
	Name           string
	IsPresenceRule bool
	IsNullableRule bool
	Function       RuleFunction
}

type RuleContext

type RuleContext struct {
	Rule
	Params []string
}

func (*RuleContext) GetFloatParam

func (context *RuleContext) GetFloatParam(index int) float64

func (*RuleContext) GetIntParam

func (context *RuleContext) GetIntParam(index int) int

func (*RuleContext) GetStringParam

func (context *RuleContext) GetStringParam(index int) string

type RuleFunction

type RuleFunction func(*FieldValidationContext) (string, bool)

type Rulebook

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

func (Rulebook) GetComposite added in v1.3.9

func (rulebook Rulebook) GetComposite(ruleDefinition string) string

func (Rulebook) GetRule

func (rulebook Rulebook) GetRule(ruleDefinition string) *RuleContext

func (Rulebook) IsComposite added in v1.3.9

func (rulebook Rulebook) IsComposite(ruleDefinition string) bool

func (Rulebook) RegisterAlias

func (rulebook Rulebook) RegisterAlias(alias string, name string) Rulebook

func (Rulebook) RegisterComposite added in v1.3.9

func (rulebook Rulebook) RegisterComposite(name string, rules string) Rulebook

func (Rulebook) RegisterRule

func (rulebook Rulebook) RegisterRule(rule Rule) Rulebook

type StructCache

type StructCache struct {
	Cache map[reflect.Type]*FieldCache
	// contains filtered or unexported fields
}

func (*StructCache) Analyze

func (structCache *StructCache) Analyze(rulebook *Rulebook, targetType reflect.Type) (*FieldCache, error)

type ValidationContext

type ValidationContext struct {
	Json            *JsonContext
	RootContext     *ValidationContext
	ParentContext   *ValidationContext
	Field           *FieldCache
	FieldName       string
	StructFieldName string
	ValidationTag   *ValidationTag
	Validator       *Validator
}

func (*ValidationContext) GetNeighborField

func (context *ValidationContext) GetNeighborField(name string) (*ValidationContext, bool)

func (*ValidationContext) IsRoot

func (context *ValidationContext) IsRoot() bool

type ValidationTag

type ValidationTag struct {
	Rules              []*RuleContext
	PresenceRules      []*RuleContext
	ExplicitlyNullable bool
}

func (*ValidationTag) GetRules

func (tag *ValidationTag) GetRules(name string) []*RuleContext

type Validator

type Validator struct {
	*Rulebook
	// contains filtered or unexported fields
}

func New

func New() *Validator

func (*Validator) Analyze

func (validator *Validator) Analyze(dataTarget any) (*FieldCache, error)

func (*Validator) Validate

func (validator *Validator) Validate(jsonData []byte, dataTarget any) error

Jump to

Keyboard shortcuts

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