rule

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: 9 Imported by: 0

Documentation

Overview

Code generated by genx:code DO NOT EDIT.

Code generated by genx:enum DO NOT EDIT.

Index

Constants

View Source
const (
	TOK_LEADER     rune = '@'
	TOK_OPTIONAL   rune = '?'
	TOK_EQUAL      rune = '='
	TOK_RANGE_IN_L rune = '['
	TOK_RANGE_IN_R rune = ']'
	TOK_RANGE_EX_L rune = '('
	TOK_RANGE_EX_R rune = ')'
	TOK_VALUES_L   rune = '{'
	TOK_VALUES_R   rune = '}'
	TOK_PARAM_L    rune = '<'
	TOK_PARAM_R    rune = '>'
	TOK_REGEXP     rune = '/'
	TOK_SPLITTER   rune = ','
)

Variables

Functions

func IsNil

func IsNil(n Node) bool

func SingleQuote

func SingleQuote(data []byte) []byte

func Slash

func Slash(data []byte) []byte

Types

type Builder

type Builder interface {
	Node

	// Name returns name of rule
	Name() string
	// SetName set rule name
	SetName(string)

	Parameter
	Ranges
	Matrix
	Matcher
	Requirements
}

func CompileAsBuilder

func CompileAsBuilder[T ~[]byte | ~string](data T) (Builder, error)

func NewBuilder

func NewBuilder(name string) Builder

NewBuilder creates validation rule builder

type Error

type Error int8

Error defines error code for validation rule +genx:code

const (
	ERROR_UNDEFINED                      Error = iota
	ERROR__INVALID_RULE_LEADER                 // invalid rule name leader, expect start with '@'
	ERROR__MISSING_RULE_NAME                   // missing rule name
	ERROR__INVALID_PARAM_LEADER                // invalid rule parameter list leader, expect start with '<'
	ERROR__INVALID_PARAM_TAILER                // invalid rule parameter list tailer, expect end with '>'
	ERROR__INVALID_PARAMS                      // invalid rule parameter list content
	ERROR__INVALID_RANGE_LEADER                // invalid rule range leader, expect start with '[' or '('
	ERROR__INVALID_RANGE_TAILER                // invalid rule range tailer, expect end with ']' or ')'
	ERROR__INVALID_RANGE_COUNT                 // invalid rule range count, expect 1 or 2
	ERROR__INVALID_RANGE_MODE                  // invalid rule range mode, while length mode must be quoted with '[]'
	ERROR__INVALID_VALUE_LEADER                // invalid rule value leader, expect start with '{'
	ERROR__INVALID_VALUE_TAILER                // invalid rule value tailer, expect end with '}'
	ERROR__INVALID_REGEXP_LEADER               // invalid rule matcher leader, expect start with '/'
	ERROR__INVALID_REGEXP_TAILER               // invalid rule matcher leader, expect end with '/'
	ERROR__FAILED_COMPILE_REGEXP               // failed to compile regexp
	ERROR__INVALID_QUOTED_DEFAULT_TAILER       // invalid rule quoted default value tailer, expect quoted with `'`
	ERROR__INVALID_LITERAL                     // invalid rule literal
)

func (Error) Message

func (e Error) Message() string

type Literal

type Literal struct {
	NodeType
	// contains filtered or unexported fields
}

func NewLiteral

func NewLiteral(text string) *Literal

func (*Literal) Append

func (l *Literal) Append(b ...byte)

func (*Literal) Bytes

func (l *Literal) Bytes() []byte

func (*Literal) IsNil

func (l *Literal) IsNil() bool

func (*Literal) String

func (l *Literal) String() string

type Matcher

type Matcher interface {
	Node

	// Pattern returns regexp literal
	// eg: @string='s'/\w+/ => string value must match \w+
	Pattern() string
	SetPattern(string) error
	// Regexp returns compiled regexp
	Regexp() *regexp.Regexp
}

func NewMatcher

func NewMatcher() Matcher

type Matrix

type Matrix interface {
	Node

	// ValueMatrix returns fields enumerated values
	// eg: @int{1,2,3} => int value must in {1,2,3}
	ValueMatrix() [][]*Literal
	AppendValues(...[]*Literal)
}

func NewMatrix

func NewMatrix() Matrix

type Node

type Node interface {
	// Type presents rule node type
	Type() NodeType
	// IsNil returns if node is nil
	IsNil() bool
	// Bytes returns formatted node content
	Bytes() []byte
}

type NodeType

type NodeType int8

NodeType rule node type +genx:enum

