The json-schema CLI does two things: check that a schema is valid (lint), and emit pre-compiled validator code (gen-validator).
go install github.com/lestrrat-go/json-schema/cmd/json-schema@latestParses a schema and compiles it, reporting any structural or semantic problem. Reads a file, or - for stdin.
json-schema lint schema.json
# Schema schema.json is valid
echo '{"type": "string"}' | json-schema lint -
# Schema stdin is validIf the document is not a valid schema or cannot be compiled, lint prints the error and exits non-zero — handy as a pre-commit or CI check on schema files.
Compiles a schema and prints Go source that rebuilds the validator directly, so production code can skip compilation. Reads a file or - for stdin.
json-schema gen-validator --name UserValidator user-schema.json--name sets the generated variable name (default val). See Code Generation for a full example of the output and how to wire it into go:generate.
# from stdin, default variable name "val"
echo '{"type":"string","minLength":1}' | json-schema gen-validator -
# val := validator.String().MinLength(1).MustBuild()| Command | Purpose | Key flag |
|---|---|---|
lint [file|-] |
Verify a schema is valid | — |
gen-validator [file|-] |
Print Go validator code | --name <var> (default val) |