Documentation
¶
Index ¶
- Variables
- func Validate(value, schema any, opts ...Option) error
- type Draft
- type Loader
- type Option
- func AssertContent() Option
- func AssertFormats() Option
- func WithBaseURI(uri string) Option
- func WithDraft(d Draft) Option
- func WithFormat(name string, valid func(string) bool) Option
- func WithLoader(l Loader) Option
- func WithMatchTimeout(d time.Duration) Option
- func WithResource(uri string, doc any) Option
- type Schema
- type SchemaError
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
var ErrUnresolvable = errors.New("unresolvable reference")
Reported, wrapped in a *SchemaError, for a reference to a document that is neither in the schema, bundled, given with WithResource nor served by a loader, or to a place or anchor its document lacks
var ErrUnsupportedURI = errors.New("unsupported URI")
Wrapped by a Loader for a URI it does not serve, so the next loader is asked
Functions ¶
Types ¶
type Loader ¶
Source of documents a schema refers to by URI: the JSON document at the URI, decoded; an error wrapping ErrUnsupportedURI hands the URI to the next loader
func DataLoader ¶
func DataLoader() Loader
Loader decoding data URIs, which carry the document itself, percent-encoded or in base64
func FileLoader ¶
Loader reading file URIs from fsys, the path of the URI without its leading slash naming the file there; a URI naming another host is not served
func HTTPLoader ¶
Loader fetching http and https URIs with a GET through client, or when client is nil through a shared client that honours the proxy settings and the address family preference
type Option ¶
type Option func(*config)
Setting for Compile, CompileContext and Validate
func AssertContent ¶
func AssertContent() Option
contentEncoding, contentMediaType and contentSchema asserted on string values: base16, base32 and base64 encodings, JSON media types and the schema of the decoded document
func AssertFormats ¶
func AssertFormats() Option
format asserted on string values for every format the package knows instead of only noted, as JSON Schema leaves it by default
func WithBaseURI ¶
URI the root schema was retrieved from: the base its relative $id and references resolve against
func WithDraft ¶
Dialect for a schema whose $schema names none the package knows, Draft202012 when not given
func WithFormat ¶
Check asserted for the named format on string values, taking the place of the built-in one
func WithLoader ¶
Loader asked, after those given before it, for referenced documents the schema does not contain and no WithResource gives; without a loader such references are errors and nothing is fetched
func WithMatchTimeout ¶
Time one pattern match may take, and a tenth of what all matches of one validation may take together, before the validation stops as failed; 100ms when not given, zero or less lets matches run unbounded
func WithResource ¶
Document a reference to uri reaches, so no loader is asked for it; repeated for several documents
type Schema ¶
type Schema struct {
// contains filtered or unexported fields
}
JSON Schema compiled once, every reference resolved: immutable and safe for concurrent use
func Compile ¶
Schema compiled from a decoded JSON value (*jsonutil.Object, map[string]any, []any, numbers, strings, booleans, nil) after checking it against the metaschema its $schema names; a *SchemaError when it is not a valid schema or a reference cannot be resolved
func CompileContext ¶
Schema compiled as by Compile, ctx passed to the loaders that fetch referenced documents
func (*Schema) Errors ¶
func (s *Schema) Errors(value any) []*ValidationError
Every failure of the value in the order the keywords of the schema produced them, those of anyOf and oneOf alternatives as Causes; nil when the value conforms; a validation that cannot finish (a pattern match out of time, a schema whose work explodes) gives only the error saying why
type SchemaError ¶
type SchemaError struct {
// Where the problem is: a URI with a JSON pointer fragment written without percent-encoding, # alone for the root of the schema
Location string
// What is wrong
Message string
// The metaschema rule the schema breaks, its InstancePath leading into the schema; nil for other problems
Violation *ValidationError
// Underlying cause, such as ErrUnresolvable, a loader failure or a pattern syntax error; nil when there is none
Err error
}
Schema that cannot be compiled: it breaks its metaschema, a reference cannot be resolved, or a keyword holds a value it cannot use
func (*SchemaError) Error ¶
func (e *SchemaError) Error() string
The problem with its location in the schema
func (*SchemaError) Unwrap ¶
func (e *SchemaError) Unwrap() error
The underlying cause; the metaschema violation is kept out of the chain so it is never taken for a failure of the value
type ValidationError ¶
type ValidationError struct {
// What is wrong, in plain words with values quoted as JSON
Message string
// Keyword that failed, empty for a false schema or a validation that cannot finish for a reason other than a pattern match running out of time
Keyword string
// Steps from the root of the value to the failing part: property names as string, array positions as int
InstancePath []any
// JSON pointer from the root schema to the failing keyword along the path evaluation took, references included
SchemaPath string
// Absolute location of the failing keyword: the URI of its schema resource with a JSON pointer fragment, written as the pointer reads, without percent-encoding
SchemaURI string
// Failures of the subschemas behind this one, for anyOf, oneOf and the keywords that summarize several failures
Causes []*ValidationError
// contains filtered or unexported fields
}
Failure of a value against a schema: what is wrong, where in the value, and which keyword at which place in the schema
func BestMatch ¶
func BestMatch(errs []*ValidationError) *ValidationError
Most relevant of a list of errors, nil for an empty list: among the errors, the one highest in the value, not from anyOf or oneOf, whose value has a type its schema rules out, the first of equals; then into its causes for as long as one of them is more relevant than the rest, there preferring the deepest, from anyOf or oneOf, whose value has the type its schema expects
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
The message, preceded by where in the value it applies and followed by the schema keyword that failed
func (*ValidationError) Location ¶
func (e *ValidationError) Location() string
JSON pointer of the failing part of the value, empty for the value itself