Chromium Code Reviews| Index: components/json_schema/json_schema_validator.cc |
| diff --git a/components/json_schema/json_schema_validator.cc b/components/json_schema/json_schema_validator.cc |
| index 32cb93ca2547c08a34b34bd575401a0a0720d6a8..3f3c0ed3b21fe5b1c07e0e194b1dd3a44ab8049b 100644 |
| --- a/components/json_schema/json_schema_validator.cc |
| +++ b/components/json_schema/json_schema_validator.cc |
| @@ -65,7 +65,9 @@ const base::Value* ExtractNameFromDictionary(const base::Value* value) { |
| return value; |
| } |
| -bool IsValidSchema(const base::DictionaryValue* dict, std::string* error) { |
| +bool IsValidSchema(const base::DictionaryValue* dict, |
| + bool ignore_unknown_attrs, |
| + std::string* error) { |
| // This array must be sorted, so that std::lower_bound can perform a |
| // binary search. |
| static const ExpectedType kExpectedTypes[] = { |
| @@ -126,7 +128,7 @@ bool IsValidSchema(const base::DictionaryValue* dict, std::string* error) { |
| // Validate the "items" attribute, which is a schema or a list of schemas. |
| if (it.key() == schema::kItems) { |
| if (it.value().GetAsDictionary(&dictionary_value)) { |
| - if (!IsValidSchema(dictionary_value, error)) { |
| + if (!IsValidSchema(dictionary_value, ignore_unknown_attrs, error)) { |
| DCHECK(!error->empty()); |
| return false; |
| } |
| @@ -138,7 +140,7 @@ bool IsValidSchema(const base::DictionaryValue* dict, std::string* error) { |
| static_cast<int>(i)); |
| return false; |
| } |
| - if (!IsValidSchema(dictionary_value, error)) { |
| + if (!IsValidSchema(dictionary_value, ignore_unknown_attrs, error)) { |
| DCHECK(!error->empty()); |
| return false; |
| } |
| @@ -155,9 +157,13 @@ bool IsValidSchema(const base::DictionaryValue* dict, std::string* error) { |
| const ExpectedType* entry = std::lower_bound( |
| kExpectedTypes, end, it.key(), CompareToString); |
| if (entry == end || entry->key != it.key()) { |
| - *error = base::StringPrintf("Invalid attribute %s", it.key().c_str()); |
| - return false; |
| + if (!ignore_unknown_attrs) { |
|
not at google - send to devlin
2013/12/06 18:12:26
i.e. an option such that this would be
if (!(opti
|
| + *error = base::StringPrintf("Invalid attribute %s", it.key().c_str()); |
| + return false; |
| + } |
| + continue; |
| } |
| + |
| if (!it.value().IsType(entry->type)) { |
| *error = base::StringPrintf("Invalid value for %s attribute", |
| it.key().c_str()); |
| @@ -185,7 +191,7 @@ bool IsValidSchema(const base::DictionaryValue* dict, std::string* error) { |
| *error = "Invalid value for properties attribute"; |
| return false; |
| } |
| - if (!IsValidSchema(dictionary_value, error)) { |
| + if (!IsValidSchema(dictionary_value, ignore_unknown_attrs, error)) { |
| DCHECK(!error->empty()); |
| return false; |
| } |
| @@ -195,7 +201,7 @@ bool IsValidSchema(const base::DictionaryValue* dict, std::string* error) { |
| // Validate "additionalProperties" attribute, which is a schema. |
| if (it.key() == schema::kAdditionalProperties) { |
| it.value().GetAsDictionary(&dictionary_value); |
| - if (!IsValidSchema(dictionary_value, error)) { |
| + if (!IsValidSchema(dictionary_value, ignore_unknown_attrs, error)) { |
| DCHECK(!error->empty()); |
| return false; |
| } |
| @@ -236,7 +242,7 @@ bool IsValidSchema(const base::DictionaryValue* dict, std::string* error) { |
| *error = "Invalid choices attribute"; |
| return false; |
| } |
| - if (!IsValidSchema(dictionary_value, error)) { |
| + if (!IsValidSchema(dictionary_value, ignore_unknown_attrs, error)) { |
| DCHECK(!error->empty()); |
| return false; |
| } |
| @@ -355,6 +361,7 @@ std::string JSONSchemaValidator::FormatErrorMessage(const std::string& format, |
| // static |
| scoped_ptr<base::DictionaryValue> JSONSchemaValidator::IsValidSchema( |
| const std::string& schema, |
| + bool ignore_unknown_attributes, |
| std::string* error) { |
| base::JSONParserOptions options = base::JSON_PARSE_RFC; |
| scoped_ptr<base::Value> json( |
| @@ -366,7 +373,7 @@ scoped_ptr<base::DictionaryValue> JSONSchemaValidator::IsValidSchema( |
| *error = "Schema must be a JSON object"; |
| return scoped_ptr<base::DictionaryValue>(); |
| } |
| - if (!::IsValidSchema(dict, error)) |
| + if (!::IsValidSchema(dict, ignore_unknown_attributes, error)) |
| return scoped_ptr<base::DictionaryValue>(); |
| ignore_result(json.release()); |
| return make_scoped_ptr(dict); |