// Package jsonschema is a focused JSON Schema 2020-12 validator covering only
// the subset of keywords used by the form-data system. See validate.go for the
// supported keyword list. Unsupported keywords are silently ignored — the
// form-spec meta-schema enforces that authors only use the supported subset.
//
// This package is intentionally smaller than a full JSON Schema implementation
// (e.g. github.com/santhosh-tekuri/jsonschema). Match-implementation-cost-to-
// surface-used: the form system never needs $ref, oneOf/anyOf, if/then/else,
// or remote schema fetch in v0; reimplementing that machinery would be more
// code than the entire validator we ship here.
//
// When the v1+ form-spec adds those features, revisit this trade-off.
package jsonschema

// Schema is the in-memory representation of a JSON Schema. Fields use both
// YAML and JSON tags so the same struct round-trips through either encoding —
// form specs are authored in YAML, but data submissions arrive as JSON.
//
// Pointer-typed fields (Minimum, Maximum, MinLength, MaxLength) distinguish
// "unset" from "set to zero", which matters because zero is a valid bound.
//
// AdditionalProperties is `any` to accept either a bool (the v0-supported
// shape — only `false` is enforced; `true` and unset are both "allow") or a
// nested schema (parsed but not enforced in v0).
type Schema struct {
	Type                 string             `yaml:"type" json:"type,omitempty"`
	Properties           map[string]*Schema `yaml:"properties" json:"properties,omitempty"`
	Required             []string           `yaml:"required" json:"required,omitempty"`
	Items                *Schema            `yaml:"items" json:"items,omitempty"`
	Enum                 []any              `yaml:"enum" json:"enum,omitempty"`
	Minimum              *float64           `yaml:"minimum" json:"minimum,omitempty"`
	Maximum              *float64           `yaml:"maximum" json:"maximum,omitempty"`
	MinLength            *int               `yaml:"minLength" json:"minLength,omitempty"`
	MaxLength            *int               `yaml:"maxLength" json:"maxLength,omitempty"`
	Format               string             `yaml:"format" json:"format,omitempty"`
	AdditionalProperties any                `yaml:"additionalProperties" json:"additionalProperties,omitempty"`
	Title                string             `yaml:"title" json:"title,omitempty"`
	Description          string             `yaml:"description" json:"description,omitempty"`
	Default              any                `yaml:"default" json:"default,omitempty"`
}

// Error reports a single validation failure. Path is a JSON Pointer (RFC 6901)
// pointing at the offending value within the validated document. Both fields
// flow through the form handler unchanged into the JSON response, so the
// browser-side renderer can locate and highlight the right widget.
type Error struct {
	Path    string `json:"path"`
	Message string `json:"message"`
}
