val

package
v1.2.5 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2022 License: Apache-2.0 Imports: 7 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// Any is the default type value. This means the Validator will accept any
	// object type and just confirms it exists.
	Any kind = iota
	// None is the opposite of the Any type. This will instruct the validator to
	// ensure the value is null
	// or non-existent.
	None
	// Number represents a type of integer or float value. These are stored as
	// float64 values.
	Number
	// Int represents a type of integer value. Similar to having the Rule 'Integer'.
	Int
	// String represents a string type value.
	String
	// Bool is a type that is either true or false.
	Bool
	// Object is a type that can be used to ensure the result is a complex map or
	// non-list type.
	Object
	// List is a type that will represent a generic list/array/slice input.
	List
	// ListNumber is a type that goes further than List and ensures that all the
	// list entries are valid numbers.
	ListNumber
	// ListString is a type that goes further than List and ensures that all the
	// list entries are valid strings.
	ListString
)
View Source
const (
	// Float adds a number constraint to ensure a number does contain a decimal
	// or a decimal of greater than zero.
	Float = number(false)
	// Integer adds a number constraint to ensure a number does not contain a
	// decimal or a decimal of zero.
	Integer = number(true)

	// Positive adds a number constraint to ensure a number is zero or greater.
	Positive = polarity(true)
	// Negative adds a number constraint to ensure a number is less than zero.
	Negative = polarity(false)

	// GreaterThanZero adds a number constraint to ensure a number greater than
	//zero.
	GreaterThanZero = Min(1)
)

Variables

View Source
var ErrInvalidName = errors.New("invalid name in set")

ErrInvalidName is a validation error returned when a Validator rule has an empty name.

ID is a ruleset that allows for identifying an ID value.

View Source
var (
	// NoEmpty adds a string constraint to ensure a string value cannot be empty.
	NoEmpty = &Length{Min: 1}
)

Functions

This section is empty.

Types

type Length

type Length struct {
	Min, Max uint64
}

Length adds a string or array length constraint to be used. Max value is ignored if empty or less than Min.

func (Length) Validate

func (l Length) Validate(i any) error

Validate fulfills the Rule interface.

type Max

type Max float64

Max is a wrapper that will add a maximum number value constraint.

func (Max) Validate

func (m Max) Validate(i any) error

Validate fulfills the Rule interface.

type Min

type Min float64

Min is a wrapper that will add a minimum number value constraint.

func (Min) Validate

func (m Min) Validate(i any) error

Validate fulfills the Rule interface.

type Rule

type Rule interface {
	Validate(any) error
}

Rule is an interface that can be used to assist with verifying the correct data passed to the Validator.

The 'Validate' function will be passed the object in question and should return nil if it passes.

func Contains

func Contains(s string) Rule

Contains returns a Rule that will verify that the value is a string and contains the supplied string.

func MustRegex

func MustRegex(s string) Rule

MustRegex will return a regular expression validator. This function will panic f the expression has any errors compiling.

func Prefix

func Prefix(s string) Rule

Prefix returns a Rule that will verify that the value is a string and starts with the supplied string.

func Regex

func Regex(s string) (Rule, error)

Regex will return a regular expression validator. This function will return an error if the expression has any errors compiling.

func Suffix

func Suffix(s string) Rule

Suffix returns a Rule that will verify that the value is a string and ends with the supplied string.

type Rules

type Rules []Rule

Rules is an alias for a list of Rules that can be used to validate Content constraints.

type Set

type Set []Validator

Set is an alias for a list of Validators that can be used to validate a request data body.

func (Set) Validate

func (s Set) Validate(c routex.Content) error

Validate will check the rules of this Set against the supplied content object. This function will return nil if the Content is considered valid.

func (Set) ValidateEmpty

func (s Set) ValidateEmpty(c routex.Content) error

ValidateEmpty will check the rules of this Set against the supplied content object.

This function will return nil if the Content is considered valid or if the Content is empty. This function allows for specifying and validating optional data.

type SubSet

type SubSet Set

SubSet is a type if Set that can be used to validate a complex object that is a child of an object being validated.

SubSets have the same options as a Set.

func (SubSet) Validate

func (s SubSet) Validate(i any) error

Validate fulfills the Rule interface.

type Validator

type Validator struct {
	Name     string `json:"name"`
	Rules    Rules  `json:"rules"`
	Type     kind   `json:"type"`
	Optional bool   `json:"optional,omitempty"`
}

Validator is a struct used to assist with data body validation. This struct can be used inside a Set to add rules for incoming data.

The Rules attribute can be used to add more constraints on the Validator.

func (Validator) Validate

func (v Validator) Validate(i any) error

Validate will attempt to validate a single validation rule and return an error if the supplied interface does not match the Validator's constraints.

Jump to

Keyboard shortcuts

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