| 1 | // Copyright 2025 The JSON Schema Go Project Authors. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | /* |
| 6 | Package jsonschema is an implementation of the [JSON Schema specification], |
| 7 | a 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 |
| 9 | data using the draft 2020-12 specification. Validation with other drafts |
| 10 | or custom meta-schemas is not supported. |
| 11 | |
| 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. |
| 26 | |
| 27 | # Validation |
| 28 | |
| 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 |
| 32 | |
| 33 | {"name": "Al", "scores": [90, 80, 100]} |
| 34 | |
| 35 | could be represented as the Go value |
| 36 | |
| 37 | map[string]any{ |
| 38 | "name": "Al", |
| 39 | "scores": []any{90, 80, 100}, |
| 40 | } |
| 41 | |
| 42 | or as a value of this type: |
| 43 | |
| 44 | type Player struct { |
| 45 | Name string `json:"name"` |
| 46 | Scores []int `json:"scores"` |
| 47 | } |
| 48 | |
| 49 | # Inference |
| 50 | |
| 51 | The [For] function returns a [Schema] describing the given Go type. |
| 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: |
| 57 | |
| 58 | { |
| 59 | "properties": { |
| 60 | "name": { |
| 61 | "type": "string" |
| 62 | }, |
| 63 | "scores": { |
| 64 | "type": "array", |
| 65 | "items": {"type": "integer"} |
| 66 | } |
| 67 | "required": ["name", "scores"], |
| 68 | "additionalProperties": {"not": {}} |
| 69 | } |
| 70 | } |
| 71 | |
| 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 | |
| 79 | # Deviations from the specification |
| 80 | |
| 81 | Regular expressions are processed with Go's regexp package, which differs |
| 82 | from ECMA 262, most significantly in not supporting back-references. |
| 83 | See [this table of differences] for more. |
| 84 | |
| 85 | The "format" keyword described in [section 7 of the validation spec] is recorded |
| 86 | in the Schema, but is ignored during validation. |
| 87 | It 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. |
| 93 | |
| 94 | [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.com]: https://www.learnjsonschema.com/2020-12/format-annotation/format/ |
| 98 | [this table of differences]: https://github.com/dlclark/regexp2?tab=readme-ov-file#compare-regexp-and-regexp2 |
| 99 | [annotations]: https://json-schema.org/draft/2020-12/json-schema-core#name-annotations |
| 100 | */ |
| 101 | package jsonschema |