ValidateJSONObject validates a JSON Schema object payload.
(field string, raw json.RawMessage, required bool)
| 174 | |
| 175 | // ValidateJSONObject validates a JSON Schema object payload. |
| 176 | func ValidateJSONObject(field string, raw json.RawMessage, required bool) error { |
| 177 | if len(raw) == 0 { |
| 178 | if required { |
| 179 | return NewValidationError(field, ReasonSchemaInvalid, "schema object is required") |
| 180 | } |
| 181 | return nil |
| 182 | } |
| 183 | |
| 184 | var decoded map[string]json.RawMessage |
| 185 | if err := json.Unmarshal(raw, &decoded); err != nil { |
| 186 | return NewValidationError(field, ReasonSchemaInvalid, err.Error()) |
| 187 | } |
| 188 | if decoded == nil { |
| 189 | if required { |
| 190 | return NewValidationError(field, ReasonSchemaInvalid, "schema must be a JSON object") |
| 191 | } |
| 192 | return nil |
| 193 | } |
| 194 | if err := validateJSONSchemaDocument("$", decoded); err != nil { |
| 195 | return NewValidationError(field, ReasonSchemaInvalid, err.Error()) |
| 196 | } |
| 197 | return nil |
| 198 | } |