OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/json_schema_validator.h" | 5 #include "chrome/common/json_schema_validator.h" |
6 | 6 |
7 #include <cfloat> | 7 #include <cfloat> |
8 #include <cmath> | 8 #include <cmath> |
9 | 9 |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 return "number"; | 86 return "number"; |
87 } | 87 } |
88 } | 88 } |
89 case Value::TYPE_STRING: | 89 case Value::TYPE_STRING: |
90 return "string"; | 90 return "string"; |
91 case Value::TYPE_DICTIONARY: | 91 case Value::TYPE_DICTIONARY: |
92 return "object"; | 92 return "object"; |
93 case Value::TYPE_LIST: | 93 case Value::TYPE_LIST: |
94 return "array"; | 94 return "array"; |
95 default: | 95 default: |
96 CHECK(false) << "Unexpected value type: " << value->GetType(); | 96 NOTREACHED() << "Unexpected value type: " << value->GetType(); |
97 return ""; | 97 return ""; |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 // static | 101 // static |
102 std::string JSONSchemaValidator::FormatErrorMessage(const std::string& format, | 102 std::string JSONSchemaValidator::FormatErrorMessage(const std::string& format, |
103 const std::string& s1) { | 103 const std::string& s1) { |
104 std::string ret_val = format; | 104 std::string ret_val = format; |
105 ReplaceFirstSubstringAfterOffset(&ret_val, 0, "*", s1); | 105 ReplaceFirstSubstringAfterOffset(&ret_val, 0, "*", s1); |
106 return ret_val; | 106 return ret_val; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 void JSONSchemaValidator::Validate(Value* instance, | 149 void JSONSchemaValidator::Validate(Value* instance, |
150 DictionaryValue* schema, | 150 DictionaryValue* schema, |
151 const std::string& path) { | 151 const std::string& path) { |
152 // If this schema defines itself as reference type, save it in this.types. | 152 // If this schema defines itself as reference type, save it in this.types. |
153 std::string id; | 153 std::string id; |
154 if (schema->GetString("id", &id)) { | 154 if (schema->GetString("id", &id)) { |
155 TypeMap::iterator iter = types_.find(id); | 155 TypeMap::iterator iter = types_.find(id); |
156 if (iter == types_.end()) | 156 if (iter == types_.end()) |
157 types_[id] = schema; | 157 types_[id] = schema; |
158 else | 158 else |
159 CHECK(iter->second == schema); | 159 DCHECK(iter->second == schema); |
160 } | 160 } |
161 | 161 |
162 // If the schema has a $ref property, the instance must validate against | 162 // If the schema has a $ref property, the instance must validate against |
163 // that schema. It must be present in types_ to be referenced. | 163 // that schema. It must be present in types_ to be referenced. |
164 std::string ref; | 164 std::string ref; |
165 if (schema->GetString("$ref", &ref)) { | 165 if (schema->GetString("$ref", &ref)) { |
166 TypeMap::iterator type = types_.find(ref); | 166 TypeMap::iterator type = types_.find(ref); |
167 if (type == types_.end()) { | 167 if (type == types_.end()) { |
168 errors_.push_back( | 168 errors_.push_back( |
169 Error(path, FormatErrorMessage(kUnknownTypeReference, ref))); | 169 Error(path, FormatErrorMessage(kUnknownTypeReference, ref))); |
(...skipping 29 matching lines...) Expand all Loading... |
199 // These casts are safe because of checks in ValidateType(). | 199 // These casts are safe because of checks in ValidateType(). |
200 if (type == "object") | 200 if (type == "object") |
201 ValidateObject(static_cast<DictionaryValue*>(instance), schema, path); | 201 ValidateObject(static_cast<DictionaryValue*>(instance), schema, path); |
202 else if (type == "array") | 202 else if (type == "array") |
203 ValidateArray(static_cast<ListValue*>(instance), schema, path); | 203 ValidateArray(static_cast<ListValue*>(instance), schema, path); |
204 else if (type == "string") | 204 else if (type == "string") |
205 ValidateString(static_cast<StringValue*>(instance), schema, path); | 205 ValidateString(static_cast<StringValue*>(instance), schema, path); |
206 else if (type == "number" || type == "integer") | 206 else if (type == "number" || type == "integer") |
207 ValidateNumber(instance, schema, path); | 207 ValidateNumber(instance, schema, path); |
208 else if (type != "boolean" && type != "null") | 208 else if (type != "boolean" && type != "null") |
209 CHECK(false) << "Unexpected type: " << type; | 209 NOTREACHED() << "Unexpected type: " << type; |
210 } | 210 } |
211 } | 211 } |
212 | 212 |
213 void JSONSchemaValidator::ValidateChoices(Value* instance, | 213 void JSONSchemaValidator::ValidateChoices(Value* instance, |
214 ListValue* choices, | 214 ListValue* choices, |
215 const std::string& path) { | 215 const std::string& path) { |
216 size_t original_num_errors = errors_.size(); | 216 size_t original_num_errors = errors_.size(); |
217 | 217 |
218 for (size_t i = 0; i < choices->GetSize(); ++i) { | 218 for (size_t i = 0; i < choices->GetSize(); ++i) { |
219 DictionaryValue* choice = NULL; | 219 DictionaryValue* choice = NULL; |
(...skipping 30 matching lines...) Expand all Loading... |
250 case Value::TYPE_INTEGER: | 250 case Value::TYPE_INTEGER: |
251 case Value::TYPE_DOUBLE: | 251 case Value::TYPE_DOUBLE: |
252 if (instance->IsType(Value::TYPE_INTEGER) || | 252 if (instance->IsType(Value::TYPE_INTEGER) || |
253 instance->IsType(Value::TYPE_DOUBLE)) { | 253 instance->IsType(Value::TYPE_DOUBLE)) { |
254 if (GetNumberValue(choice) == GetNumberValue(instance)) | 254 if (GetNumberValue(choice) == GetNumberValue(instance)) |
255 return; | 255 return; |
256 } | 256 } |
257 break; | 257 break; |
258 | 258 |
259 default: | 259 default: |
260 CHECK(false) << "Unexpected type in enum: " << choice->GetType(); | 260 NOTREACHED() << "Unexpected type in enum: " << choice->GetType(); |
261 } | 261 } |
262 } | 262 } |
263 | 263 |
264 errors_.push_back(Error(path, kInvalidEnum)); | 264 errors_.push_back(Error(path, kInvalidEnum)); |
265 } | 265 } |
266 | 266 |
267 void JSONSchemaValidator::ValidateObject(DictionaryValue* instance, | 267 void JSONSchemaValidator::ValidateObject(DictionaryValue* instance, |
268 DictionaryValue* schema, | 268 DictionaryValue* schema, |
269 const std::string& path) { | 269 const std::string& path) { |
270 DictionaryValue* properties = NULL; | 270 DictionaryValue* properties = NULL; |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
474 | 474 |
475 if (*additional_properties_schema) { | 475 if (*additional_properties_schema) { |
476 std::string additional_properties_type("any"); | 476 std::string additional_properties_type("any"); |
477 CHECK((*additional_properties_schema)->GetString( | 477 CHECK((*additional_properties_schema)->GetString( |
478 "type", &additional_properties_type)); | 478 "type", &additional_properties_type)); |
479 return additional_properties_type == "any"; | 479 return additional_properties_type == "any"; |
480 } else { | 480 } else { |
481 return default_allow_additional_properties_; | 481 return default_allow_additional_properties_; |
482 } | 482 } |
483 } | 483 } |
OLD | NEW |