Documentation
¶
Overview ¶
Package schema turns a cmdguard config struct into a JSON Schema document.
The point is to let editors, CI pipelines, and non-Go tools validate a config file without running the binary. The schema is derived from the same struct tags that drive flags and strict config loading (v5.ConfigKeys), so it cannot accept a key the loader rejects or miss one it accepts.
This package deliberately lives outside the core module: it is a build-time code generator, not runtime machinery, and core has no reason to carry it.
Validation at runtime is already covered by [cmdguard.WithStrictConfigKeys]; this package is for everything that happens before the binary runs.
Index ¶
Constants ¶
const Dialect = "https://json-schema.org/draft/2020-12/schema"
Dialect is the JSON Schema dialect emitted. Draft 2020-12 is what editors and CI validators support today.
const ExtensionKey = "x-cmdguard"
ExtensionKey names the non-standard hints cmdguard adds to a property. It is namespaced so a validator ignores it and a tool can read it.
Variables ¶
This section is empty.
Functions ¶
func CommandSchemas ¶
func CommandSchemas(description *v5.CLIDescription) map[string]*Document
CommandSchemas builds one flag schema per command path from the machine contract. Keys are command paths ("app", "app deploy"); each document describes the flags that command accepts, including inherited ones.
This is for tooling that drives a binary: a config schema constrains a file, a flag schema constrains an invocation.
Types ¶
type Document ¶
type Document struct {
Schema string `json:"$schema"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Type string `json:"type"`
Properties PropertyMap `json:"properties"`
Required []string `json:"required,omitempty"`
AdditionalProperties bool `json:"additionalProperties"`
}
Document is a JSON Schema describing a cmdguard config file.
func FromCLI ¶
FromCLI builds the schema for a CLI's config, using the CLI's name and short description as the document's title and description.
func FromConfig ¶
FromConfig builds the schema for a cmdguard config struct.
The result rejects unknown keys (additionalProperties: false) to match strict config loading: a typo caught by the schema is the same typo the binary would reject.
type Hints ¶
type Hints struct {
// Flag is the command-line flag that sets the same value.
Flag string `json:"flag,omitempty"`
// Env is the environment variable that sets the same value.
Env string `json:"env,omitempty"`
// Format is a cmdguard-specific type hint (duration, hostport, path) for
// values JSON Schema only sees as strings.
Format string `json:"format,omitempty"`
// Secret marks a value that must never be logged or committed.
Secret bool `json:"secret,omitempty"`
}
Hints carries metadata JSON Schema has no home for.
type Property ¶
type Property struct {
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
Default any `json:"default,omitempty"`
Enum []string `json:"enum,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
MinLength *int `json:"minLength,omitempty"`
MaxLength *int `json:"maxLength,omitempty"`
Pattern string `json:"pattern,omitempty"`
Format string `json:"format,omitempty"`
Properties PropertyMap `json:"properties,omitempty"`
Required []string `json:"required,omitempty"`
Items *Property `json:"items,omitempty"`
AdditionalProperties *bool `json:"additionalProperties,omitempty"`
Hints *Hints `json:"x-cmdguard,omitempty"`
}
Property describes one accepted key. Standard fields carry the constraints a validator enforces; Hints carries what only cmdguard knows.
type PropertyMap ¶
PropertyMap is a set of named properties. Plain Go maps marshal in map iteration order under encoding/json/v2, so this type sorts its keys: generated schemas are byte-for-byte reproducible.
func (PropertyMap) MarshalJSONTo ¶
func (m PropertyMap) MarshalJSONTo(out *jsontext.Encoder) error