const (
	NODE_TYPE_UNKNOWN NodeType = iota
	NODE_TYPE__ROOT   NodeType = iota + 0
	NODE_TYPE__PARAMETERS
	NODE_TYPE__RANGE
	NODE_TYPE__ENUMERATIONS
	NODE_TYPE__REGEXP_MATCHER
	NODE_TYPE__REQUIREMENTS

	NODE_TYPE__LITERAL
)

func ParseNodeType

func ParseNodeType(key string) (NodeType, error)

ParseNodeType parse NodeType from key

func (NodeType) IsZero

func (v NodeType) IsZero() bool

IsZero checks if v is zero

func (NodeType) MarshalText

func (v NodeType) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler

func (*NodeType) Scan

func (v *NodeType) Scan(src any) error

Scan implements sql.Scanner

func (NodeType) String

func (v NodeType) String() string

String returns v's string as key

func (NodeType) Text

func (v NodeType) Text() string

Text returns the description as for human reading

func (NodeType) Type

func (t NodeType) Type() NodeType

func (*NodeType) UnmarshalText

func (v *NodeType) UnmarshalText(data []byte) error

UnmarshalText implements encoding.TextUnmarshaler

func (NodeType) Value

func (v NodeType) Value() (driver.Value, error)

Value implements driver.Valuer

func (NodeType) Values

func (NodeType) Values() []NodeType

Values returns enum value list of NodeType

type Parameter

type Parameter interface {
	Node

	// Parameters returns parameter of rule
	// eg:
	//	@map<@string,@int?> => map value with string key and int value
	//	@string<byte>[10] => string value with max 10 byte length
	//	@string<rune>[10] => string value with max 10 rune length
	//	@slice<@float64<10,4>[-1000.0001,1000.0002]> => slice with float64 with width 10 and precision 4 and between -1000.0001 and 1000.0002
	Parameters() []Node
	AddParameters(...Node)
}

func NewParameters

func NewParameters() Parameter

type Ranges

type Ranges interface {
	Node

	Min() (*Literal, bool)
	SetMin(*Literal, bool)
	Max() (*Literal, bool)
	SetMax(*Literal, bool)

	LengthMode() bool
	SetLengthMode(bool)
}

Ranges describes validation value's range eg:

@int32[,10]   => int32 value must greater than or equal to 0 and less than or equal to 10
@int64(,10)   => int64 value must greater than 0 and less or equal than 10
@string[10,)  => string length must greater than or equal to 10
@string[10]   => string length must equal to 10

func NewRanges

func NewRanges() Ranges

type Requirements

type Requirements interface {
	Node

	// Optional returns if field is optional
	// eg: @string? => optional string value
	Optional() bool
	SetOptional(bool)

	// Defaults returns default value of field
	// eg: @string='abc' => string value defaults is "abc" if not assigned
	Defaults() *Literal
	SetDefaults(*Literal)
}

func NewRequirements

func NewRequirements() Requirements

type Rule

type Rule interface {
	Node

	// Name returns name of rule
	Name() string
	// Parameters returns parameter of rule
	// eg:
	//	@map<@string,@int?> => map value with string key and int value
	//	@string<byte>[10] => string value with max 10 byte length
	//	@string<rune>[10] => string value with max 10 rune length
	//	@slice<@float64<10,4>[-1000.0001,1000.0002]> => slice with float64 with width 10 and precision 4 and between -1000.0001 and 1000.0002
	Parameters() []Node
	// Min returns range minimum value and exclusive
	// eg: @int(0,10) => min: 0 exclusive: true
	Min() (*Literal, bool)
	// Max returns range maximum value and exclusive
	// eg: @int(0,10] => max: 10 exclusive: false
	Max() (*Literal, bool)
	// LengthMode return if range use LengthMode
	// eg: @string[10] => length of string must be 10
	LengthMode() bool
	// ValueMatrix returns value's enumeration value matrix
	// eg:
	//	@string{A,B,C} value must be A, B or C
	//	@slice<@int>{1,2,3}{4,5,6} slice value must be []int{1,2,3} or []int{4,5,6}
	ValueMatrix() [][]*Literal
	// Pattern returns regexp literal
	// eg: @string='s'/\w+/ => string value must match \w+
	Pattern() string
	// Regexp returns compiled regexp
	Regexp() *regexp.Regexp
	// Optional returns if field is optional
	// eg: @string? => optional string value
	Optional() bool
	// Defaults returns default value of field
	// eg: @string='abc' => string value defaults is "abc" if not assigned
	Defaults() *Literal
}

func Compile

func Compile[T ~[]byte | ~string](data T) (Rule, error)

Compile compiles validation rule TODO cache compiled rules for acceleration

func MustCompile

func MustCompile[T ~[]byte | ~string](data T) Rule

Jump to

Keyboard shortcuts

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