Documentation
¶
Overview ¶
Package jsonschema implements JSON schemas.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func IsValidationError ¶
IsValidationError reports whether err is a validation error. If this reports true on an error returned by [Schema.Validate], it means that the instance does not match the schema. If this reports false, it means that there is some error in the JSON schema itself.
func SetDefaultSchema ¶
SetDefaultSchema sets the default schema. The argument should be something like "draft7" or "draft2020-12". This is a global property, as there is no way to pass the desired value into the JSON decoder. Callers should use appropriate locking. This is mainly for tests.
func SetLoader ¶
func SetLoader(fn func(schemaID string, uri *url.URL) (*Schema, error)) func(string, *url.URL) (*Schema, error)
SetLoader sets a function to call when resolving a $ref to an external schema. This is a global property, as there is no way to pass the desired value into the JSON decoder. Callers should use appropriate locking.
Note that when unmarshaling user-written schemas, the loader function can be called with arbitrary URIs. It's probably unwise to simply call net/http.Get in all cases.
To fully support JSON schema cross references, the loader should call SchemaFromJSON. The caller will handle calling [Schema.Resolve].
This returns the old loader function. The default loader function is nil, which will produce an error for a $ref to an external schema.
Types ¶
type ResolveOpts ¶
type ResolveOpts = types.ResolveOpts
ResolveOpts is options that may be passed to [Schema.Resolve].
type Schema ¶
Schema is a JSON schema. A JSON schema determines whether an instance is valid or not.
If you have a JSON schema in JSON format, unmarshal it into a value of this type:
var s jsonschema.Schema
if err := json.Unmarshal(data, &s); err != nil {
// handle error
}
To create a JSON schema in Go code, use a Builder for the schema version you want to use; do not just fill in the Parts slice. For the current default JSON schema version use [draft202012.NewBuilder].
You can also infer a JSON schema from a Go type. That is, given a Go type, you can build a JSON schema that tests that a JSON value can be unmarshaled into a value of that Go type. To do this create a Builder for the schema version you want to use, and call [builder.Infer] or [builder.InferType].
func SchemaFromJSON ¶
SchemaFromJSON builds a Schema from a JSON value that has already been parsed. This could be used as something like
var v any
if err := json.Unmarshal(data, &v); err != nil { ... }
s, err := schema.SchemaFromJSON(v)
This can be useful in cases where it's not clear whether the JSON encoding contains a schema or not.
The optional schemaID argument is something like [draft202012.SchemaID]. The optional uri is where the schema was loaded from.
It is normally necessary to call Resolve on the result.
type ValidateOpts ¶
type ValidateOpts = types.ValidateOpts
ValidateOpts describes validation options that may be passed to [Schema.ValidateWithOpts]. These are uncommon so we use a different method for them.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package builder defines a Builder type that may be used to build a [jsonschema.Schema] step by step.
|
Package builder defines a Builder type that may be used to build a [jsonschema.Schema] step by step. |
|
Package draft201909 defines the keywords used by JSON schema version 2020-109.
|
Package draft201909 defines the keywords used by JSON schema version 2020-109. |
|
Package draft202012 defines the keywords used by JSON schema version 2020-12.
|
Package draft202012 defines the keywords used by JSON schema version 2020-12. |
|
Package draft7 defines the keywords used by JSON schema version draft7.
|
Package draft7 defines the keywords used by JSON schema version draft7. |
|
Package format defines format checkers for the format keyword.
|
Package format defines format checkers for the format keyword. |
|
internal
|
|
|
argtype
Package argtype defines a few helpers for schema.ArgType.
|
Package argtype defines a few helpers for schema.ArgType. |
|
cmd/keywordgen
command
keywordgen generates repetitive code for JSON schema keywords.
|
keywordgen generates repetitive code for JSON schema keywords. |
|
cmd/testgen
command
testgen downloads the current testsuite from json-schema-org and updates the tests in the tree.
|
testgen downloads the current testsuite from json-schema-org and updates the tests in the tree. |
|
cmd/validatorgen
command
validatorgen generates validator functions for different schema argument types.
|
validatorgen generates validator functions for different schema argument types. |
|
schemacache
Package schemacache is a simple in-process cache for schemas that have been parsed.
|
Package schemacache is a simple in-process cache for schemas that have been parsed. |
|
validator
Package validator contains functions to handle different schema arguments.
|
Package validator contains functions to handle different schema arguments. |
|
validerr
Package validerr defines the errors return by a failure to validate.
|
Package validerr defines the errors return by a failure to validate. |
|
Package jsonpointer implements JSON pointers for the jsonschema package.
|
Package jsonpointer implements JSON pointers for the jsonschema package. |
|
Package notes defines a type that holds information passed between keywords during schema validation.
|
Package notes defines a type that holds information passed between keywords during schema validation. |
|
Package types defines the JSON schema types.
|
Package types defines the JSON schema types. |