|
| 1 | +package jsonschema |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "reflect" |
| 7 | +) |
| 8 | + |
| 9 | +// Items MUST be either a valid JSON Schema or an array of valid JSON Schemas. |
| 10 | +// This keyword determines how child instances validate for arrays, and does not directly validate the |
| 11 | +// immediate instance itself. |
| 12 | +// * If "items" is a schema, validation succeeds if all elements in the array successfully validate |
| 13 | +// against that schema. |
| 14 | +// * If "items" is an array of schemas, validation succeeds if each element of the instance validates |
| 15 | +// against the schema at the same position, if any. |
| 16 | +// * Omitting this keyword has the same behavior as an empty schema. |
| 17 | +type Items struct { |
| 18 | + // need to track weather user specficied a singl object or arry |
| 19 | + // b/c it affects additionalItems validation semantics |
| 20 | + single bool |
| 21 | + Schemas []*Schema |
| 22 | +} |
| 23 | + |
| 24 | +// Validate implements the Validator interface for Items |
| 25 | +func (it Items) Validate(data interface{}) error { |
| 26 | + if arr, ok := data.([]interface{}); ok { |
| 27 | + if it.single { |
| 28 | + for i, elem := range arr { |
| 29 | + if err := it.Schemas[0].Validate(elem); err != nil { |
| 30 | + return fmt.Errorf("element %d: %s", i, err.Error()) |
| 31 | + } |
| 32 | + } |
| 33 | + } else { |
| 34 | + for i, vs := range it.Schemas { |
| 35 | + if i < len(arr) { |
| 36 | + if err := vs.Validate(arr[i]); err != nil { |
| 37 | + return fmt.Errorf("element %d: %s", i, err.Error()) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +// UnmarshalJSON implements the json.Unmarshaler interface for Items |
| 47 | +func (it *Items) UnmarshalJSON(data []byte) error { |
| 48 | + s := &Schema{} |
| 49 | + if err := json.Unmarshal(data, s); err == nil { |
| 50 | + *it = Items{single: true, Schemas: []*Schema{s}} |
| 51 | + return nil |
| 52 | + } |
| 53 | + ss := []*Schema{} |
| 54 | + if err := json.Unmarshal(data, &ss); err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + *it = Items{Schemas: ss} |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +// MarshalJSON implements the json.Marshaler interface for Items |
| 62 | +func (it Items) MarshalJSON() ([]byte, error) { |
| 63 | + if it.single { |
| 64 | + return json.Marshal(it.Schemas[0]) |
| 65 | + } |
| 66 | + return json.Marshal([]*Schema(it.Schemas)) |
| 67 | +} |
| 68 | + |
| 69 | +// AdditionalItems determines how child instances validate for arrays, and does not directly validate the immediate |
| 70 | +// instance itself. |
| 71 | +// If "items" is an array of schemas, validation succeeds if every instance element at a position greater than |
| 72 | +// the size of "items" validates against "additionalItems". |
| 73 | +// Otherwise, "additionalItems" MUST be ignored, as the "items" schema (possibly the default value of an empty schema) is applied to all elements. |
| 74 | +// Omitting this keyword has the same behavior as an empty schema. |
| 75 | +type AdditionalItems struct { |
| 76 | + startIndex int |
| 77 | + Schema *Schema |
| 78 | +} |
| 79 | + |
| 80 | +// Validate implements the Validator interface for AdditionalItems |
| 81 | +func (a *AdditionalItems) Validate(data interface{}) error { |
| 82 | + if a.startIndex >= 0 { |
| 83 | + if arr, ok := data.([]interface{}); ok { |
| 84 | + for i, elem := range arr { |
| 85 | + if i < a.startIndex { |
| 86 | + continue |
| 87 | + } |
| 88 | + if err := a.Schema.Validate(elem); err != nil { |
| 89 | + return fmt.Errorf("element %d: %s", i, err.Error()) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +// UnmarshalJSON implements the json.Unmarshaler interface for AdditionalItems |
| 98 | +func (a *AdditionalItems) UnmarshalJSON(data []byte) error { |
| 99 | + sch := &Schema{} |
| 100 | + if err := json.Unmarshal(data, sch); err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + // begin with -1 as default index to prevent AdditionalItems from evaluating |
| 104 | + // unless startIndex is explicitly set |
| 105 | + *a = AdditionalItems{startIndex: -1, Schema: sch} |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +// MaxItems MUST be a non-negative integer. |
| 110 | +// An array instance is valid against "maxItems" if its size is less than, or equal to, the value of this keyword. |
| 111 | +type MaxItems int |
| 112 | + |
| 113 | +// Validate implements the Validator interface for MaxItems |
| 114 | +func (m MaxItems) Validate(data interface{}) error { |
| 115 | + if arr, ok := data.([]interface{}); ok { |
| 116 | + if len(arr) > int(m) { |
| 117 | + return fmt.Errorf("%d array items exceeds %d max", len(arr), m) |
| 118 | + } |
| 119 | + } |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +// MinItems MUST be a non-negative integer. |
| 124 | +// An array instance is valid against "minItems" if its size is greater than, or equal to, the value of this keyword. |
| 125 | +// Omitting this keyword has the same behavior as a value of 0. |
| 126 | +type MinItems int |
| 127 | + |
| 128 | +// Validate implements the Validator interface for MinItems |
| 129 | +func (m MinItems) Validate(data interface{}) error { |
| 130 | + if arr, ok := data.([]interface{}); ok { |
| 131 | + if len(arr) < int(m) { |
| 132 | + return fmt.Errorf("%d array items below %d minimum", len(arr), m) |
| 133 | + } |
| 134 | + } |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +// UniqueItems requires array instance elements be unique |
| 139 | +// If this keyword has boolean value false, the instance validates successfully. If it has |
| 140 | +// boolean value true, the instance validates successfully if all of its elements are unique. |
| 141 | +// Omitting this keyword has the same behavior as a value of false. |
| 142 | +type UniqueItems bool |
| 143 | + |
| 144 | +// Validate implements the Validator interface for UniqueItems |
| 145 | +func (u *UniqueItems) Validate(data interface{}) error { |
| 146 | + if arr, ok := data.([]interface{}); ok { |
| 147 | + found := []interface{}{} |
| 148 | + for _, elem := range arr { |
| 149 | + for _, f := range found { |
| 150 | + if reflect.DeepEqual(f, elem) { |
| 151 | + return fmt.Errorf("arry must be unique: %v", arr) |
| 152 | + } |
| 153 | + } |
| 154 | + found = append(found, elem) |
| 155 | + } |
| 156 | + } |
| 157 | + return nil |
| 158 | +} |
| 159 | + |
| 160 | +// Contains validates that an array instance is valid against "contains" if at |
| 161 | +// least one of its elements is valid against the given schema. |
| 162 | +type Contains Schema |
| 163 | + |
| 164 | +// Validate implements the Validator interface for Contains |
| 165 | +func (c *Contains) Validate(data interface{}) error { |
| 166 | + v := Schema(*c) |
| 167 | + if arr, ok := data.([]interface{}); ok { |
| 168 | + for _, elem := range arr { |
| 169 | + if err := v.Validate(elem); err == nil { |
| 170 | + return nil |
| 171 | + } |
| 172 | + } |
| 173 | + return fmt.Errorf("expected %v to contain at least one of: %s", data, c) |
| 174 | + } |
| 175 | + return nil |
| 176 | +} |
| 177 | + |
| 178 | +// UnmarshalJSON implements the json.Unmarshaler interface for Contains |
| 179 | +func (c *Contains) UnmarshalJSON(data []byte) error { |
| 180 | + var sch Schema |
| 181 | + if err := json.Unmarshal(data, &sch); err != nil { |
| 182 | + return err |
| 183 | + } |
| 184 | + *c = Contains(sch) |
| 185 | + return nil |
| 186 | +} |
0 commit comments