| Index: chrome/common/json_schema_validator.cc
|
| diff --git a/chrome/common/json_schema_validator.cc b/chrome/common/json_schema_validator.cc
|
| index ccb649bcf35b7333d08d929074be35bb2629c98f..88595eac3a66d4d9c15c319da7fbef2ad20ff343 100644
|
| --- a/chrome/common/json_schema_validator.cc
|
| +++ b/chrome/common/json_schema_validator.cc
|
| @@ -10,8 +10,11 @@
|
| #include "base/string_number_conversions.h"
|
| #include "base/string_util.h"
|
| #include "base/values.h"
|
| +#include "chrome/common/json_schema_constants.h"
|
| #include "ui/base/l10n/l10n_util.h"
|
|
|
| +using namespace json_schema_constants;
|
| +
|
| namespace {
|
|
|
| double GetNumberValue(Value* value) {
|
| @@ -71,27 +74,27 @@ const char JSONSchemaValidator::kInvalidType[] =
|
| std::string JSONSchemaValidator::GetJSONSchemaType(Value* value) {
|
| switch (value->GetType()) {
|
| case Value::TYPE_NULL:
|
| - return "null";
|
| + return kNull;
|
| case Value::TYPE_BOOLEAN:
|
| - return "boolean";
|
| + return kBoolean;
|
| case Value::TYPE_INTEGER:
|
| - return "integer";
|
| + return kInteger;
|
| case Value::TYPE_DOUBLE: {
|
| double double_value = 0;
|
| value->GetAsDouble(&double_value);
|
| if (std::abs(double_value) <= std::pow(2.0, DBL_MANT_DIG) &&
|
| double_value == floor(double_value)) {
|
| - return "integer";
|
| + return kInteger;
|
| } else {
|
| - return "number";
|
| + return kNumber;
|
| }
|
| }
|
| case Value::TYPE_STRING:
|
| - return "string";
|
| + return kString;
|
| case Value::TYPE_DICTIONARY:
|
| - return "object";
|
| + return kObject;
|
| case Value::TYPE_LIST:
|
| - return "array";
|
| + return kArray;
|
| default:
|
| NOTREACHED() << "Unexpected value type: " << value->GetType();
|
| return "";
|
| @@ -131,7 +134,7 @@ JSONSchemaValidator::JSONSchemaValidator(DictionaryValue* schema,
|
| CHECK(types->GetDictionary(i, &type));
|
|
|
| std::string id;
|
| - CHECK(type->GetString("id", &id));
|
| + CHECK(type->GetString(kId, &id));
|
|
|
| CHECK(types_.find(id) == types_.end());
|
| types_[id] = type;
|
| @@ -151,7 +154,7 @@ void JSONSchemaValidator::Validate(Value* instance,
|
| const std::string& path) {
|
| // If this schema defines itself as reference type, save it in this.types.
|
| std::string id;
|
| - if (schema->GetString("id", &id)) {
|
| + if (schema->GetString(kId, &id)) {
|
| TypeMap::iterator iter = types_.find(id);
|
| if (iter == types_.end())
|
| types_[id] = schema;
|
| @@ -176,7 +179,7 @@ void JSONSchemaValidator::Validate(Value* instance,
|
| // If the schema has a choices property, the instance must validate against at
|
| // least one of the items in that array.
|
| ListValue* choices = NULL;
|
| - if (schema->GetList("choices", &choices)) {
|
| + if (schema->GetList(kChoices, &choices)) {
|
| ValidateChoices(instance, choices, path);
|
| return;
|
| }
|
| @@ -184,28 +187,28 @@ void JSONSchemaValidator::Validate(Value* instance,
|
| // If the schema has an enum property, the instance must be one of those
|
| // values.
|
| ListValue* enumeration = NULL;
|
| - if (schema->GetList("enum", &enumeration)) {
|
| + if (schema->GetList(kEnum, &enumeration)) {
|
| ValidateEnum(instance, enumeration, path);
|
| return;
|
| }
|
|
|
| std::string type;
|
| - schema->GetString("type", &type);
|
| + schema->GetString(kType, &type);
|
| CHECK(!type.empty());
|
| - if (type != "any") {
|
| + if (type != kAny) {
|
| if (!ValidateType(instance, type, path))
|
| return;
|
|
|
| // These casts are safe because of checks in ValidateType().
|
| - if (type == "object")
|
| + if (type == kObject)
|
| ValidateObject(static_cast<DictionaryValue*>(instance), schema, path);
|
| - else if (type == "array")
|
| + else if (type == kArray)
|
| ValidateArray(static_cast<ListValue*>(instance), schema, path);
|
| - else if (type == "string")
|
| + else if (type == kString)
|
| ValidateString(static_cast<StringValue*>(instance), schema, path);
|
| - else if (type == "number" || type == "integer")
|
| + else if (type == kNumber || type == kInteger)
|
| ValidateNumber(instance, schema, path);
|
| - else if (type != "boolean" && type != "null")
|
| + else if (type != kBoolean && type != kNull)
|
| NOTREACHED() << "Unexpected type: " << type;
|
| }
|
| }
|
| @@ -268,7 +271,7 @@ void JSONSchemaValidator::ValidateObject(DictionaryValue* instance,
|
| DictionaryValue* schema,
|
| const std::string& path) {
|
| DictionaryValue* properties = NULL;
|
| - schema->GetDictionary("properties", &properties);
|
| + schema->GetDictionary(kProperties, &properties);
|
| if (properties) {
|
| for (DictionaryValue::key_iterator key = properties->begin_keys();
|
| key != properties->end_keys(); ++key) {
|
| @@ -283,7 +286,7 @@ void JSONSchemaValidator::ValidateObject(DictionaryValue* instance,
|
| // Properties are required unless there is an optional field set to
|
| // 'true'.
|
| bool is_optional = false;
|
| - prop_schema->GetBoolean("optional", &is_optional);
|
| + prop_schema->GetBoolean(kOptional, &is_optional);
|
| if (!is_optional) {
|
| errors_.push_back(Error(prop_path, kObjectPropertyIsRequired));
|
| }
|
| @@ -317,9 +320,9 @@ void JSONSchemaValidator::ValidateArray(ListValue* instance,
|
| const std::string& path) {
|
| DictionaryValue* single_type = NULL;
|
| size_t instance_size = instance->GetSize();
|
| - if (schema->GetDictionary("items", &single_type)) {
|
| + if (schema->GetDictionary(kItems, &single_type)) {
|
| int min_items = 0;
|
| - if (schema->GetInteger("minItems", &min_items)) {
|
| + if (schema->GetInteger(kMinItems, &min_items)) {
|
| CHECK(min_items >= 0);
|
| if (instance_size < static_cast<size_t>(min_items)) {
|
| errors_.push_back(Error(path, FormatErrorMessage(
|
| @@ -328,7 +331,7 @@ void JSONSchemaValidator::ValidateArray(ListValue* instance,
|
| }
|
|
|
| int max_items = 0;
|
| - if (schema->GetInteger("maxItems", &max_items)) {
|
| + if (schema->GetInteger(kMaxItems, &max_items)) {
|
| CHECK(max_items >= 0);
|
| if (instance_size > static_cast<size_t>(max_items)) {
|
| errors_.push_back(Error(path, FormatErrorMessage(
|
| @@ -358,7 +361,7 @@ void JSONSchemaValidator::ValidateTuple(ListValue* instance,
|
| DictionaryValue* schema,
|
| const std::string& path) {
|
| ListValue* tuple_type = NULL;
|
| - schema->GetList("items", &tuple_type);
|
| + schema->GetList(kItems, &tuple_type);
|
| size_t tuple_size = tuple_type ? tuple_type->GetSize() : 0;
|
| if (tuple_type) {
|
| for (size_t i = 0; i < tuple_size; ++i) {
|
| @@ -372,7 +375,7 @@ void JSONSchemaValidator::ValidateTuple(ListValue* instance,
|
| Validate(item_value, item_schema, item_path);
|
| } else {
|
| bool is_optional = false;
|
| - item_schema->GetBoolean("optional", &is_optional);
|
| + item_schema->GetBoolean(kOptional, &is_optional);
|
| if (!is_optional) {
|
| errors_.push_back(Error(item_path, kArrayItemRequired));
|
| return;
|
| @@ -409,7 +412,7 @@ void JSONSchemaValidator::ValidateString(StringValue* instance,
|
| CHECK(instance->GetAsString(&value));
|
|
|
| int min_length = 0;
|
| - if (schema->GetInteger("minLength", &min_length)) {
|
| + if (schema->GetInteger(kMinLength, &min_length)) {
|
| CHECK(min_length >= 0);
|
| if (value.size() < static_cast<size_t>(min_length)) {
|
| errors_.push_back(Error(path, FormatErrorMessage(
|
| @@ -418,7 +421,7 @@ void JSONSchemaValidator::ValidateString(StringValue* instance,
|
| }
|
|
|
| int max_length = 0;
|
| - if (schema->GetInteger("maxLength", &max_length)) {
|
| + if (schema->GetInteger(kMaxLength, &max_length)) {
|
| CHECK(max_length >= 0);
|
| if (value.size() > static_cast<size_t>(max_length)) {
|
| errors_.push_back(Error(path, FormatErrorMessage(
|
| @@ -426,7 +429,7 @@ void JSONSchemaValidator::ValidateString(StringValue* instance,
|
| }
|
| }
|
|
|
| - CHECK(!schema->HasKey("pattern")) << "Pattern is not supported.";
|
| + CHECK(!schema->HasKey(kPattern)) << "Pattern is not supported.";
|
| }
|
|
|
| void JSONSchemaValidator::ValidateNumber(Value* instance,
|
| @@ -438,14 +441,14 @@ void JSONSchemaValidator::ValidateNumber(Value* instance,
|
| // but isnan and isinf aren't defined on Windows.
|
|
|
| double minimum = 0;
|
| - if (schema->GetDouble("minimum", &minimum)) {
|
| + if (schema->GetDouble(kMinimum, &minimum)) {
|
| if (value < minimum)
|
| errors_.push_back(Error(path, FormatErrorMessage(
|
| kNumberMinimum, base::DoubleToString(minimum))));
|
| }
|
|
|
| double maximum = 0;
|
| - if (schema->GetDouble("maximum", &maximum)) {
|
| + if (schema->GetDouble(kMaximum, &maximum)) {
|
| if (value > maximum)
|
| errors_.push_back(Error(path, FormatErrorMessage(
|
| kNumberMaximum, base::DoubleToString(maximum))));
|
| @@ -457,7 +460,7 @@ bool JSONSchemaValidator::ValidateType(Value* instance,
|
| const std::string& path) {
|
| std::string actual_type = GetJSONSchemaType(instance);
|
| if (expected_type == actual_type ||
|
| - (expected_type == "number" && actual_type == "integer")) {
|
| + (expected_type == kNumber && actual_type == kInteger)) {
|
| return true;
|
| } else {
|
| errors_.push_back(Error(path, FormatErrorMessage(
|
| @@ -470,13 +473,13 @@ bool JSONSchemaValidator::SchemaAllowsAnyAdditionalItems(
|
| DictionaryValue* schema, DictionaryValue** additional_properties_schema) {
|
| // If the validator allows additional properties globally, and this schema
|
| // doesn't override, then we can exit early.
|
| - schema->GetDictionary("additionalProperties", additional_properties_schema);
|
| + schema->GetDictionary(kAdditionalProperties, additional_properties_schema);
|
|
|
| if (*additional_properties_schema) {
|
| - std::string additional_properties_type("any");
|
| + std::string additional_properties_type(kAny);
|
| CHECK((*additional_properties_schema)->GetString(
|
| - "type", &additional_properties_type));
|
| - return additional_properties_type == "any";
|
| + kType, &additional_properties_type));
|
| + return additional_properties_type == kAny;
|
| } else {
|
| return default_allow_additional_properties_;
|
| }
|
|
|