schema.go 5.7 KB

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