validator

package
v0.0.0-...-d36796e Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Validator is a package to provide lots of useful validators to validate API request data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal[T comparable](v, target T)

Equal asserts that v must equal to target. Otherwise, it throws an error.

func In

func In[T any](v T, rangers ...Ranger[T])

In asserts that v must be in all of the rangers. Otherwise, it throws an error.

func InAny

func InAny[T any](v T, rangers ...Ranger[T])

InAny asserts that v must be in one of the rangers. Otherwise, it throws an error.

func NotEqual

func NotEqual[T comparable](v, target T)

NotEqual asserts that v must not equal to target. Otherwise, it throws an error.

func Validate

func Validate(ctx context.Context, data any) (err error)

Validate validates data and its struct fields in depth-first order.

Types

type Comparable

type Comparable[T any] interface {
	// Compare compares v with the data.
	//
	//     - Returns > 0 if the data is greater than v;
	//     - Returns < 0 if it is less than v;
	//     - Returns 0 if it is equal to v.
	Compare(v T) int
}

Comparable represents a data which can be compared with another data.

type Range

type Range[T any] struct {
	Min, Max Comparable[T]

	MinInclusive bool // Whether the minimum value is inclusive.
	MaxInclusive bool // Whether the maximum value is inclusive.
}

Range represents a range of values.

func NewRange

func NewRange[T any](min, max Comparable[T]) *Range[T]

NewRange returns a new half-open-interval Range.

func (*Range[T]) InRange

func (r *Range[T]) InRange(v T) bool

InRange returns true if v is in the range.

type Ranger

type Ranger[T any] interface {
	InRange(v T) bool
}

Ranger represents a data which can be checked if it is in a range.

func And

func And[T any](rangers ...Ranger[T]) Ranger[T]

And returns a new Ranger which returns true if all of the rangers return true.

func NewEnum

func NewEnum[T comparable](values ...T) Ranger[T]

NewEnum returns a new Ranger which returns true if a value is one of the values.

func Not

func Not[T any](ranger Ranger[T]) Ranger[T]

Not returns a new Ranger which inverts the result of the ranger.

func Or

func Or[T any](rangers ...Ranger[T]) Ranger[T]

Or returns a new Ranger which returns true if any one of the rangers returns true.

type Validator

type Validator interface {
	Validate(ctx context.Context)
}

Validator represents a data which can work with Shana validator.

Directories

Path Synopsis
Package alphabet provides a set of string validators.
Package alphabet provides a set of string validators.

Jump to

Keyboard shortcuts

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