Documentation
¶
Index ¶
- Constants
- Variables
- func OverflowError[T any](v T) error
- func ParseStringTo[T FieldType](s string) (T, error)
- type Field
- type FieldType
- type Number
- type PersistentField
- type ValidateFunc
- func BeFalse() ValidateFunc[bool]
- func BeTrue() ValidateFunc[bool]
- func Between[T Number | time.Time](min, max T) ValidateFunc[T]
- func CharSetAll(charSets ...charSet) ValidateFunc[string]
- func CharSetAny(charSets ...charSet) ValidateFunc[string]
- func CharSetNo(charSets ...charSet) ValidateFunc[string]
- func CharSetOnly(charSets ...charSet) ValidateFunc[string]
- func Email() ValidateFunc[string]
- func ExactLength(length int) ValidateFunc[string]
- func Gt[T Number | time.Time](min T) ValidateFunc[T]
- func Gte[T Number | time.Time](min T) ValidateFunc[T]
- func LengthBetween(min, max int) ValidateFunc[string]
- func Lt[T Number | time.Time](max T) ValidateFunc[T]
- func Lte[T Number | time.Time](max T) ValidateFunc[T]
- func Match(pattern string) ValidateFunc[string]
- func MaxLength(max int) ValidateFunc[string]
- func MinLength(min int) ValidateFunc[string]
- func OneOf[T FieldType](allowed ...T) ValidateFunc[T]
- func URL() ValidateFunc[string]
- type Validator
Constants ¶
const ( LowerCaseChar charSet = iota UpperCaseChar NumberChar SpecialChar )
Variables ¶
var ( LowerCaseCharSet = string(lo.LowerCaseLettersCharset) UpperCaseCharSet = string(lo.UpperCaseLettersCharset) NumberCharSet = string(lo.NumbersCharset) SpecialCharSet = string(lo.SpecialCharset) )
var ( ErrIntegerOverflow = errors.New("integer overflow") ErrTypeMismatch = errors.New("type mismatch") ErrRequired = errors.New("is required but not found") ErrLengthMin = errors.New("length must be at least") ErrLengthMax = errors.New("length must be at most") ErrLengthExact = errors.New("length must be exactly") ErrLengthBetween = errors.New("length must be between") ErrCharSetOnly = errors.New("can only contain characters from") ErrCharSetAny = errors.New("must contain at least one character from") ErrCharSetAll = errors.New("not contains chars from") ErrCharSetNo = errors.New("must not contain any characters from") ErrNotMatch = errors.New("not match pattern") ErrNotValidEmail = errors.New("not valid email address") ErrNotValidURL = errors.New("not valid url") ErrNotOneOf = errors.New("value must be one of") ErrMustGt = errors.New("must be greater than") ErrMustGte = errors.New("must be greater than or equal to") ErrMustLt = errors.New("must be less than") ErrMustLte = errors.New("must be less than or equal to") ErrMustBetween = errors.New("must be between") ErrMustBeTrue = errors.New("must be true") ErrMustBeFalse = errors.New("must be false") )
var DefaultTimeLayouts = []string{time.RFC3339Nano, time.RFC3339, "2006-01-02T15:04:05", "2006-01-02"}
DefaultTimeLayouts are the default layouts used to parse time strings.
Functions ¶
func OverflowError ¶
OverflowError returns a standard overflow error wrapping ErrIntegerOverflow.
func ParseStringTo ¶
ParseStringTo converts a string into the specified FieldType T. This is shared by view/value layers when converting URL params into typed values.
Types ¶
type Field ¶
type Field interface {
// Name returns the provider identifier used by generated code and by
// view validation. It should be unique inside a Schema.
Name() string
// QualifiedName returns a DB-qualified column reference in the form
// "table.column". Consumers (SQL builders) rely on this format.
QualifiedName() string
// ViewName returns the JSON/view key used when building or validating
// ValueObjects. It may differ from Name() when a separate JSON key is
// desired for presentation.
ViewName() string
// contains filtered or unexported methods
}
Field is a sealed interface describing a single field's metadata.
Implementations provide three read-only accessors:
- Name(): the canonical provider name (usually the exported Go field name)
- Qualified(): the DB-qualified column name in the form "table.column"
- ViewName(): the JSON/view facing name (the key used in validated objects)
The unexported seal() method prevents external packages from implementing Field; only code inside this module (and generator-produced code that lives in the same module) may implement Field.
type PersistentField ¶
PersistentField is a generic alias indicating this Field carries a Go type parameter used for validators or type hints. It currently does not add methods beyond Field but documents the intended semantic role.
func NewField ¶
func NewField[E entity.Entity, T FieldType](name string, column string, view string, vfs ...ValidateFunc[T]) PersistentField[T]
var f = NewField[Account, int64]("ID", "id", "id")
type ValidateFunc ¶
func Between ¶
func Between[T Number | time.Time](min, max T) ValidateFunc[T]
Between validates that a value is within a given range (inclusive of min and max).
func CharSetAll ¶
func CharSetAll(charSets ...charSet) ValidateFunc[string]
CharSetAll validates that a string contains at least one character from each of the specified character sets.
func CharSetAny ¶
func CharSetAny(charSets ...charSet) ValidateFunc[string]
CharSetAny validates that a string contains at least one character from any of the specified character sets.
func CharSetNo ¶
func CharSetNo(charSets ...charSet) ValidateFunc[string]
CharSetNo validates that a string does not contain any characters from the specified character sets.
func CharSetOnly ¶
func CharSetOnly(charSets ...charSet) ValidateFunc[string]
CharSetOnly validates that a string only contains characters from the specified character sets.
func Email ¶
func Email() ValidateFunc[string]
Email validates that a string is a valid email address.
func ExactLength ¶
func ExactLength(length int) ValidateFunc[string]
ExactLength validates that a string's length is exactly the specified length.
func Gt ¶
func Gt[T Number | time.Time](min T) ValidateFunc[T]
Gt validates that a value is greater than the specified minimum.
func Gte ¶
func Gte[T Number | time.Time](min T) ValidateFunc[T]
Gte validates that a value is greater than or equal to the specified minimum.
func LengthBetween ¶
func LengthBetween(min, max int) ValidateFunc[string]
LengthBetween validates that a string's length is within a given range (inclusive).
func Lt ¶
func Lt[T Number | time.Time](max T) ValidateFunc[T]
Lt validates that a value is less than the specified maximum.
func Lte ¶
func Lte[T Number | time.Time](max T) ValidateFunc[T]
Lte validates that a value is less than or equal to the specified maximum.
func Match ¶
func Match(pattern string) ValidateFunc[string]
Match validates that a string matches a given pattern. The pattern can include wildcards:
- `*`: matches any sequence of non-separator characters.
- `?`: matches any single non-separator character.
Example: Match("foo*") will match "foobar", "foo", etc.
func MaxLength ¶
func MaxLength(max int) ValidateFunc[string]
MaxLength validates that a string's length is at most the specified maximum.
func MinLength ¶
func MinLength(min int) ValidateFunc[string]
MinLength validates that a string's length is at least the specified minimum.
func OneOf ¶
func OneOf[T FieldType](allowed ...T) ValidateFunc[T]
OneOf validates that a value is one of the allowed values. This works for any comparable type in FieldType (string, bool, all numbers).