validity

package module
v0.0.0-...-4995b57 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 5 Imported by: 0

README

validity

Validity is an experimental codegen pipeline for generating optimal, language-native validators from existing validation logic. The idea: you already have constraints defined somewhere — struct tags, ozzo-validation methods, JSON Schema, CUE files — and you shouldn't have to rewrite them to get a fast, static validator in another language.

How it works

Input plugins extract constraints from a source and express them as a CUE value. CUE is the intermediate representation: expressive enough to capture constraints from any source, and well-suited for merging, overriding, and reasoning about them. Output plugins consume the IR and generate a validator in the target language.

The generated code is optimal for its target — pre-allocated sentinel errors, no heap allocations in the hot path, nothing dynamic at validation time.

Inputs and outputs are plugins that register themselves via init(). The default validity binary ships with built-in plugins, but you can compose your own binary to extend it.

Status

Early prototype. The plugin interfaces and IR are still taking shape.

Documentation

Overview

Package validity helps you build fast and portable validation logic.

Index

Constants

This section is empty.

Variables

View Source
var (
	Inputs  = make(registry[Input])
	Outputs = make(registry[Output])

	// ErrPluginAlreadyRegistered is returned when a plugin ID is registered more than once.
	ErrPluginAlreadyRegistered = errors.New("plugin already registered")
)

Inputs is the global registry of input plugins. Outputs is the global registry of output plugins.

Functions

func Cfg

func Cfg[T any](cfg map[string]any, key string) (T, error)

Cfg retrieves a value from cfg by key, returning an error if the key is absent or the value is not of type T.

Types

type Constraint

type Constraint struct {
	Kind   Kind
	Op     string // for KindBound: "<=", "<", ">=", ">"
	Int    int64  // for KindBound
	Values []any  // for KindEnum: allowed values as native string/int64/float64
	Raw    cue.Value
}

Constraint is a single classified constraint on a Field.

type Def

type Def struct {
	Name string
	Raw  cue.Value
}

Def is a top-level CUE definition (e.g. #Person).

func (Def) Fields

func (d Def) Fields() ([]Field, error)

Fields returns the fields of the definition.

type Field

type Field struct {
	Name     string
	Optional bool
	Raw      cue.Value
}

Field is a field within a Def.

func (Field) Constraints

func (f Field) Constraints() []Constraint

Constraints returns the classified constraints on the field. Constraints the reading layer does not recognize are returned with Kind KindUnsupported and their Raw value intact, never dropped.

type IR

type IR struct {
	Raw cue.Value
}

IR is the intermediate representation passed between input and output plugins. The wrapped CUE value is the source of truth: the methods on IR and its child types are a classified view over it, and Raw is always available as an escape hatch for anything those methods do not cover.

func NewIR

func NewIR(v cue.Value) IR

NewIR returns an IR wrapping v.

func (IR) Definitions

func (ir IR) Definitions() ([]Def, error)

Definitions returns the top-level definitions in the IR.

type Input

type Input interface {
	Plugin
	Value(cfg map[string]any) (IR, error)
}

Input is a plugin that produces an IR from a source.

type Kind

type Kind int

Kind classifies a Constraint.

const (
	// KindUnsupported marks a constraint the reading layer could not classify.
	// Its Raw value is preserved so callers can report or handle it explicitly
	// rather than silently dropping it.
	KindUnsupported Kind = iota
	// KindBound is a numeric comparison constraint.
	KindBound
	// KindNonZero requires the field's value to differ from its type's zero value.
	KindNonZero
	// KindEnum requires the field's value to be one of a fixed set.
	KindEnum
)

type Output

type Output interface {
	Plugin
	Generate(cfg map[string]any, ir IR) error
}

Output is a plugin that consumes an IR and generates a validator.

type Plugin

type Plugin interface {
	ID() string
	Flags() []cli.Flag
}

Plugin is the base interface implemented by all input and output plugins.

Directories

Path Synopsis
cmd
validity command
examples
input
cue
Package cue implements the cue input plugin.
Package cue implements the cue input plugin.
validator
Package validator implements an input plugin that derives a CUE IR from go-playground/validator struct tags.
Package validator implements an input plugin that derives a CUE IR from go-playground/validator struct tags.
output
cel
Package cel implements the CEL output plugin.
Package cel implements the CEL output plugin.
cue
Package cue implements the CUE output plugin.
Package cue implements the CUE output plugin.
golang
Package golang implements the golang output plugin.
Package golang implements the golang output plugin.
Package runner provides the CLI entry point for validity.
Package runner provides the CLI entry point for validity.

Jump to

Keyboard shortcuts

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