models.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. // Package jsonschema supports the reading, writing, and manipulation
  15. // of JSON Schemas.
  16. package jsonschema
  17. import "gopkg.in/yaml.v3"
  18. // The Schema struct models a JSON Schema and, because schemas are
  19. // defined hierarchically, contains many references to itself.
  20. // All fields are pointers and are nil if the associated values
  21. // are not specified.
  22. type Schema struct {
  23. Schema *string // $schema
  24. ID *string // id keyword used for $ref resolution scope
  25. Ref *string // $ref, i.e. JSON Pointers
  26. // http://json-schema.org/latest/json-schema-validation.html
  27. // 5.1. Validation keywords for numeric instances (number and integer)
  28. MultipleOf *SchemaNumber
  29. Maximum *SchemaNumber
  30. ExclusiveMaximum *bool
  31. Minimum *SchemaNumber
  32. ExclusiveMinimum *bool
  33. // 5.2. Validation keywords for strings
  34. MaxLength *int64
  35. MinLength *int64
  36. Pattern *string
  37. // 5.3. Validation keywords for arrays
  38. AdditionalItems *SchemaOrBoolean
  39. Items *SchemaOrSchemaArray
  40. MaxItems *int64
  41. MinItems *int64
  42. UniqueItems *bool
  43. // 5.4. Validation keywords for objects
  44. MaxProperties *int64
  45. MinProperties *int64
  46. Required *[]string
  47. AdditionalProperties *SchemaOrBoolean
  48. Properties *[]*NamedSchema
  49. PatternProperties *[]*NamedSchema
  50. Dependencies *[]*NamedSchemaOrStringArray
  51. // 5.5. Validation keywords for any instance type
  52. Enumeration *[]SchemaEnumValue
  53. Type *StringOrStringArray
  54. AllOf *[]*Schema
  55. AnyOf *[]*Schema
  56. OneOf *[]*Schema
  57. Not *Schema
  58. Definitions *[]*NamedSchema
  59. // 6. Metadata keywords
  60. Title *string
  61. Description *string
  62. Default *yaml.Node
  63. // 7. Semantic validation with "format"
  64. Format *string
  65. }
  66. // These helper structs represent "combination" types that generally can
  67. // have values of one type or another. All are used to represent parts
  68. // of Schemas.
  69. // SchemaNumber represents a value that can be either an Integer or a Float.
  70. type SchemaNumber struct {
  71. Integer *int64
  72. Float *float64
  73. }
  74. // NewSchemaNumberWithInteger creates and returns a new object
  75. func NewSchemaNumberWithInteger(i int64) *SchemaNumber {
  76. result := &SchemaNumber{}
  77. result.Integer = &i
  78. return result
  79. }
  80. // NewSchemaNumberWithFloat creates and returns a new object
  81. func NewSchemaNumberWithFloat(f float64) *SchemaNumber {
  82. result := &SchemaNumber{}
  83. result.Float = &f
  84. return result
  85. }
  86. // SchemaOrBoolean represents a value that can be either a Schema or a Boolean.
  87. type SchemaOrBoolean struct {
  88. Schema *Schema
  89. Boolean *bool
  90. }
  91. // NewSchemaOrBooleanWithSchema creates and returns a new object
  92. func NewSchemaOrBooleanWithSchema(s *Schema) *SchemaOrBoolean {
  93. result := &SchemaOrBoolean{}
  94. result.Schema = s
  95. return result
  96. }
  97. // NewSchemaOrBooleanWithBoolean creates and returns a new object
  98. func NewSchemaOrBooleanWithBoolean(b bool) *SchemaOrBoolean {
  99. result := &SchemaOrBoolean{}
  100. result.Boolean = &b
  101. return result
  102. }
  103. // StringOrStringArray represents a value that can be either
  104. // a String or an Array of Strings.
  105. type StringOrStringArray struct {
  106. String *string
  107. StringArray *[]string
  108. }
  109. // NewStringOrStringArrayWithString creates and returns a new object
  110. func NewStringOrStringArrayWithString(s string) *StringOrStringArray {
  111. result := &StringOrStringArray{}
  112. result.String = &s
  113. return result
  114. }
  115. // NewStringOrStringArrayWithStringArray creates and returns a new object
  116. func NewStringOrStringArrayWithStringArray(a []string) *StringOrStringArray {
  117. result := &StringOrStringArray{}
  118. result.StringArray = &a
  119. return result
  120. }
  121. // SchemaOrStringArray represents a value that can be either
  122. // a Schema or an Array of Strings.
  123. type SchemaOrStringArray struct {
  124. Schema *Schema
  125. StringArray *[]string
  126. }
  127. // SchemaOrSchemaArray represents a value that can be either
  128. // a Schema or an Array of Schemas.
  129. type SchemaOrSchemaArray struct {
  130. Schema *Schema
  131. SchemaArray *[]*Schema
  132. }
  133. // NewSchemaOrSchemaArrayWithSchema creates and returns a new object
  134. func NewSchemaOrSchemaArrayWithSchema(s *Schema) *SchemaOrSchemaArray {
  135. result := &SchemaOrSchemaArray{}
  136. result.Schema = s
  137. return result
  138. }
  139. // NewSchemaOrSchemaArrayWithSchemaArray creates and returns a new object
  140. func NewSchemaOrSchemaArrayWithSchemaArray(a []*Schema) *SchemaOrSchemaArray {
  141. result := &SchemaOrSchemaArray{}
  142. result.SchemaArray = &a
  143. return result
  144. }
  145. // SchemaEnumValue represents a value that can be part of an
  146. // enumeration in a Schema.
  147. type SchemaEnumValue struct {
  148. String *string
  149. Bool *bool
  150. }
  151. // NamedSchema is a name-value pair that is used to emulate maps
  152. // with ordered keys.
  153. type NamedSchema struct {
  154. Name string
  155. Value *Schema
  156. }
  157. // NewNamedSchema creates and returns a new object
  158. func NewNamedSchema(name string, value *Schema) *NamedSchema {
  159. return &NamedSchema{Name: name, Value: value}
  160. }
  161. // NamedSchemaOrStringArray is a name-value pair that is used
  162. // to emulate maps with ordered keys.
  163. type NamedSchemaOrStringArray struct {
  164. Name string
  165. Value *SchemaOrStringArray
  166. }
  167. // Access named subschemas by name
  168. func namedSchemaArrayElementWithName(array *[]*NamedSchema, name string) *Schema {
  169. if array == nil {
  170. return nil
  171. }
  172. for _, pair := range *array {
  173. if pair.Name == name {
  174. return pair.Value
  175. }
  176. }
  177. return nil
  178. }
  179. // PropertyWithName returns the selected element.
  180. func (s *Schema) PropertyWithName(name string) *Schema {
  181. return namedSchemaArrayElementWithName(s.Properties, name)
  182. }
  183. // PatternPropertyWithName returns the selected element.
  184. func (s *Schema) PatternPropertyWithName(name string) *Schema {
  185. return namedSchemaArrayElementWithName(s.PatternProperties, name)
  186. }
  187. // DefinitionWithName returns the selected element.
  188. func (s *Schema) DefinitionWithName(name string) *Schema {
  189. return namedSchemaArrayElementWithName(s.Definitions, name)
  190. }
  191. // AddProperty adds a named property.
  192. func (s *Schema) AddProperty(name string, property *Schema) {
  193. *s.Properties = append(*s.Properties, NewNamedSchema(name, property))
  194. }