Documentation
¶
Overview ¶
Package z provides the public API for Check schema validation.
Usage:
import "github.com/Alinxus/check/z"
schema := z.Object(map[string]z.Schema{
"name": z.String().Min(3).Max(50),
"email": z.String().Email(),
"age": z.Int().Min(0).Max(150).Optional(),
"tags": z.Array(z.String()).MinLength(1),
})
result, err := schema.Parse(data)
Index ¶
- func Any() *check.AnySchema
- func Array(element Schema) *check.ArraySchema
- func Bool() *check.BoolSchema
- func CoerceBool() *check.CoerceBoolSchema
- func CoerceFloat() *check.CoerceFloatSchema
- func CoerceInt() *check.CoerceIntSchema
- func Float() *check.FloatSchema
- func Int() *check.IntSchema
- func Map(key, value Schema) *check.MapSchema
- func Object(fields map[string]Schema) *check.ObjectSchema
- func ParseJSON(schema Schema, data []byte) (any, error)
- func ParseStruct(schema Schema, data any, target any) error
- func ParseTyped[T any](schema Schema, value any) (T, error)
- func String() *check.StringSchema
- func Time() *check.TimeSchema
- func Transform(inner Schema, fn func(any) (any, error)) *check.TransformSchema
- func Union(schemas ...Schema) *check.UnionSchema
- type Schema
- type ValidationError
- type ValidationErrors
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Array ¶
func Array(element Schema) *check.ArraySchema
Array creates an array schema where each element must match the given schema.
func CoerceBool ¶
func CoerceBool() *check.CoerceBoolSchema
CoerceBool wraps a bool schema to coerce strings to bool.
func CoerceFloat ¶
func CoerceFloat() *check.CoerceFloatSchema
CoerceFloat wraps a float schema to coerce strings to float64.
func CoerceInt ¶
func CoerceInt() *check.CoerceIntSchema
Coerce wraps a schema to coerce input types before validation. For example, CoerceInt() will parse "42" (string) into 42 (int).
func Object ¶
func Object(fields map[string]Schema) *check.ObjectSchema
Object creates an object schema with named fields.
func ParseJSON ¶
ParseJSON validates raw JSON bytes against a schema. It unmarshals the JSON into map[string]any / []any / primitives, then validates.
func ParseStruct ¶
ParseStruct validates data against a schema, then maps the result into a Go struct. The target must be a pointer to a struct.
func ParseTyped ¶
ParseTyped validates a value and returns a typed result using generics.
name, err := z.ParseTyped[string](z.String().Min(3), "Alice") age, err := z.ParseTyped[int](z.Int().Positive(), 25)
func Transform ¶
Transform wraps a schema and applies a transformation function after validation.
// Parse a price string into cents
cents := z.Transform(z.Float().Min(0), func(v any) (any, error) {
return int(v.(float64) * 100), nil
})
func Union ¶
func Union(schemas ...Schema) *check.UnionSchema
Union creates a schema that accepts a value matching any of the given schemas.
Types ¶
type ValidationError ¶
type ValidationError = check.ValidationError
ValidationError represents a single validation failure.
type ValidationErrors ¶
type ValidationErrors = check.ValidationErrors
ValidationErrors is a slice of ValidationError that implements error.