63 lines
3.7 KiB
Go
63 lines
3.7 KiB
Go
// 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"`
|
|
Pattern string `yaml:"pattern,omitempty" json:"pattern,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"`
|
|
|
|
// ReadOnly: if true, clients render this property as disabled and
|
|
// suppress edit affordances. Not enforced by the validator (a
|
|
// rejected write is the wrong UX for a read-only field — the
|
|
// server strips the value instead). Surface to clients via JSON.
|
|
ReadOnly bool `yaml:"readOnly,omitempty" json:"readOnly,omitempty"`
|
|
|
|
// Labels is an extension used by the form renderer to display a
|
|
// human label next to a code in enum-with-labels dropdowns. The
|
|
// key matches an entry in Enum; the value is the display label
|
|
// (e.g. {"ACM": "Acme Inc"}). Server-injected from the cascade's
|
|
// field_codes:codes map; not enforced by the validator.
|
|
Labels map[string]string `yaml:"x-labels,omitempty" json:"x-labels,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"`
|
|
}
|