configschema

package
v0.18.4 Latest Latest
Warning

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

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

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

View Source
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"
)
View Source
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

View Source
var ErrBuild = errors.New("configschema: build")

ErrBuild wraps schema construction failures.

View Source
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).

View Source
var ErrTwin = errors.New("configschema: yaml twin")

ErrTwin wraps YAML-twin path and conversion failures.

Functions

func Build

func Build() (map[string]any, error)

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

func BuildJSON() ([]byte, error)

BuildJSON renders Build() deterministically: sorted keys, two-space indent, trailing newline — the exact bytes committed at SchemaPath.

func Check

func Check(root string) ([]string, error)

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

func CorpusFiles(root string) (jsonFiles, yamlFiles []string, err error)

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

func SourcePath(yamlRel string) (string, error)

SourcePath is the inverse of TwinPath: configs/yaml/<rel>.yaml -> configs/<rel>.json.

func ToYAMLTwin

func ToYAMLTwin(jsonData []byte) ([]byte, error)

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.

func TwinPath

func TwinPath(jsonRel string) (string, error)

TwinPath maps a root-relative JSON fixture path to its YAML twin: configs/<rel>.json -> configs/yaml/<rel>.yaml.

func Write

func Write(root string) ([]string, error)

Write regenerates the committed schema and the YAML twin of every configs/**/*.json fixture under root, returning the root-relative paths it wrote (schema first, then twins in corpus order).

Types

type Validator

type Validator struct {
	// contains filtered or unexported fields
}

Validator validates config documents against one compiled schema.

func Compile

func Compile(schemaJSON []byte) (*Validator, error)

Compile compiles a draft 2020-12 schema document.

func LoadValidator

func LoadValidator(root string) (*Validator, error)

LoadValidator compiles the committed schema (SchemaPath) under root.

func (*Validator) Validate

func (v *Validator) Validate(cfgJSON []byte) error

Validate checks one JSON config document (bytes as returned by configfile.ReadFile / ToJSON) against the schema.

func (*Validator) ValidateFile

func (v *Validator) ValidateFile(path string) error

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.

Jump to

Keyboard shortcuts

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