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