Documentation
¶
Overview ¶
Package jsonschema validates JSON documents somebody else wrote against a JSON Schema 2020-12 they publish — a webhook's payload, an OpenAPI request body, another program's configuration — with the standard library alone.
v, err := jsonschema.NewValidator(schema, jsonschema.Config{})
report, err := v.Validate(body, 1<<20)
if !report.OK() { … report.Paths() … }
The core, applicator and validation vocabularies are compiled; "$ref" resolves within the schema and to the resources Config registers, never over the network; "format" is asserted only for the formats Config.AssertFormats names. Drafts before 2020-12, the dynamic scope, "$vocabulary", the unevaluated and content vocabularies are refused by name. A report's paths follow the validation package's grammar and no message quotes the document.
Index ¶
Constants ¶
const ( // CodeSchemaInvalid is 0.3.103.1, [SchemaInvalid]. CodeSchemaInvalid errs.Code = corejsonschema.CodeJSONSchemaSchemaInvalid // CodeKeywordRefused is 0.3.103.2, [KeywordRefused]. CodeKeywordRefused errs.Code = corejsonschema.CodeJSONSchemaKeywordRefused // CodeRefUnresolved is 0.3.103.3, [RefUnresolved]. CodeRefUnresolved errs.Code = corejsonschema.CodeJSONSchemaRefUnresolved // CodeFormatUnknown is 0.3.103.4, [FormatUnknown]. CodeFormatUnknown errs.Code = corejsonschema.CodeJSONSchemaFormatUnknown // CodeViolation is 0.3.103.5: the code of a violation no validation code // names — type, multipleOf, the combinators, the applicators, format. CodeViolation errs.Code = corejsonschema.CodeJSONSchemaViolation // CodeReferenceLoop is 0.3.103.6, [ReferenceLoop]. CodeReferenceLoop errs.Code = corejsonschema.CodeJSONSchemaReferenceLoop )
The error codes, range 0.3.103.*, declared in the core (ADR 0160).
Variables ¶
var ( // SchemaInvalid refuses a schema NewValidator cannot compile. SchemaInvalid = corejsonschema.SchemaInvalid // KeywordRefused refuses a keyword out of scope, or another draft. KeywordRefused = corejsonschema.KeywordRefused // RefUnresolved refuses a "$ref" naming nothing compiled or registered. RefUnresolved = corejsonschema.RefUnresolved // FormatUnknown refuses a format to assert without a checker. FormatUnknown = corejsonschema.FormatUnknown // Violation is the sentinel of [CodeViolation]. Violation = corejsonschema.Violation // ReferenceLoop refuses a "$ref" cycle that consumes nothing. ReferenceLoop = corejsonschema.ReferenceLoop )
The sentinels, for errors.Is: each carries the code of the same name.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config = svcjsonschema.Config
Config configures NewValidator — the resources a "$ref" may name, the formats asserted.
type Validator ¶
type Validator = svcjsonschema.Validator
Validator is a compiled schema, immutable and reused; its Validate returns the report.