Documentation
¶
Overview ¶
Package validate provides a fluent, type-safe, and AST-based validation library for Go.
It separates schema definition from domain models, supports distinguishing between "null" and "missing" fields, and offers a rich set of composable validation rules.
Index ¶
- Constants
- func AnyOf[T any](schemaList ...combinator.Schema[T]) *combinator.AnyOfSchema[T]
- func Array[E any]() *array.Schema[E]
- func Boolean() *boolean.Schema
- func Date() *date.Schema
- func Duration() *duration.Schema
- func Number[N types.Number]() *number.Schema[N]
- func Object[T any](builder func(target *T, schemaValue *object.Schema[T])) *object.Schema[T]
- func OneOf[T any](schemaList ...combinator.Schema[T]) *combinator.OneOfSchema[T]
- func Record[V any]() *record.Schema[V]
- func Register(schemaValue AnySchema)
- func ResetRegistry()
- func Text() *text.Schema
- func Validate[T any](input any, optionList ...Option) (T, error)
- type AnyOfSchema
- type AnySchema
- type ArraySchema
- type BooleanFieldBuilder
- type BooleanSchema
- type CombinatorSchema
- type ConditionOp
- type Context
- type DateFieldBuilder
- type DateSchema
- type DurationFieldBuilder
- type DurationSchema
- type FieldBuilder
- type Formatter
- type Issue
- type NumberFieldBuilder
- type NumberSchema
- type ObjectSchema
- type OneOfSchema
- type Option
- func WithAdditionalDateLayouts(layouts ...string) Option
- func WithCoerce(value bool) Option
- func WithCoerceDateUnixMilliseconds(value bool) Option
- func WithCoerceDateUnixSeconds(value bool) Option
- func WithCoerceDurationMilliseconds(value bool) Option
- func WithCoerceDurationSeconds(value bool) Option
- func WithCoerceNumberUnderscore(value bool) Option
- func WithCoerceTrimSpace(value bool) Option
- func WithDateLayouts(layouts ...string) Option
- func WithDefaultOnNull(value bool) Option
- func WithFailFast(value bool) Option
- func WithFormatter(formatter Formatter) Option
- func WithMaxIssues(value int) Option
- func WithOmitZero(value bool) Option
- func WithTimeLocation(value *time.Location) Option
- type Options
- type RecordSchema
- type Reporter
- type Rule
- type RuleFn
- type TextFieldBuilder
- type TextSchema
- type ValidationError
- type Value
Constants ¶
const CodeOneOf = combinator.CodeOneOf
Variables ¶
This section is empty.
Functions ¶
func AnyOf ¶
func AnyOf[T any](schemaList ...combinator.Schema[T]) *combinator.AnyOfSchema[T]
AnyOf creates a combinator schema that succeeds if at least one of the provided schemas succeeds.
func Number ¶
Number creates a new NumberSchema[N] for numeric validation. N can be any integer or float type.
func Object ¶
Object creates a new ObjectSchema for type T. The builder function is used to define fields and semantic rules on the schema.
func OneOf ¶
func OneOf[T any](schemaList ...combinator.Schema[T]) *combinator.OneOfSchema[T]
OneOf creates a combinator schema that succeeds if exactly one of the provided schemas succeeds.
func Register ¶
func Register(schemaValue AnySchema)
Register registers a schema in the global registry for its output type.
func ResetRegistry ¶
func ResetRegistry()
ResetRegistry clears all registered schemas. Useful for testing.
Types ¶
type AnyOfSchema ¶
type AnyOfSchema[T any] = combinator.AnyOfSchema[T]
AnyOfSchema succeeds if at least one schema succeeds.
type BooleanFieldBuilder ¶
type BooleanFieldBuilder[T any] = object.BooleanFieldBuilder[T]
BooleanFieldBuilder is the entry point for defining rules on boolean fields.
type CombinatorSchema ¶
type CombinatorSchema[T any] = combinator.Schema[T]
CombinatorSchema combines multiple schemas.
type ConditionOp ¶
type ConditionOp = object.ConditionOp
ConditionOp is a type that represents a condition operator used in conditional validation.
const ( Eq ConditionOp = object.Eq Ne ConditionOp = object.Ne Present ConditionOp = object.Present Missing ConditionOp = object.Missing Null ConditionOp = object.Null NotNull ConditionOp = object.NotNull )
type Context ¶
Context holds the state of the current validation, including the path and issues found.
type DateFieldBuilder ¶
type DateFieldBuilder[T any] = object.DateFieldBuilder[T]
DateFieldBuilder is the entry point for defining rules on date fields.
type DurationFieldBuilder ¶
type DurationFieldBuilder[T any] = object.DurationFieldBuilder[T]
DurationFieldBuilder is the entry point for defining rules on duration fields.
type DurationSchema ¶
DurationSchema validates time.Duration values.
type FieldBuilder ¶
type FieldBuilder[T any] = object.FieldBuilder[T]
FieldBuilder is the entry point for defining field rules.
type NumberFieldBuilder ¶
type NumberFieldBuilder[T any] = object.NumberFieldBuilder[T]
NumberFieldBuilder is the entry point for defining rules on numeric fields.
type NumberSchema ¶
NumberSchema validates numeric values (int, float, etc).
type ObjectSchema ¶
ObjectSchema validates structs or maps against defined fields.
type OneOfSchema ¶
type OneOfSchema[T any] = combinator.OneOfSchema[T]
OneOfSchema succeeds if exactly one schema succeeds.
type Option ¶
Option is a function that configures Options.
func WithAdditionalDateLayouts ¶
WithAdditionalDateLayouts adds more date parsing layouts to the defaults.
func WithCoerce ¶
WithCoerce enables automatic type coercion (e.g. string "123" to int 123).
func WithCoerceDateUnixMilliseconds ¶
WithCoerceDateUnixMilliseconds allows coercing unix timestamp (ms) to Date.
func WithCoerceDateUnixSeconds ¶
WithCoerceDateUnixSeconds allows coercing unix timestamp (seconds) to Date.
func WithCoerceDurationMilliseconds ¶
WithCoerceDurationMilliseconds allows coercing numeric milliseconds to Duration.
func WithCoerceDurationSeconds ¶
WithCoerceDurationSeconds allows coercing numeric seconds to Duration.
func WithCoerceNumberUnderscore ¶
WithCoerceNumberUnderscore allows underscores in number strings (e.g. "1_000").
func WithCoerceTrimSpace ¶
WithCoerceTrimSpace trims spaces from strings before validation/coercion.
func WithDateLayouts ¶
WithDateLayouts overrides the default date parsing layouts.
func WithDefaultOnNull ¶
WithDefaultOnNull enables setting default values when input is explicit null.
func WithFailFast ¶
WithFailFast stops validation on the first error.
func WithFormatter ¶
WithFormatter sets a custom error message formatter.
func WithMaxIssues ¶
WithMaxIssues limits the number of issues reported. Validation stops after reaching this limit.
func WithOmitZero ¶
WithOmitZero treats zero values (e.g. 0, "") as missing/undefined.
func WithTimeLocation ¶
WithTimeLocation sets the time location for date parsing/formatting.
type RecordSchema ¶
RecordSchema validates maps with homogeneous value types.
type Reporter ¶ added in v0.18.6
Reporter is the interface passed to Custom rules for reporting validation issues.
type RuleFn ¶ added in v0.18.5
RuleFn is the function signature for custom validation rules used in Custom().
type TextFieldBuilder ¶
type TextFieldBuilder[T any] = object.TextFieldBuilder[T]
TextFieldBuilder is the entry point for defining rules on string fields.
type ValidationError ¶
type ValidationError = issues.ValidationError
ValidationError is an error type that aggregates multiple validation issues.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
ast
internal/ast/ast.go
|
internal/ast/ast.go |
|
codec
internal/codec/codec.go
|
internal/codec/codec.go |
|
defaults
internal/defaults/defaults.go
|
internal/defaults/defaults.go |
|
engine
internal/engine/engine.go
|
internal/engine/engine.go |
|
issues
internal/issues/issues.go
|
internal/issues/issues.go |
|
path
internal/path/path.go
|
internal/path/path.go |
|
reflection
internal/reflection/reflection.go
|
internal/reflection/reflection.go |
|
registry
internal/registry/registry.go
|
internal/registry/registry.go |
|
ruleset
internal/ruleset/ruleset.go
|
internal/ruleset/ruleset.go |
|
testkit
internal/testkit/testkit.go
|
internal/testkit/testkit.go |
|
validate/schema/options.go
|
validate/schema/options.go |
|
array
schema/array/array.go
|
schema/array/array.go |
|
boolean
schema/boolean/boolean.go
|
schema/boolean/boolean.go |
|
combinator
schema/combinator/combinator.go
|
schema/combinator/combinator.go |
|
date
schema/date/date.go
|
schema/date/date.go |
|
duration
schema/duration/duration.go
|
schema/duration/duration.go |
|
number
schema/number/number.go
|
schema/number/number.go |
|
number/util
schema/number/util/util.go
|
schema/number/util/util.go |
|
object
schema/object/object.go
|
schema/object/object.go |
|
record
schema/record/record.go
|
schema/record/record.go |
|
text
schema/text/text.go
|
schema/text/text.go |