| 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" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/common/json_schema_constants.h" |
| 13 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
| 14 | 15 |
| 16 using namespace json_schema_constants; |
| 17 |
| 15 namespace { | 18 namespace { |
| 16 | 19 |
| 17 double GetNumberValue(Value* value) { | 20 double GetNumberValue(Value* value) { |
| 18 double result = 0; | 21 double result = 0; |
| 19 CHECK(value->GetAsDouble(&result)) | 22 CHECK(value->GetAsDouble(&result)) |
| 20 << "Unexpected value type: " << value->GetType(); | 23 << "Unexpected value type: " << value->GetType(); |
| 21 return result; | 24 return result; |
| 22 } | 25 } |
| 23 | 26 |
| 24 } // namespace | 27 } // namespace |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 const char JSONSchemaValidator::kNumberMaximum[] = | 67 const char JSONSchemaValidator::kNumberMaximum[] = |
| 65 "Value must not be greater than *."; | 68 "Value must not be greater than *."; |
| 66 const char JSONSchemaValidator::kInvalidType[] = | 69 const char JSONSchemaValidator::kInvalidType[] = |
| 67 "Expected '*' but got '*'."; | 70 "Expected '*' but got '*'."; |
| 68 | 71 |
| 69 | 72 |
| 70 // static | 73 // static |
| 71 std::string JSONSchemaValidator::GetJSONSchemaType(Value* value) { | 74 std::string JSONSchemaValidator::GetJSONSchemaType(Value* value) { |
| 72 switch (value->GetType()) { | 75 switch (value->GetType()) { |
| 73 case Value::TYPE_NULL: | 76 case Value::TYPE_NULL: |
| 74 return "null"; | 77 return kNull; |
| 75 case Value::TYPE_BOOLEAN: | 78 case Value::TYPE_BOOLEAN: |
| 76 return "boolean"; | 79 return kBoolean; |
| 77 case Value::TYPE_INTEGER: | 80 case Value::TYPE_INTEGER: |
| 78 return "integer"; | 81 return kInteger; |
| 79 case Value::TYPE_DOUBLE: { | 82 case Value::TYPE_DOUBLE: { |
| 80 double double_value = 0; | 83 double double_value = 0; |
| 81 value->GetAsDouble(&double_value); | 84 value->GetAsDouble(&double_value); |
| 82 if (std::abs(double_value) <= std::pow(2.0, DBL_MANT_DIG) && | 85 if (std::abs(double_value) <= std::pow(2.0, DBL_MANT_DIG) && |
| 83 double_value == floor(double_value)) { | 86 double_value == floor(double_value)) { |
| 84 return "integer"; | 87 return kInteger; |
| 85 } else { | 88 } else { |
| 86 return "number"; | 89 return kNumber; |
| 87 } | 90 } |
| 88 } | 91 } |
| 89 case Value::TYPE_STRING: | 92 case Value::TYPE_STRING: |
| 90 return "string"; | 93 return kString; |
| 91 case Value::TYPE_DICTIONARY: | 94 case Value::TYPE_DICTIONARY: |
| 92 return "object"; | 95 return kObject; |
| 93 case Value::TYPE_LIST: | 96 case Value::TYPE_LIST: |
| 94 return "array"; | 97 return kArray; |
| 95 default: | 98 default: |
| 96 NOTREACHED() << "Unexpected value type: " << value->GetType(); | 99 NOTREACHED() << "Unexpected value type: " << value->GetType(); |
| 97 return ""; | 100 return ""; |
| 98 } | 101 } |
| 99 } | 102 } |
| 100 | 103 |
| 101 // static | 104 // static |
| 102 std::string JSONSchemaValidator::FormatErrorMessage(const std::string& format, | 105 std::string JSONSchemaValidator::FormatErrorMessage(const std::string& format, |
| 103 const std::string& s1) { | 106 const std::string& s1) { |
| 104 std::string ret_val = format; | 107 std::string ret_val = format; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 124 ListValue* types) | 127 ListValue* types) |
| 125 : schema_root_(schema), default_allow_additional_properties_(false) { | 128 : schema_root_(schema), default_allow_additional_properties_(false) { |
| 126 if (!types) | 129 if (!types) |
| 127 return; | 130 return; |
| 128 | 131 |
| 129 for (size_t i = 0; i < types->GetSize(); ++i) { | 132 for (size_t i = 0; i < types->GetSize(); ++i) { |
| 130 DictionaryValue* type = NULL; | 133 DictionaryValue* type = NULL; |
| 131 CHECK(types->GetDictionary(i, &type)); | 134 CHECK(types->GetDictionary(i, &type)); |
| 132 | 135 |
| 133 std::string id; | 136 std::string id; |
| 134 CHECK(type->GetString("id", &id)); | 137 CHECK(type->GetString(kId, &id)); |
| 135 | 138 |
| 136 CHECK(types_.find(id) == types_.end()); | 139 CHECK(types_.find(id) == types_.end()); |
| 137 types_[id] = type; | 140 types_[id] = type; |
| 138 } | 141 } |
| 139 } | 142 } |
| 140 | 143 |
| 141 JSONSchemaValidator::~JSONSchemaValidator() {} | 144 JSONSchemaValidator::~JSONSchemaValidator() {} |
| 142 | 145 |
| 143 bool JSONSchemaValidator::Validate(Value* instance) { | 146 bool JSONSchemaValidator::Validate(Value* instance) { |
| 144 errors_.clear(); | 147 errors_.clear(); |
| 145 Validate(instance, schema_root_, ""); | 148 Validate(instance, schema_root_, ""); |
| 146 return errors_.empty(); | 149 return errors_.empty(); |
| 147 } | 150 } |
| 148 | 151 |
| 149 void JSONSchemaValidator::Validate(Value* instance, | 152 void JSONSchemaValidator::Validate(Value* instance, |
| 150 DictionaryValue* schema, | 153 DictionaryValue* schema, |
| 151 const std::string& path) { | 154 const std::string& path) { |
| 152 // If this schema defines itself as reference type, save it in this.types. | 155 // If this schema defines itself as reference type, save it in this.types. |
| 153 std::string id; | 156 std::string id; |
| 154 if (schema->GetString("id", &id)) { | 157 if (schema->GetString(kId, &id)) { |
| 155 TypeMap::iterator iter = types_.find(id); | 158 TypeMap::iterator iter = types_.find(id); |
| 156 if (iter == types_.end()) | 159 if (iter == types_.end()) |
| 157 types_[id] = schema; | 160 types_[id] = schema; |
| 158 else | 161 else |
| 159 DCHECK(iter->second == schema); | 162 DCHECK(iter->second == schema); |
| 160 } | 163 } |
| 161 | 164 |
| 162 // If the schema has a $ref property, the instance must validate against | 165 // If the schema has a $ref property, the instance must validate against |
| 163 // that schema. It must be present in types_ to be referenced. | 166 // that schema. It must be present in types_ to be referenced. |
| 164 std::string ref; | 167 std::string ref; |
| 165 if (schema->GetString("$ref", &ref)) { | 168 if (schema->GetString("$ref", &ref)) { |
| 166 TypeMap::iterator type = types_.find(ref); | 169 TypeMap::iterator type = types_.find(ref); |
| 167 if (type == types_.end()) { | 170 if (type == types_.end()) { |
| 168 errors_.push_back( | 171 errors_.push_back( |
| 169 Error(path, FormatErrorMessage(kUnknownTypeReference, ref))); | 172 Error(path, FormatErrorMessage(kUnknownTypeReference, ref))); |
| 170 } else { | 173 } else { |
| 171 Validate(instance, type->second, path); | 174 Validate(instance, type->second, path); |
| 172 } | 175 } |
| 173 return; | 176 return; |
| 174 } | 177 } |
| 175 | 178 |
| 176 // If the schema has a choices property, the instance must validate against at | 179 // If the schema has a choices property, the instance must validate against at |
| 177 // least one of the items in that array. | 180 // least one of the items in that array. |
| 178 ListValue* choices = NULL; | 181 ListValue* choices = NULL; |
| 179 if (schema->GetList("choices", &choices)) { | 182 if (schema->GetList(kChoices, &choices)) { |
| 180 ValidateChoices(instance, choices, path); | 183 ValidateChoices(instance, choices, path); |
| 181 return; | 184 return; |
| 182 } | 185 } |
| 183 | 186 |
| 184 // If the schema has an enum property, the instance must be one of those | 187 // If the schema has an enum property, the instance must be one of those |
| 185 // values. | 188 // values. |
| 186 ListValue* enumeration = NULL; | 189 ListValue* enumeration = NULL; |
| 187 if (schema->GetList("enum", &enumeration)) { | 190 if (schema->GetList(kEnum, &enumeration)) { |
| 188 ValidateEnum(instance, enumeration, path); | 191 ValidateEnum(instance, enumeration, path); |
| 189 return; | 192 return; |
| 190 } | 193 } |
| 191 | 194 |
| 192 std::string type; | 195 std::string type; |
| 193 schema->GetString("type", &type); | 196 schema->GetString(kType, &type); |
| 194 CHECK(!type.empty()); | 197 CHECK(!type.empty()); |
| 195 if (type != "any") { | 198 if (type != kAny) { |
| 196 if (!ValidateType(instance, type, path)) | 199 if (!ValidateType(instance, type, path)) |
| 197 return; | 200 return; |
| 198 | 201 |
| 199 // These casts are safe because of checks in ValidateType(). | 202 // These casts are safe because of checks in ValidateType(). |
| 200 if (type == "object") | 203 if (type == kObject) |
| 201 ValidateObject(static_cast<DictionaryValue*>(instance), schema, path); | 204 ValidateObject(static_cast<DictionaryValue*>(instance), schema, path); |
| 202 else if (type == "array") | 205 else if (type == kArray) |
| 203 ValidateArray(static_cast<ListValue*>(instance), schema, path); | 206 ValidateArray(static_cast<ListValue*>(instance), schema, path); |
| 204 else if (type == "string") | 207 else if (type == kString) |
| 205 ValidateString(static_cast<StringValue*>(instance), schema, path); | 208 ValidateString(static_cast<StringValue*>(instance), schema, path); |
| 206 else if (type == "number" || type == "integer") | 209 else if (type == kNumber || type == kInteger) |
| 207 ValidateNumber(instance, schema, path); | 210 ValidateNumber(instance, schema, path); |
| 208 else if (type != "boolean" && type != "null") | 211 else if (type != kBoolean && type != kNull) |
| 209 NOTREACHED() << "Unexpected type: " << type; | 212 NOTREACHED() << "Unexpected type: " << type; |
| 210 } | 213 } |
| 211 } | 214 } |
| 212 | 215 |
| 213 void JSONSchemaValidator::ValidateChoices(Value* instance, | 216 void JSONSchemaValidator::ValidateChoices(Value* instance, |
| 214 ListValue* choices, | 217 ListValue* choices, |
| 215 const std::string& path) { | 218 const std::string& path) { |
| 216 size_t original_num_errors = errors_.size(); | 219 size_t original_num_errors = errors_.size(); |
| 217 | 220 |
| 218 for (size_t i = 0; i < choices->GetSize(); ++i) { | 221 for (size_t i = 0; i < choices->GetSize(); ++i) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 } | 264 } |
| 262 } | 265 } |
| 263 | 266 |
| 264 errors_.push_back(Error(path, kInvalidEnum)); | 267 errors_.push_back(Error(path, kInvalidEnum)); |
| 265 } | 268 } |
| 266 | 269 |
| 267 void JSONSchemaValidator::ValidateObject(DictionaryValue* instance, | 270 void JSONSchemaValidator::ValidateObject(DictionaryValue* instance, |
| 268 DictionaryValue* schema, | 271 DictionaryValue* schema, |
| 269 const std::string& path) { | 272 const std::string& path) { |
| 270 DictionaryValue* properties = NULL; | 273 DictionaryValue* properties = NULL; |
| 271 schema->GetDictionary("properties", &properties); | 274 schema->GetDictionary(kProperties, &properties); |
| 272 if (properties) { | 275 if (properties) { |
| 273 for (DictionaryValue::key_iterator key = properties->begin_keys(); | 276 for (DictionaryValue::key_iterator key = properties->begin_keys(); |
| 274 key != properties->end_keys(); ++key) { | 277 key != properties->end_keys(); ++key) { |
| 275 std::string prop_path = path.empty() ? *key : (path + "." + *key); | 278 std::string prop_path = path.empty() ? *key : (path + "." + *key); |
| 276 DictionaryValue* prop_schema = NULL; | 279 DictionaryValue* prop_schema = NULL; |
| 277 CHECK(properties->GetDictionary(*key, &prop_schema)); | 280 CHECK(properties->GetDictionary(*key, &prop_schema)); |
| 278 | 281 |
| 279 Value* prop_value = NULL; | 282 Value* prop_value = NULL; |
| 280 if (instance->Get(*key, &prop_value)) { | 283 if (instance->Get(*key, &prop_value)) { |
| 281 Validate(prop_value, prop_schema, prop_path); | 284 Validate(prop_value, prop_schema, prop_path); |
| 282 } else { | 285 } else { |
| 283 // Properties are required unless there is an optional field set to | 286 // Properties are required unless there is an optional field set to |
| 284 // 'true'. | 287 // 'true'. |
| 285 bool is_optional = false; | 288 bool is_optional = false; |
| 286 prop_schema->GetBoolean("optional", &is_optional); | 289 prop_schema->GetBoolean(kOptional, &is_optional); |
| 287 if (!is_optional) { | 290 if (!is_optional) { |
| 288 errors_.push_back(Error(prop_path, kObjectPropertyIsRequired)); | 291 errors_.push_back(Error(prop_path, kObjectPropertyIsRequired)); |
| 289 } | 292 } |
| 290 } | 293 } |
| 291 } | 294 } |
| 292 } | 295 } |
| 293 | 296 |
| 294 DictionaryValue* additional_properties_schema = NULL; | 297 DictionaryValue* additional_properties_schema = NULL; |
| 295 if (SchemaAllowsAnyAdditionalItems(schema, &additional_properties_schema)) | 298 if (SchemaAllowsAnyAdditionalItems(schema, &additional_properties_schema)) |
| 296 return; | 299 return; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 310 Validate(prop_value, additional_properties_schema, prop_path); | 313 Validate(prop_value, additional_properties_schema, prop_path); |
| 311 } | 314 } |
| 312 } | 315 } |
| 313 } | 316 } |
| 314 | 317 |
| 315 void JSONSchemaValidator::ValidateArray(ListValue* instance, | 318 void JSONSchemaValidator::ValidateArray(ListValue* instance, |
| 316 DictionaryValue* schema, | 319 DictionaryValue* schema, |
| 317 const std::string& path) { | 320 const std::string& path) { |
| 318 DictionaryValue* single_type = NULL; | 321 DictionaryValue* single_type = NULL; |
| 319 size_t instance_size = instance->GetSize(); | 322 size_t instance_size = instance->GetSize(); |
| 320 if (schema->GetDictionary("items", &single_type)) { | 323 if (schema->GetDictionary(kItems, &single_type)) { |
| 321 int min_items = 0; | 324 int min_items = 0; |
| 322 if (schema->GetInteger("minItems", &min_items)) { | 325 if (schema->GetInteger(kMinItems, &min_items)) { |
| 323 CHECK(min_items >= 0); | 326 CHECK(min_items >= 0); |
| 324 if (instance_size < static_cast<size_t>(min_items)) { | 327 if (instance_size < static_cast<size_t>(min_items)) { |
| 325 errors_.push_back(Error(path, FormatErrorMessage( | 328 errors_.push_back(Error(path, FormatErrorMessage( |
| 326 kArrayMinItems, base::IntToString(min_items)))); | 329 kArrayMinItems, base::IntToString(min_items)))); |
| 327 } | 330 } |
| 328 } | 331 } |
| 329 | 332 |
| 330 int max_items = 0; | 333 int max_items = 0; |
| 331 if (schema->GetInteger("maxItems", &max_items)) { | 334 if (schema->GetInteger(kMaxItems, &max_items)) { |
| 332 CHECK(max_items >= 0); | 335 CHECK(max_items >= 0); |
| 333 if (instance_size > static_cast<size_t>(max_items)) { | 336 if (instance_size > static_cast<size_t>(max_items)) { |
| 334 errors_.push_back(Error(path, FormatErrorMessage( | 337 errors_.push_back(Error(path, FormatErrorMessage( |
| 335 kArrayMaxItems, base::IntToString(max_items)))); | 338 kArrayMaxItems, base::IntToString(max_items)))); |
| 336 } | 339 } |
| 337 } | 340 } |
| 338 | 341 |
| 339 // If the items property is a single schema, each item in the array must | 342 // If the items property is a single schema, each item in the array must |
| 340 // validate against that schema. | 343 // validate against that schema. |
| 341 for (size_t i = 0; i < instance_size; ++i) { | 344 for (size_t i = 0; i < instance_size; ++i) { |
| 342 Value* item = NULL; | 345 Value* item = NULL; |
| 343 CHECK(instance->Get(i, &item)); | 346 CHECK(instance->Get(i, &item)); |
| 344 std::string i_str = base::UintToString(i); | 347 std::string i_str = base::UintToString(i); |
| 345 std::string item_path = path.empty() ? i_str : (path + "." + i_str); | 348 std::string item_path = path.empty() ? i_str : (path + "." + i_str); |
| 346 Validate(item, single_type, item_path); | 349 Validate(item, single_type, item_path); |
| 347 } | 350 } |
| 348 | 351 |
| 349 return; | 352 return; |
| 350 } | 353 } |
| 351 | 354 |
| 352 // Otherwise, the list must be a tuple type, where each item in the list has a | 355 // Otherwise, the list must be a tuple type, where each item in the list has a |
| 353 // particular schema. | 356 // particular schema. |
| 354 ValidateTuple(instance, schema, path); | 357 ValidateTuple(instance, schema, path); |
| 355 } | 358 } |
| 356 | 359 |
| 357 void JSONSchemaValidator::ValidateTuple(ListValue* instance, | 360 void JSONSchemaValidator::ValidateTuple(ListValue* instance, |
| 358 DictionaryValue* schema, | 361 DictionaryValue* schema, |
| 359 const std::string& path) { | 362 const std::string& path) { |
| 360 ListValue* tuple_type = NULL; | 363 ListValue* tuple_type = NULL; |
| 361 schema->GetList("items", &tuple_type); | 364 schema->GetList(kItems, &tuple_type); |
| 362 size_t tuple_size = tuple_type ? tuple_type->GetSize() : 0; | 365 size_t tuple_size = tuple_type ? tuple_type->GetSize() : 0; |
| 363 if (tuple_type) { | 366 if (tuple_type) { |
| 364 for (size_t i = 0; i < tuple_size; ++i) { | 367 for (size_t i = 0; i < tuple_size; ++i) { |
| 365 std::string i_str = base::UintToString(i); | 368 std::string i_str = base::UintToString(i); |
| 366 std::string item_path = path.empty() ? i_str : (path + "." + i_str); | 369 std::string item_path = path.empty() ? i_str : (path + "." + i_str); |
| 367 DictionaryValue* item_schema = NULL; | 370 DictionaryValue* item_schema = NULL; |
| 368 CHECK(tuple_type->GetDictionary(i, &item_schema)); | 371 CHECK(tuple_type->GetDictionary(i, &item_schema)); |
| 369 Value* item_value = NULL; | 372 Value* item_value = NULL; |
| 370 instance->Get(i, &item_value); | 373 instance->Get(i, &item_value); |
| 371 if (item_value && item_value->GetType() != Value::TYPE_NULL) { | 374 if (item_value && item_value->GetType() != Value::TYPE_NULL) { |
| 372 Validate(item_value, item_schema, item_path); | 375 Validate(item_value, item_schema, item_path); |
| 373 } else { | 376 } else { |
| 374 bool is_optional = false; | 377 bool is_optional = false; |
| 375 item_schema->GetBoolean("optional", &is_optional); | 378 item_schema->GetBoolean(kOptional, &is_optional); |
| 376 if (!is_optional) { | 379 if (!is_optional) { |
| 377 errors_.push_back(Error(item_path, kArrayItemRequired)); | 380 errors_.push_back(Error(item_path, kArrayItemRequired)); |
| 378 return; | 381 return; |
| 379 } | 382 } |
| 380 } | 383 } |
| 381 } | 384 } |
| 382 } | 385 } |
| 383 | 386 |
| 384 DictionaryValue* additional_properties_schema = NULL; | 387 DictionaryValue* additional_properties_schema = NULL; |
| 385 if (SchemaAllowsAnyAdditionalItems(schema, &additional_properties_schema)) | 388 if (SchemaAllowsAnyAdditionalItems(schema, &additional_properties_schema)) |
| (...skipping 16 matching lines...) Expand all Loading... |
| 402 } | 405 } |
| 403 } | 406 } |
| 404 | 407 |
| 405 void JSONSchemaValidator::ValidateString(StringValue* instance, | 408 void JSONSchemaValidator::ValidateString(StringValue* instance, |
| 406 DictionaryValue* schema, | 409 DictionaryValue* schema, |
| 407 const std::string& path) { | 410 const std::string& path) { |
| 408 std::string value; | 411 std::string value; |
| 409 CHECK(instance->GetAsString(&value)); | 412 CHECK(instance->GetAsString(&value)); |
| 410 | 413 |
| 411 int min_length = 0; | 414 int min_length = 0; |
| 412 if (schema->GetInteger("minLength", &min_length)) { | 415 if (schema->GetInteger(kMinLength, &min_length)) { |
| 413 CHECK(min_length >= 0); | 416 CHECK(min_length >= 0); |
| 414 if (value.size() < static_cast<size_t>(min_length)) { | 417 if (value.size() < static_cast<size_t>(min_length)) { |
| 415 errors_.push_back(Error(path, FormatErrorMessage( | 418 errors_.push_back(Error(path, FormatErrorMessage( |
| 416 kStringMinLength, base::IntToString(min_length)))); | 419 kStringMinLength, base::IntToString(min_length)))); |
| 417 } | 420 } |
| 418 } | 421 } |
| 419 | 422 |
| 420 int max_length = 0; | 423 int max_length = 0; |
| 421 if (schema->GetInteger("maxLength", &max_length)) { | 424 if (schema->GetInteger(kMaxLength, &max_length)) { |
| 422 CHECK(max_length >= 0); | 425 CHECK(max_length >= 0); |
| 423 if (value.size() > static_cast<size_t>(max_length)) { | 426 if (value.size() > static_cast<size_t>(max_length)) { |
| 424 errors_.push_back(Error(path, FormatErrorMessage( | 427 errors_.push_back(Error(path, FormatErrorMessage( |
| 425 kStringMaxLength, base::IntToString(max_length)))); | 428 kStringMaxLength, base::IntToString(max_length)))); |
| 426 } | 429 } |
| 427 } | 430 } |
| 428 | 431 |
| 429 CHECK(!schema->HasKey("pattern")) << "Pattern is not supported."; | 432 CHECK(!schema->HasKey(kPattern)) << "Pattern is not supported."; |
| 430 } | 433 } |
| 431 | 434 |
| 432 void JSONSchemaValidator::ValidateNumber(Value* instance, | 435 void JSONSchemaValidator::ValidateNumber(Value* instance, |
| 433 DictionaryValue* schema, | 436 DictionaryValue* schema, |
| 434 const std::string& path) { | 437 const std::string& path) { |
| 435 double value = GetNumberValue(instance); | 438 double value = GetNumberValue(instance); |
| 436 | 439 |
| 437 // TODO(aa): It would be good to test that the double is not infinity or nan, | 440 // TODO(aa): It would be good to test that the double is not infinity or nan, |
| 438 // but isnan and isinf aren't defined on Windows. | 441 // but isnan and isinf aren't defined on Windows. |
| 439 | 442 |
| 440 double minimum = 0; | 443 double minimum = 0; |
| 441 if (schema->GetDouble("minimum", &minimum)) { | 444 if (schema->GetDouble(kMinimum, &minimum)) { |
| 442 if (value < minimum) | 445 if (value < minimum) |
| 443 errors_.push_back(Error(path, FormatErrorMessage( | 446 errors_.push_back(Error(path, FormatErrorMessage( |
| 444 kNumberMinimum, base::DoubleToString(minimum)))); | 447 kNumberMinimum, base::DoubleToString(minimum)))); |
| 445 } | 448 } |
| 446 | 449 |
| 447 double maximum = 0; | 450 double maximum = 0; |
| 448 if (schema->GetDouble("maximum", &maximum)) { | 451 if (schema->GetDouble(kMaximum, &maximum)) { |
| 449 if (value > maximum) | 452 if (value > maximum) |
| 450 errors_.push_back(Error(path, FormatErrorMessage( | 453 errors_.push_back(Error(path, FormatErrorMessage( |
| 451 kNumberMaximum, base::DoubleToString(maximum)))); | 454 kNumberMaximum, base::DoubleToString(maximum)))); |
| 452 } | 455 } |
| 453 } | 456 } |
| 454 | 457 |
| 455 bool JSONSchemaValidator::ValidateType(Value* instance, | 458 bool JSONSchemaValidator::ValidateType(Value* instance, |
| 456 const std::string& expected_type, | 459 const std::string& expected_type, |
| 457 const std::string& path) { | 460 const std::string& path) { |
| 458 std::string actual_type = GetJSONSchemaType(instance); | 461 std::string actual_type = GetJSONSchemaType(instance); |
| 459 if (expected_type == actual_type || | 462 if (expected_type == actual_type || |
| 460 (expected_type == "number" && actual_type == "integer")) { | 463 (expected_type == kNumber && actual_type == kInteger)) { |
| 461 return true; | 464 return true; |
| 462 } else { | 465 } else { |
| 463 errors_.push_back(Error(path, FormatErrorMessage( | 466 errors_.push_back(Error(path, FormatErrorMessage( |
| 464 kInvalidType, expected_type, actual_type))); | 467 kInvalidType, expected_type, actual_type))); |
| 465 return false; | 468 return false; |
| 466 } | 469 } |
| 467 } | 470 } |
| 468 | 471 |
| 469 bool JSONSchemaValidator::SchemaAllowsAnyAdditionalItems( | 472 bool JSONSchemaValidator::SchemaAllowsAnyAdditionalItems( |
| 470 DictionaryValue* schema, DictionaryValue** additional_properties_schema) { | 473 DictionaryValue* schema, DictionaryValue** additional_properties_schema) { |
| 471 // If the validator allows additional properties globally, and this schema | 474 // If the validator allows additional properties globally, and this schema |
| 472 // doesn't override, then we can exit early. | 475 // doesn't override, then we can exit early. |
| 473 schema->GetDictionary("additionalProperties", additional_properties_schema); | 476 schema->GetDictionary(kAdditionalProperties, additional_properties_schema); |
| 474 | 477 |
| 475 if (*additional_properties_schema) { | 478 if (*additional_properties_schema) { |
| 476 std::string additional_properties_type("any"); | 479 std::string additional_properties_type(kAny); |
| 477 CHECK((*additional_properties_schema)->GetString( | 480 CHECK((*additional_properties_schema)->GetString( |
| 478 "type", &additional_properties_type)); | 481 kType, &additional_properties_type)); |
| 479 return additional_properties_type == "any"; | 482 return additional_properties_type == kAny; |
| 480 } else { | 483 } else { |
| 481 return default_allow_additional_properties_; | 484 return default_allow_additional_properties_; |
| 482 } | 485 } |
| 483 } | 486 } |
| OLD | NEW |