validators

package module
v0.0.0-...-ec19fbf Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2023 License: MIT Imports: 3 Imported by: 0

README

Go Reference

validate

validate provides convenience functions to create CustomTypeValidator instances for use within the govalidator CustomTypeTagMap.

These can be used together with govalidator to ensure JSON objects are valid.

{
	"statement": "Hello World"
}

Use

The "valid" tag on a struct attribute defines the list of custom validators (or govalidator defined validators) which are then applied sequentially to the attribute value.

type Message struct {
	Statement string `json:"statement" valid:"saysHelloWorld"`
}

func init() {
	govalidator.CustomTypeTagMap.Set("saysHelloWorld", NewEquals("Hello World"))
}

func UnmarshalMessage(b []byte)  (*Message, error) {
	var m = &Message{}
	_ = json.Unmarshal(b, m)
	_, err := govalidator.ValidateStruct(m)
	if err != nil {
		return nil, err
	}
	return m
}

How?

The command line is all you need.

go get github.com/gford1000-go/validate

Documentation

Index

Constants

View Source
const (
	ExhaustiveIn    existenceCheckType = iota // All items are found in the comparison slice
	ExhaustiveNotIn                           // None of the items are in the comparison slice
)

Variables

This section is empty.

Functions

func NewConditionalCheck

func NewConditionalCheck(clauses []ConditionClause) func(i interface{}, context interface{}) bool

NewConditionalCheck creates a custom type validator which applies an arbitray number of ConditionClause checks, each with an arbitrary number of validators. The context object must support the Getter interface to apply the ConditionClause validations ... if it doesn't then the custom type validator returns false.

The validators within the ConditionClause slices must have been pre-registered to govalidator CustomTypeTagMap; if not found the custom type validator will panic.

func NewEquals

func NewEquals[T comparable](item T) func(i interface{}, context interface{}) bool

NewEquals returns a function instance that will test equality of an interface against the specified item. The returned function can be used as a named func in the govalidators.CustomTypeTagMap for use in struct attribute value validations

func NewIsIn

func NewIsIn[T comparable](items []T) func(i interface{}, context interface{}) bool

NewIsIn returns a function instance that will test existence of an interface against the specified items. The function will return true if the interface is a value and is present, or when the interface is a slice and all the values in the slice are present in items. The returned function can be used as a named func in the govalidators.CustomTypeTagMap for use in struct attribute value validations

func NewIsNotIn

func NewIsNotIn[T comparable](items []T) func(i interface{}, context interface{}) bool

NewIsNotIn returns a function instance that will test non-existence of an interface against the specified items. The function will return true if the interface is a value and is not present, or when the interface is a slice and none of the values in the slice are present in items. The returned function can be used as a named func in the govalidators.CustomTypeTagMap for use in struct attribute value validations

func NewIsTypeOf

func NewIsTypeOf(exemplar any) func(i interface{}, context interface{}) bool

NewIsTypeOf returns a function that returns true only if the interface type, determined by reflect.TypeOf(), is the same as the supplied exemplar. nil values always return false. The returned function can be used as a named func in the govalidators.CustomTypeTagMap for use in struct attribute value validations

func NewNotEquals

func NewNotEquals[T comparable](item T) func(i interface{}, context interface{}) bool

NewNotEquals returns a function instance that will test non-equality of an interface against the specified item. The returned function can be used as a named func in the govalidators.CustomTypeTagMap for use in struct attribute value validations

Types

type ConditionClause

type ConditionClause struct {
	ContextAttrName string              // The name of the attribute in the context, whose value to retrieve
	ValueConditions map[string][]string // The set of validations to apply based on the attribute's value
}

ConditionClause defines which custom validators should be executed, based on the value of the specified attribute within the current context.

type Getter

type Getter interface {
	GetAsString(attrName string) (string, error)
}

Getter provides a mechanism to access the value of particular attributes, by name

Jump to

Keyboard shortcuts

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