z

package
v0.0.0-...-cfcbc57 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2026 License: MIT Imports: 2 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any() *check.AnySchema

Any creates a schema that accepts any value.

func Array

func Array(element Schema) *check.ArraySchema

Array creates an array schema where each element must match the given schema.

func Bool

func Bool() *check.BoolSchema

Bool creates a new boolean 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 Float

func Float() *check.FloatSchema

Float creates a new float64 schema.

func Int

func Int() *check.IntSchema

Int creates a new integer schema.

func Map

func Map(key, value Schema) *check.MapSchema

Map creates a map schema where all keys/values must match their respective schemas.

func Object

func Object(fields map[string]Schema) *check.ObjectSchema

Object creates an object schema with named fields.

func ParseJSON

func ParseJSON(schema Schema, data []byte) (any, error)

ParseJSON validates raw JSON bytes against a schema. It unmarshals the JSON into map[string]any / []any / primitives, then validates.

func ParseStruct

func ParseStruct(schema Schema, data any, target any) error

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

func ParseTyped[T any](schema Schema, value any) (T, error)

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 String

func String() *check.StringSchema

String creates a new string schema.

func Time

func Time() *check.TimeSchema

Time creates a new time schema.

func Transform

func Transform(inner Schema, fn func(any) (any, error)) *check.TransformSchema

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 Schema

type Schema = check.Schema

Schema is the core validation interface.

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.

Jump to

Keyboard shortcuts

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