| 1 |
// Copyright 2017 Google LLC. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
// Package jsonschema supports the reading, writing, and manipulation |
| 16 |
// of JSON Schemas. |
| 17 |
package jsonschema |
| 18 |
|
| 19 |
import "go.yaml.in/yaml/v3" |
| 20 |
|
| 21 |
// The Schema struct models a JSON Schema and, because schemas are |
| 22 |
// defined hierarchically, contains many references to itself. |
| 23 |
// All fields are pointers and are nil if the associated values |
| 24 |
// are not specified. |
| 25 |
type Schema struct { |
| 26 |
Schema *string // $schema |
| 27 |
ID *string // id keyword used for $ref resolution scope |
| 28 |
Ref *string // $ref, i.e. JSON Pointers |
| 29 |
|
| 30 |
// http://json-schema.org/latest/json-schema-validation.html |
| 31 |
// 5.1. Validation keywords for numeric instances (number and integer) |
| 32 |
MultipleOf *SchemaNumber |
| 33 |
Maximum *SchemaNumber |
| 34 |
ExclusiveMaximum *bool |
| 35 |
Minimum *SchemaNumber |
| 36 |
ExclusiveMinimum *bool |
| 37 |
|
| 38 |
// 5.2. Validation keywords for strings |
| 39 |
MaxLength *int64 |
| 40 |
MinLength *int64 |
| 41 |
Pattern *string |
| 42 |
|
| 43 |
// 5.3. Validation keywords for arrays |
| 44 |
AdditionalItems *SchemaOrBoolean |
| 45 |
Items *SchemaOrSchemaArray |
| 46 |
MaxItems *int64 |
| 47 |
MinItems *int64 |
| 48 |
UniqueItems *bool |
| 49 |
|
| 50 |
// 5.4. Validation keywords for objects |
| 51 |
MaxProperties *int64 |
| 52 |
MinProperties *int64 |
| 53 |
Required *[]string |
| 54 |
AdditionalProperties *SchemaOrBoolean |
| 55 |
Properties *[]*NamedSchema |
| 56 |
PatternProperties *[]*NamedSchema |
| 57 |
Dependencies *[]*NamedSchemaOrStringArray |
| 58 |
|
| 59 |
// 5.5. Validation keywords for any instance type |
| 60 |
Enumeration *[]SchemaEnumValue |
| 61 |
Type *StringOrStringArray |
| 62 |
AllOf *[]*Schema |
| 63 |
AnyOf *[]*Schema |
| 64 |
OneOf *[]*Schema |
| 65 |
Not *Schema |
| 66 |
Definitions *[]*NamedSchema |
| 67 |
|
| 68 |
// 6. Metadata keywords |
| 69 |
Title *string |
| 70 |
Description *string |
| 71 |
Default *yaml.Node |
| 72 |
|
| 73 |
// 7. Semantic validation with "format" |
| 74 |
Format *string |
| 75 |
} |
| 76 |
|
| 77 |
// These helper structs represent "combination" types that generally can |
| 78 |
// have values of one type or another. All are used to represent parts |
| 79 |
// of Schemas. |
| 80 |
|
| 81 |
// SchemaNumber represents a value that can be either an Integer or a Float. |
| 82 |
type SchemaNumber struct { |
| 83 |
Integer *int64 |
| 84 |
Float *float64 |
| 85 |
} |
| 86 |
|
| 87 |
// NewSchemaNumberWithInteger creates and returns a new object |
| 88 |
func NewSchemaNumberWithInteger(i int64) *SchemaNumber { |
| 89 |
result := &SchemaNumber{} |
| 90 |
result.Integer = &i |
| 91 |
return result |
| 92 |
} |
| 93 |
|
| 94 |
// NewSchemaNumberWithFloat creates and returns a new object |
| 95 |
func NewSchemaNumberWithFloat(f float64) *SchemaNumber { |
| 96 |
result := &SchemaNumber{} |
| 97 |
result.Float = &f |
| 98 |
return result |
| 99 |
} |
| 100 |
|
| 101 |
// SchemaOrBoolean represents a value that can be either a Schema or a Boolean. |
| 102 |
type SchemaOrBoolean struct { |
| 103 |
Schema *Schema |
| 104 |
Boolean *bool |
| 105 |
} |
| 106 |
|
| 107 |
// NewSchemaOrBooleanWithSchema creates and returns a new object |
| 108 |
func NewSchemaOrBooleanWithSchema(s *Schema) *SchemaOrBoolean { |
| 109 |
result := &SchemaOrBoolean{} |
| 110 |
result.Schema = s |
| 111 |
return result |
| 112 |
} |
| 113 |
|
| 114 |
// NewSchemaOrBooleanWithBoolean creates and returns a new object |
| 115 |
func NewSchemaOrBooleanWithBoolean(b bool) *SchemaOrBoolean { |
| 116 |
result := &SchemaOrBoolean{} |
| 117 |
result.Boolean = &b |
| 118 |
return result |
| 119 |
} |
| 120 |
|
| 121 |
// StringOrStringArray represents a value that can be either |
| 122 |
// a String or an Array of Strings. |
| 123 |
type StringOrStringArray struct { |
| 124 |
String *string |
| 125 |
StringArray *[]string |
| 126 |
} |
| 127 |
|
| 128 |
// NewStringOrStringArrayWithString creates and returns a new object |
| 129 |
func NewStringOrStringArrayWithString(s string) *StringOrStringArray { |
| 130 |
result := &StringOrStringArray{} |
| 131 |
result.String = &s |
| 132 |
return result |
| 133 |
} |
| 134 |
|
| 135 |
// NewStringOrStringArrayWithStringArray creates and returns a new object |
| 136 |
func NewStringOrStringArrayWithStringArray(a []string) *StringOrStringArray { |
| 137 |
result := &StringOrStringArray{} |
| 138 |
result.StringArray = &a |
| 139 |
return result |
| 140 |
} |
| 141 |
|
| 142 |
// SchemaOrStringArray represents a value that can be either |
| 143 |
// a Schema or an Array of Strings. |
| 144 |
type SchemaOrStringArray struct { |
| 145 |
Schema *Schema |
| 146 |
StringArray *[]string |
| 147 |
} |
| 148 |
|
| 149 |
// SchemaOrSchemaArray represents a value that can be either |
| 150 |
// a Schema or an Array of Schemas. |
| 151 |
type SchemaOrSchemaArray struct { |
| 152 |
Schema *Schema |
| 153 |
SchemaArray *[]*Schema |
| 154 |
} |
| 155 |
|
| 156 |
// NewSchemaOrSchemaArrayWithSchema creates and returns a new object |
| 157 |
func NewSchemaOrSchemaArrayWithSchema(s *Schema) *SchemaOrSchemaArray { |
| 158 |
result := &SchemaOrSchemaArray{} |
| 159 |
result.Schema = s |
| 160 |
return result |
| 161 |
} |
| 162 |
|
| 163 |
// NewSchemaOrSchemaArrayWithSchemaArray creates and returns a new object |
| 164 |
func NewSchemaOrSchemaArrayWithSchemaArray(a []*Schema) *SchemaOrSchemaArray { |
| 165 |
result := &SchemaOrSchemaArray{} |
| 166 |
result.SchemaArray = &a |
| 167 |
return result |
| 168 |
} |
| 169 |
|
| 170 |
// SchemaEnumValue represents a value that can be part of an |
| 171 |
// enumeration in a Schema. |
| 172 |
type SchemaEnumValue struct { |
| 173 |
String *string |
| 174 |
Bool *bool |
| 175 |
} |
| 176 |
|
| 177 |
// NamedSchema is a name-value pair that is used to emulate maps |
| 178 |
// with ordered keys. |
| 179 |
type NamedSchema struct { |
| 180 |
Name string |
| 181 |
Value *Schema |
| 182 |
} |
| 183 |
|
| 184 |
// NewNamedSchema creates and returns a new object |
| 185 |
func NewNamedSchema(name string, value *Schema) *NamedSchema { |
| 186 |
return &NamedSchema{Name: name, Value: value} |
| 187 |
} |
| 188 |
|
| 189 |
// NamedSchemaOrStringArray is a name-value pair that is used |
| 190 |
// to emulate maps with ordered keys. |
| 191 |
type NamedSchemaOrStringArray struct { |
| 192 |
Name string |
| 193 |
Value *SchemaOrStringArray |
| 194 |
} |
| 195 |
|
| 196 |
// Access named subschemas by name |
| 197 |
|
| 198 |
func namedSchemaArrayElementWithName(array *[]*NamedSchema, name string) *Schema { |
| 199 |
if array == nil { |
| 200 |
return nil |
| 201 |
} |
| 202 |
for _, pair := range *array { |
| 203 |
if pair.Name == name { |
| 204 |
return pair.Value |
| 205 |
} |
| 206 |
} |
| 207 |
return nil |
| 208 |
} |
| 209 |
|
| 210 |
// PropertyWithName returns the selected element. |
| 211 |
func (s *Schema) PropertyWithName(name string) *Schema { |
| 212 |
return namedSchemaArrayElementWithName(s.Properties, name) |
| 213 |
} |
| 214 |
|
| 215 |
// PatternPropertyWithName returns the selected element. |
| 216 |
func (s *Schema) PatternPropertyWithName(name string) *Schema { |
| 217 |
return namedSchemaArrayElementWithName(s.PatternProperties, name) |
| 218 |
} |
| 219 |
|
| 220 |
// DefinitionWithName returns the selected element. |
| 221 |
func (s *Schema) DefinitionWithName(name string) *Schema { |
| 222 |
return namedSchemaArrayElementWithName(s.Definitions, name) |
| 223 |
} |
| 224 |
|
| 225 |
// AddProperty adds a named property. |
| 226 |
func (s *Schema) AddProperty(name string, property *Schema) { |
| 227 |
*s.Properties = append(*s.Properties, NewNamedSchema(name, property)) |
| 228 |
} |