Documentation
¶
Overview ¶
Package configschema derives the published JSON Schema (schema/apic-config.schema.json) from the generator's config model, and validates JSON/YAML configs against it. It is build-time tooling: tests and tools/configschema.go call it; nothing at runtime does.
Index ¶
- Constants
- Variables
- func Build() (map[string]any, error)
- func BuildJSON() ([]byte, error)
- func Check(root string) ([]string, error)
- func CorpusFiles(root string) (jsonFiles, yamlFiles []string, err error)
- func ModuleRoot() string
- func SourcePath(yamlRel string) (string, error)
- func ToYAMLTwin(jsonData []byte) ([]byte, error)
- func TwinPath(jsonRel string) (string, error)
- func Write(root string) ([]string, error)
- type Validator
Constants ¶
const ( // SchemaPath is where the committed schema lives, relative to ModuleRoot. SchemaPath = "schema/apic-config.schema.json" // SchemaID is the schema's $id. SchemaID = "https://devnw.dev/apic/schema/apic-config.schema.json" // Draft is the JSON Schema dialect the generated schema declares. Draft = "https://json-schema.org/draft/2020-12/schema" )
const ( // ConfigsDir is the fixture corpus root, relative to ModuleRoot. ConfigsDir = "configs" // YAMLTwinDir holds the YAML twin of every JSON fixture, relative to ConfigsDir. YAMLTwinDir = "yaml" )
Variables ¶
var ErrBuild = errors.New("configschema: build")
ErrBuild wraps schema construction failures.
var ErrSchemaViolation = errors.New("configschema: config violates schema")
ErrSchemaViolation wraps a config that does not conform to the schema (or cannot be parsed as JSON at all).
var ErrTwin = errors.New("configschema: yaml twin")
ErrTwin wraps YAML-twin path and conversion failures.
Functions ¶
func Build ¶
Build returns the JSON Schema (draft 2020-12) for the apic config as a generic document. Reflection over generator.Config supplies the structure (json tags, embedded-struct promotion with outer-shadows-embedded precedence, pointer nullability, additionalProperties:false everywhere to mirror the generator's RejectUnknownMembers decode); Go doc comments supply descriptions; overlay.go supplies enums and required members.
func BuildJSON ¶
BuildJSON renders Build() deterministically: sorted keys, two-space indent, trailing newline — the exact bytes committed at SchemaPath.
func Check ¶
Check verifies (1) the committed schema matches the model, (2) every corpus config, JSON and YAML, validates against the freshly built schema, (3) every JSON fixture has a semantically equivalent YAML twin, and (4) no twin is orphaned. It returns one human-readable problem per finding; an empty slice means clean.
func CorpusFiles ¶
CorpusFiles lists every JSON and YAML config under root/configs (recursively, skipping configs/testdata), sorted, as root-relative paths.
func ModuleRoot ¶
func ModuleRoot() string
ModuleRoot returns the apic checkout root. It is derived from this source file's compiled location and falls back to walking up from the working directory until a go.mod is found.
func SourcePath ¶
SourcePath is the inverse of TwinPath: configs/yaml/<rel>.yaml -> configs/<rel>.json.
func ToYAMLTwin ¶
ToYAMLTwin converts a JSON fixture into its YAML twin. Cross-file schema references ("$ref": "<rel>.json#/schemas/<Name>") are rewritten to point at the referenced fixture's own twin ("<rel>.yaml#/schemas/<Name>") so the twin corpus resolves entirely within configs/yaml/. Only the VALUE of a member literally named "$ref" is touched.
Types ¶
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator validates config documents against one compiled schema.
func LoadValidator ¶
LoadValidator compiles the committed schema (SchemaPath) under root.
func (*Validator) Validate ¶
Validate checks one JSON config document (bytes as returned by configfile.ReadFile / ToJSON) against the schema.
func (*Validator) ValidateFile ¶
ValidateFile reads a JSON or YAML config and validates it. Cross-file "$ref" stubs are validated as written (the schema permits them); resolution is not needed for shape checking.