55/*
66Package jsonschema is an implementation of the [JSON Schema specification],
77a JSON-based format for describing the structure of JSON data.
8- The package can be used to read schemas for code generation, and to validate data using the
9- draft 2020-12 specification. Validation with other drafts or custom meta-schemas
10- is not supported.
8+ The package can be used to read schemas for code generation, and to validate
9+ data using the draft 2020-12 specification. Validation with other drafts
10+ or custom meta-schemas is not supported.
1111
12- Construct a [Schema] as you would any Go struct (for example, by writing a struct
13- literal), or unmarshal a JSON schema into a [Schema] in the usual way (with [encoding/json],
14- for instance). It can then be used for code generation or other purposes without
15- further processing.
12+ Construct a [Schema] as you would any Go struct (for example, by writing
13+ a struct literal), or unmarshal a JSON schema into a [Schema] in the usual
14+ way (with [encoding/json], for instance). It can then be used for code
15+ generation or other purposes without further processing.
16+ You can also infer a schema from a Go struct.
17+
18+ # Resolution
19+
20+ A Schema can refer to other schemas, both inside and outside itself. These
21+ references must be resolved before a schema can be used for validation.
22+ Call [Schema.Resolve] to obtain a resolved schema (called a [Resolved]).
23+ If the schema has external references, pass a [ResolveOptions] with a [Loader]
24+ to load them. To validate default values in a schema, set
25+ [ResolveOptions.ValidateDefaults] to true.
1626
1727# Validation
1828
19- Before using a Schema to validate a JSON value, you must first resolve it by calling
20- [Schema.Resolve].
21- The call [Resolved.Validate] on the result to validate a JSON value.
22- The value must be a Go value that looks like the result of unmarshaling a JSON
23- value into an [any] or a struct. For example, the JSON value
29+ Call [Resolved.Validate] to validate a JSON value. The value must be a
30+ Go value that looks like the result of unmarshaling a JSON value into an
31+ [any] or a struct. For example, the JSON value
2432
2533 {"name": "Al", "scores": [90, 80, 100]}
2634
@@ -41,8 +49,11 @@ or as a value of this type:
4149# Inference
4250
4351The [For] function returns a [Schema] describing the given Go type.
44- The type cannot contain any function or channel types, and any map types must have a string key.
45- For example, calling For on the above Player type results in this schema:
52+ Each field in the struct becomes a property of the schema.
53+ The values of "json" tags are respected: the field's property name is taken
54+ from the tag, and fields omitted from the JSON are omitted from the schema as
55+ well.
56+ For example, `jsonschema.For[Player]()` returns this schema:
4657
4758 {
4859 "properties": {
@@ -58,16 +69,32 @@ For example, calling For on the above Player type results in this schema:
5869 }
5970 }
6071
72+ Use the "jsonschema" struct tag to provide a description for the property:
73+
74+ type Player struct {
75+ Name string `json:"name" jsonschema:"player name"`
76+ Scores []int `json:"scores" jsonschema:"scores of player's games"`
77+ }
78+
6179# Deviations from the specification
6280
63- Regular expressions are processed with Go's regexp package, which differs from ECMA 262,
64- most significantly in not supporting back-references.
81+ Regular expressions are processed with Go's regexp package, which differs
82+ from ECMA 262, most significantly in not supporting back-references.
6583See [this table of differences] for more.
6684
67- The value of the "format" keyword is recorded in the Schema, but is ignored during validation.
85+ The "format" keyword described in [section 7 of the validation spec] is recorded
86+ in the Schema, but is ignored during validation.
6887It does not even produce [annotations].
88+ Use the "pattern" keyword instead: it will work more reliably across JSON Schema
89+ implementations. See [learnjsonschema.com] for more recommendations about "format".
90+
91+ The content keywords described in [section 8 of the validation spec]
92+ are recorded in the schema, but ignored during validation.
6993
7094[JSON Schema specification]: https://json-schema.org
95+ [section 7 of the validation spec]: https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-00#rfc.section.7
96+ [section 8 of the validation spec]: https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-validation-00#rfc.section.8
97+ [learnjsonschema.org]: https://www.learnjsonschema.com/2020-12/format-annotation/format/
7198[this table of differences] https://github.com/dlclark/regexp2?tab=readme-ov-file#compare-regexp-and-regexp2
7299[annotations]: https://json-schema.org/draft/2020-12/json-schema-core#name-annotations
73100*/
0 commit comments