schema.go

 1package jsonschema
 2
 3import (
 4	"encoding/json"
 5
 6	orderedmap "github.com/wk8/go-ordered-map/v2"
 7)
 8
 9// Version is the JSON Schema version.
10var Version = "https://json-schema.org/draft/2020-12/schema"
11
12// Schema represents a JSON Schema object type.
13// RFC draft-bhutton-json-schema-00 section 4.3
14type Schema struct {
15	// RFC draft-bhutton-json-schema-00
16	Version     string      `json:"$schema,omitempty"`     // section 8.1.1
17	ID          ID          `json:"$id,omitempty"`         // section 8.2.1
18	Anchor      string      `json:"$anchor,omitempty"`     // section 8.2.2
19	Ref         string      `json:"$ref,omitempty"`        // section 8.2.3.1
20	DynamicRef  string      `json:"$dynamicRef,omitempty"` // section 8.2.3.2
21	Definitions Definitions `json:"$defs,omitempty"`       // section 8.2.4
22	Comments    string      `json:"$comment,omitempty"`    // section 8.3
23	// RFC draft-bhutton-json-schema-00 section 10.2.1 (Sub-schemas with logic)
24	AllOf []*Schema `json:"allOf,omitempty"` // section 10.2.1.1
25	AnyOf []*Schema `json:"anyOf,omitempty"` // section 10.2.1.2
26	OneOf []*Schema `json:"oneOf,omitempty"` // section 10.2.1.3
27	Not   *Schema   `json:"not,omitempty"`   // section 10.2.1.4
28	// RFC draft-bhutton-json-schema-00 section 10.2.2 (Apply sub-schemas conditionally)
29	If               *Schema            `json:"if,omitempty"`               // section 10.2.2.1
30	Then             *Schema            `json:"then,omitempty"`             // section 10.2.2.2
31	Else             *Schema            `json:"else,omitempty"`             // section 10.2.2.3
32	DependentSchemas map[string]*Schema `json:"dependentSchemas,omitempty"` // section 10.2.2.4
33	// RFC draft-bhutton-json-schema-00 section 10.3.1 (arrays)
34	PrefixItems []*Schema `json:"prefixItems,omitempty"` // section 10.3.1.1
35	Items       *Schema   `json:"items,omitempty"`       // section 10.3.1.2  (replaces additionalItems)
36	Contains    *Schema   `json:"contains,omitempty"`    // section 10.3.1.3
37	// RFC draft-bhutton-json-schema-00 section 10.3.2 (sub-schemas)
38	Properties           *orderedmap.OrderedMap[string, *Schema] `json:"properties,omitempty"`           // section 10.3.2.1
39	PatternProperties    map[string]*Schema                      `json:"patternProperties,omitempty"`    // section 10.3.2.2
40	AdditionalProperties *Schema                                 `json:"additionalProperties,omitempty"` // section 10.3.2.3
41	PropertyNames        *Schema                                 `json:"propertyNames,omitempty"`        // section 10.3.2.4
42	// RFC draft-bhutton-json-schema-validation-00, section 6
43	Type              string              `json:"type,omitempty"`              // section 6.1.1
44	Enum              []any               `json:"enum,omitempty"`              // section 6.1.2
45	Const             any                 `json:"const,omitempty"`             // section 6.1.3
46	MultipleOf        json.Number         `json:"multipleOf,omitempty"`        // section 6.2.1
47	Maximum           json.Number         `json:"maximum,omitempty"`           // section 6.2.2
48	ExclusiveMaximum  json.Number         `json:"exclusiveMaximum,omitempty"`  // section 6.2.3
49	Minimum           json.Number         `json:"minimum,omitempty"`           // section 6.2.4
50	ExclusiveMinimum  json.Number         `json:"exclusiveMinimum,omitempty"`  // section 6.2.5
51	MaxLength         *uint64             `json:"maxLength,omitempty"`         // section 6.3.1
52	MinLength         *uint64             `json:"minLength,omitempty"`         // section 6.3.2
53	Pattern           string              `json:"pattern,omitempty"`           // section 6.3.3
54	MaxItems          *uint64             `json:"maxItems,omitempty"`          // section 6.4.1
55	MinItems          *uint64             `json:"minItems,omitempty"`          // section 6.4.2
56	UniqueItems       bool                `json:"uniqueItems,omitempty"`       // section 6.4.3
57	MaxContains       *uint64             `json:"maxContains,omitempty"`       // section 6.4.4
58	MinContains       *uint64             `json:"minContains,omitempty"`       // section 6.4.5
59	MaxProperties     *uint64             `json:"maxProperties,omitempty"`     // section 6.5.1
60	MinProperties     *uint64             `json:"minProperties,omitempty"`     // section 6.5.2
61	Required          []string            `json:"required,omitempty"`          // section 6.5.3
62	DependentRequired map[string][]string `json:"dependentRequired,omitempty"` // section 6.5.4
63	// RFC draft-bhutton-json-schema-validation-00, section 7
64	Format string `json:"format,omitempty"`
65	// RFC draft-bhutton-json-schema-validation-00, section 8
66	ContentEncoding  string  `json:"contentEncoding,omitempty"`  // section 8.3
67	ContentMediaType string  `json:"contentMediaType,omitempty"` // section 8.4
68	ContentSchema    *Schema `json:"contentSchema,omitempty"`    // section 8.5
69	// RFC draft-bhutton-json-schema-validation-00, section 9
70	Title       string `json:"title,omitempty"`       // section 9.1
71	Description string `json:"description,omitempty"` // section 9.1
72	Default     any    `json:"default,omitempty"`     // section 9.2
73	Deprecated  bool   `json:"deprecated,omitempty"`  // section 9.3
74	ReadOnly    bool   `json:"readOnly,omitempty"`    // section 9.4
75	WriteOnly   bool   `json:"writeOnly,omitempty"`   // section 9.4
76	Examples    []any  `json:"examples,omitempty"`    // section 9.5
77
78	Extras map[string]any `json:"-"`
79
80	// Special boolean representation of the Schema - section 4.3.2
81	boolean *bool
82}
83
84var (
85	// TrueSchema defines a schema with a true value
86	TrueSchema = &Schema{boolean: &[]bool{true}[0]}
87	// FalseSchema defines a schema with a false value
88	FalseSchema = &Schema{boolean: &[]bool{false}[0]}
89)
90
91// Definitions hold schema definitions.
92// http://json-schema.org/latest/json-schema-validation.html#rfc.section.5.26
93// RFC draft-wright-json-schema-validation-00, section 5.26
94type Definitions map[string]*Schema