validators

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package validators defines field validation

For basic types, such as integer, float, string, boolean, regexp For compose types with basic type elements, map, slice

refs: JavaScript safe number https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MIN_SAFE_INTEGER IEEE 754 https://en.wikipedia.org/wiki/IEEE_754

Usage:

@RULE_NAME<RULE_PARAMETER_LIST>[RULE_VALUE_RANGE]{RULE_ENUMERATION_LIST}RULE_REQUIREMENTS

1. RULE_NAME: literal composed by ASCII, '-', '_'. MUST be prefixed with '@'
2. RULE_PARAMETERS: MUST is a rule or literal, use ',' to seperate parameters.
	eg:
		@float<22,4>       => float value with max 22 digitals and 4 decimal part.
		@slice<@int>       => int slice
		@map<@string,@int> => map with string key and int value
3. RULE_VALUE_RANGE: use '[',']','(',')'.
	eg:
		[,10]    => GET type minimum and LET 10
		[10,15]  => GET 10 and LET 15
		(,10)    => GT type minimum and LT 10
		(10,15)  => GT 10 and LT 15
		[10]     => elements MUST equal 10
4. RULE_ENUMERATION_LIST: use '{}' quoted and ',' spliter. basic type only, composed type(slice,map) is not supported.
	eg:
		@int{1,2,3}         => int value must in 1,2,3
		@string{abc,def,gh} => string value must in abc,def,gh
		@slice<@int>{1,2,3} => unsupported
5. RULE_REQUIREMENTS: suffix with '?' means value is optional, use '=' means value has default
	eg:
		@int        => int value and MUST not be 0
		@string     => string value and MUST not be empty
		@int?       => optional int value
		@string?    => optional string value
		@int=1      => optional int value with 1 as its default
		@float=0.1  => optional float value with 0.1 as its default
		@string=ABC => optional string value with 'ABC' as its default

Int

@int                    => value MUST between minimum int32 and maximum int32
@int<8>                 => value MUST be with max 8 bits
@int<53>                => value MUST be with max 53 bits(js safe integer)
@int[1,10)              => value MUST be GET 1 and LT 10
@int[1,)                => value MUST be GET 1 and LT the maximum of int32
@int(,1)                => value MUST be LT 1 and GT minimum of int32
@int{1,2,3}             => value MUST be one of {1,2,3}
@int{%2}                => value MUST be multiple of 2. multiple mode MUST contain only 1 parameter.
@int<8>                 => be equivalent of @int8
@int<16>                => be equivalent of @int16
@int<32>                => be equivalent of @int32
@int<64>                => be equivalent of @int64
@uint<64>               => be equivalent of @uint64

Float

@float[1,10)            => value MUST be larger than or equal to 1 and less than 10
@float<5,2>             => value width MUST be LET 5 and precision(scale) MUST be LET 2. eg: 11.123 -> too many fractional 1234.56 too many digits
@float32                => be equivalent of @float
@float64                => be equivalent of @double

String

@string/\w+/            => value MUST match \w+. regexp start and end with '/'
@string[1,5)            => value byte length MUST GET 1 and LT 5
@string<byte>[1,5]      => be equivalent of @string[1,5]
@string<rune>[1,5]      => value rune length MUST GET 1 and LET 5
@string{ab,cd,ef}       => value MUST be one of "ab", "de" and "ef"
@string(100)            => failed to parse, length mode must be quoted with '[]'

Slice

@slice<@float32>         => slice MUST be with float32 elements
@slice<@int8{1,2,3}>[5]  => slice MUST be with int8 elements and length MUST be 5, and each element MUST be one of {1,2,3}

Map

	@map<@string{A,B,C},@int>[,100]
                          => map MUST be with string key and key MUST be one
                             of A,B,C and MUST be with int value
                             and map length MUST between 0 and 100

Index

Constants

View Source
const (
	BYTE_MODE = "byte"
	RUNE_MODE = "rune"
)

Variables

View Source
var LenFunc = map[string]func(string) uint{
	BYTE_MODE: func(s string) uint { return uint(len(s)) },
	RUNE_MODE: func(s string) uint { return uint(utf8.RuneCount([]byte(s))) },
}

Functions

func NewRegexpProvider

func NewRegexpProvider(exp, name, hint string) validation.Provider

func NewUserDefinedProvider

func NewUserDefinedProvider(name string, fn func(unquoted string) error) validation.Provider

Types

type Boolean

type Boolean struct{}

func (*Boolean) String

func (v *Boolean) String() string

func (*Boolean) Validate

func (v *Boolean) Validate(value []byte) error

type Float

type Float struct {
	BitSize uint

	*va.FloatScaleVa
	*va.EnumVa[float64]
	*va.RangeVa[float64]
	*va.MultipleVa[float64]
}

func (*Float) String

func (v *Float) String() string

func (*Float) Validate

func (v *Float) Validate(value []byte) error

type Int

type Int[T int64 | uint64] struct {
	*va.IntScaleVa
	*va.EnumVa[T]
	*va.RangeVa[T]
	*va.MultipleVa[T]
}

func (*Int[T]) String

func (v *Int[T]) String() string

func (*Int[T]) Validate

func (v *Int[T]) Validate(value []byte) error

type Map

type Map struct {
	*va.LengthVa
	// contains filtered or unexported fields
}

func (*Map) ElemRule

func (v *Map) ElemRule() rule.Rule

func (*Map) KeyRule

func (v *Map) KeyRule() rule.Rule

func (*Map) PostValidate

func (v *Map) PostValidate(rv reflect.Value) error

func (*Map) SetElemRule

func (v *Map) SetElemRule(r rule.Rule)

func (*Map) SetKeyRule

func (v *Map) SetKeyRule(r rule.Rule)

func (*Map) String

func (v *Map) String() string

func (*Map) Validate

func (v *Map) Validate(_ []byte) error

type Slice

type Slice struct {
	*va.LengthVa
	// contains filtered or unexported fields
}

func (*Slice) ElemRule

func (v *Slice) ElemRule() rule.Rule

func (*Slice) PostValidate

func (v *Slice) PostValidate(rv reflect.Value) error

func (*Slice) String

func (v *Slice) String() string

func (*Slice) Validate

func (v *Slice) Validate(value []byte) error

type String

type String struct {
	*va.LengthVa
	*va.RegexpVa
	*va.EnumVa[string]
	// contains filtered or unexported fields
}

func (*String) Format

func (v *String) Format() string

func (*String) Mode

func (v *String) Mode() string

func (*String) SetFormat

func (v *String) SetFormat(f string)

func (*String) SetMode

func (v *String) SetMode(m string)

func (*String) String

func (v *String) String() string

func (*String) Validate

func (v *String) Validate(value []byte) error

type UserDefined

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

func (*UserDefined) Format

func (v *UserDefined) Format() string

func (*UserDefined) String

func (v *UserDefined) String() string

func (*UserDefined) Validate

func (v *UserDefined) Validate(value []byte) error

Jump to

Keyboard shortcuts

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