models.go 6.4 KB

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