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: "Ada" } -> runtime.noop:schema=TestUser
Index ¶
Constants ¶
const ID = "schema"
Variables ¶
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.
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.
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 ¶
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
}
SchemaField is one parsed field declaration.
type Schema ¶
Schema is one complete declaration.
func SchemasFromMap ¶
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.