schema

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package schema provides the `@schema NAME { Field Type `json:"..." validate:"..."` ... }` directive. The declaration lands in meta["schemas"]; the `:schema=NAME` modifier attaches the name to an atom so the strict-mode validation hook can find it.

Typical use:

@schema TestUser {
  Name  string `json:"name"  validate:"required"`
  Email string `json:"email" validate:"email"`
}
{ name: "Maksymilian" } -> runtime.noop:schema=TestUser

Index

Constants

View Source
const ID = "schema"

Variables

View Source
var Directive = core.Directive{
	Name:    "schema",
	Example: `@schema Plan { Summary string }`,
	Handler: handleDirective,
}

Directive parses `@schema NAME { ... }` into a Schema appended to meta["schemas"]. Duplicate names are a compile error.

View Source
var SchemaModifier = core.String("schema",
	func(b *action.Builder[any, any], name string) *action.Builder[any, any] {
		return b.Tag("schema:" + name)
	})

SchemaModifier attaches a schema name to an atom as a tag.

View Source
var ValidateAction = action.New("schema.validate", func(ctx context.Context, req ValidateRequest) (any, error) {
	if req.Name == "" {
		return nil, xerr.BadRequest("schema.validate: schema name is required (use @{ name: \"SchemaName\" })")
	}

	schemas, _ := schemasContextKey.From(ctx)
	targetSchema, ok := schemas[req.Name]
	if !ok {
		return nil, xerr.NotFound("schema.validate: schema '" + req.Name + "' not registered in workflow")
	}

	targetPayload := req.Payload
	if targetPayload == nil {
		targetPayload, _ = rootInputKey.From(ctx)
	}

	if targetPayload == nil {
		return nil, xerr.BadRequest("schema.validate: no payload available to validate")
	}

	if err := Validate(targetSchema, targetPayload); err != nil {
		return nil, err
	}

	return targetPayload, nil
}).Description("Validate payload against a declared @schema").
	Tag("schema", "validation").
	Build()

ValidateAction provides runtime schema validation on arbitrary payload maps.

Functions

func Bundle

func Bundle(_ map[string]string) core.Bundle

func Library added in v0.12.0

func Library() action.Library

func SplitFields added in v0.17.0

func SplitFields(s Schema) (payload, config []string)

SplitFields classifies schema fields by their transport tags.

A field with config:"true" goes only to compile config unless it
also declares payload:"true".
A field without config:"true" goes only to payload unless it
declares config:"true" without payload.

A field without either tag defaults to payload. A field with both goes to both channels. Any tag value other than "true" is treated as absent. Result slices preserve declaration order.

func Validate

func Validate(schema Schema, payload any) error

Validate checks payload against schema. It only enforces required fields and coarse type compatibility; richer validation is the job of the validation package.

func WithContextSchemas added in v0.12.0

func WithContextSchemas(ctx context.Context, schemas []Schema) context.Context

WithContextSchemas binds workflow schemas into the runtime context.

Types

type Field

type Field struct {
	Name     string
	JSONName string
	Type     string
	Kind     Kind
	ElemType string
	Pointer  bool
	Slice    bool
	Map      bool
	Tags     map[string]string
	// Embed is set only while a @schema body is being parsed: a line with
	// a single identifier is a composition (embed) of another declared
	// schema, not a field. Resolved schemas never carry Embed.
	Embed string
}

SchemaField is one parsed field declaration.

type Kind

type Kind uint8

SchemaKind is a coarse classification used by Validate.

const (
	KindString Kind = iota
	KindInt
	KindFloat
	KindBool
	KindAny
	KindStruct
)

type Schema

type Schema struct {
	Name   string
	Fields []Field
}

Schema is one complete declaration.

func ByName

func ByName(meta map[string]any, name string) (Schema, bool)

SchemaByName returns the named schema if declared.

func SchemasFromMap

func SchemasFromMap(meta map[string]any) []Schema

SchemasFromMap returns the schemas stored under meta["schemas"]. Nil-safe.

type ValidateRequest added in v0.12.0

type ValidateRequest struct {
	Name    string `json:"name" validate:"required"`
	Payload any    `json:"payload,omitempty"`
}

ValidateRequest specifies which schema to run against the input.

Jump to

Keyboard shortcuts

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