summaryrefslogtreecommitdiffstats
path: root/chrome/common
diff options
context:
space:
mode:
authorjoaodasilva@chromium.org <joaodasilva@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-09-16 15:36:23 +0000
committerjoaodasilva@chromium.org <joaodasilva@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-09-16 15:36:23 +0000
commit367dbf7a04463d7a874056a69de02b9d16a1babb (patch)
tree154857a57e431df04716033fb77ca0e8d60fe055 /chrome/common
parentc6dc72b2cecab85ba19d0e12d57ad5c5deaf9d1e (diff)
downloadchromium_src-367dbf7a04463d7a874056a69de02b9d16a1babb.zip
chromium_src-367dbf7a04463d7a874056a69de02b9d16a1babb.tar.gz
chromium_src-367dbf7a04463d7a874056a69de02b9d16a1babb.tar.bz2
Share JSON schema constants.
BUG=None TEST=unit_tests green Review URL: https://chromiumcodereview.appspot.com/10696034 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@157051 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r--chrome/common/json_schema_constants.cc34
-rw-r--r--chrome/common/json_schema_constants.h38
-rw-r--r--chrome/common/json_schema_validator.cc78
-rw-r--r--chrome/common/json_schema_validator_unittest_base.cc243
4 files changed, 259 insertions, 134 deletions
diff --git a/chrome/common/json_schema_constants.cc b/chrome/common/json_schema_constants.cc
new file mode 100644
index 0000000..c364bdf
--- /dev/null
+++ b/chrome/common/json_schema_constants.cc
@@ -0,0 +1,34 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/common/json_schema_constants.h"
+
+namespace json_schema_constants {
+
+const char kAdditionalProperties[] = "additionalProperties";
+const char kAny[] = "any";
+const char kArray[] = "array";
+const char kBoolean[] = "boolean";
+const char kChoices[] = "choices";
+const char kEnum[] = "enum";
+const char kId[] = "id";
+const char kInteger[] = "integer";
+const char kItems[] = "items";
+const char kMaximum[] = "maximum";
+const char kMaxItems[] = "maxItems";
+const char kMaxLength[] = "maxLength";
+const char kMinimum[] = "minimum";
+const char kMinItems[] = "minItems";
+const char kMinLength[] = "minLength";
+const char kNull[] = "null";
+const char kNumber[] = "number";
+const char kObject[] = "object";
+const char kOptional[] = "optional";
+const char kPattern[] = "pattern";
+const char kProperties[] = "properties";
+const char kRef[] = "$ref";
+const char kString[] = "string";
+const char kType[] = "type";
+
+} // namespace json_schema_constants
diff --git a/chrome/common/json_schema_constants.h b/chrome/common/json_schema_constants.h
new file mode 100644
index 0000000..8e3a7ee
--- /dev/null
+++ b/chrome/common/json_schema_constants.h
@@ -0,0 +1,38 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_COMMON_JSON_SCHEMA_CONSTANTS_H_
+#define CHROME_COMMON_JSON_SCHEMA_CONSTANTS_H_
+
+// These constants are shared by code that uses JSON schemas.
+namespace json_schema_constants {
+
+extern const char kAdditionalProperties[];
+extern const char kAny[];
+extern const char kArray[];
+extern const char kBoolean[];
+extern const char kChoices[];
+extern const char kEnum[];
+extern const char kId[];
+extern const char kInteger[];
+extern const char kItems[];
+extern const char kMaximum[];
+extern const char kMaxItems[];
+extern const char kMaxLength[];
+extern const char kMinimum[];
+extern const char kMinItems[];
+extern const char kMinLength[];
+extern const char kNull[];
+extern const char kNumber[];
+extern const char kObject[];
+extern const char kOptional[];
+extern const char kPattern[];
+extern const char kProperties[];
+extern const char kRef[];
+extern const char kString[];
+extern const char kType[];
+
+} // namespace json_schema_constants
+
+#endif // CHROME_COMMON_JSON_SCHEMA_CONSTANTS_H_
diff --git a/chrome/common/json_schema_validator.cc b/chrome/common/json_schema_validator.cc
index ccb649b..0632a37 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"
+namespace schema = 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 schema::kNull;
case Value::TYPE_BOOLEAN:
- return "boolean";
+ return schema::kBoolean;
case Value::TYPE_INTEGER:
- return "integer";
+ return schema::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 schema::kInteger;
} else {
- return "number";
+ return schema::kNumber;
}
}
case Value::TYPE_STRING:
- return "string";
+ return schema::kString;
case Value::TYPE_DICTIONARY:
- return "object";
+ return schema::kObject;
case Value::TYPE_LIST:
- return "array";
+ return schema::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(schema::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(schema::kId, &id)) {
TypeMap::iterator iter = types_.find(id);
if (iter == types_.end())
types_[id] = schema;
@@ -162,7 +165,7 @@ void JSONSchemaValidator::Validate(Value* instance,
// If the schema has a $ref property, the instance must validate against
// that schema. It must be present in types_ to be referenced.
std::string ref;
- if (schema->GetString("$ref", &ref)) {
+ if (schema->GetString(schema::kRef, &ref)) {
TypeMap::iterator type = types_.find(ref);
if (type == types_.end()) {
errors_.push_back(
@@ -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(schema::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(schema::kEnum, &enumeration)) {
ValidateEnum(instance, enumeration, path);
return;
}
std::string type;
- schema->GetString("type", &type);
+ schema->GetString(schema::kType, &type);
CHECK(!type.empty());
- if (type != "any") {
+ if (type != schema::kAny) {
if (!ValidateType(instance, type, path))
return;
// These casts are safe because of checks in ValidateType().
- if (type == "object")
+ if (type == schema::kObject)
ValidateObject(static_cast<DictionaryValue*>(instance), schema, path);
- else if (type == "array")
+ else if (type == schema::kArray)
ValidateArray(static_cast<ListValue*>(instance), schema, path);
- else if (type == "string")
+ else if (type == schema::kString)
ValidateString(static_cast<StringValue*>(instance), schema, path);
- else if (type == "number" || type == "integer")
+ else if (type == schema::kNumber || type == schema::kInteger)
ValidateNumber(instance, schema, path);
- else if (type != "boolean" && type != "null")
+ else if (type != schema::kBoolean && type != schema::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(schema::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(schema::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(schema::kItems, &single_type)) {
int min_items = 0;
- if (schema->GetInteger("minItems", &min_items)) {
+ if (schema->GetInteger(schema::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(schema::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(schema::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(schema::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(schema::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(schema::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(schema::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(schema::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(schema::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 == schema::kNumber && actual_type == schema::kInteger)) {
return true;
} else {
errors_.push_back(Error(path, FormatErrorMessage(
@@ -470,13 +473,14 @@ 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(schema::kAdditionalProperties,
+ additional_properties_schema);
if (*additional_properties_schema) {
- std::string additional_properties_type("any");
+ std::string additional_properties_type(schema::kAny);
CHECK((*additional_properties_schema)->GetString(
- "type", &additional_properties_type));
- return additional_properties_type == "any";
+ schema::kType, &additional_properties_type));
+ return additional_properties_type == schema::kAny;
} else {
return default_allow_additional_properties_;
}
diff --git a/chrome/common/json_schema_validator_unittest_base.cc b/chrome/common/json_schema_validator_unittest_base.cc
index 3e1ee73..b3cc89d 100644
--- a/chrome/common/json_schema_validator_unittest_base.cc
+++ b/chrome/common/json_schema_validator_unittest_base.cc
@@ -16,8 +16,11 @@
#include "base/stringprintf.h"
#include "base/values.h"
#include "chrome/common/chrome_paths.h"
+#include "chrome/common/json_schema_constants.h"
#include "chrome/common/json_schema_validator.h"
+namespace schema = json_schema_constants;
+
namespace {
#define TEST_SOURCE base::StringPrintf("%s:%i", __FILE__, __LINE__)
@@ -94,7 +97,9 @@ void JSONSchemaValidatorTestBase::TestComplex() {
instance->Append(new DictionaryValue());
ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "number", "object"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kNumber,
+ schema::kObject));
instance->Remove(instance->GetSize() - 1, NULL);
DictionaryValue* item = NULL;
@@ -113,8 +118,8 @@ void JSONSchemaValidatorTestBase::TestStringPattern() {
return;
scoped_ptr<DictionaryValue> schema(new DictionaryValue());
- schema->SetString("type", "string");
- schema->SetString("pattern", "foo+");
+ schema->SetString(schema::kType, schema::kString);
+ schema->SetString(schema::kPattern, "foo+");
ExpectValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateStringValue("foo")).get(),
@@ -182,9 +187,9 @@ void JSONSchemaValidatorTestBase::TestExtends() {
void JSONSchemaValidatorTestBase::TestObject() {
scoped_ptr<DictionaryValue> schema(new DictionaryValue());
- schema->SetString("type", "object");
- schema->SetString("properties.foo.type", "string");
- schema->SetString("properties.bar.type", "integer");
+ schema->SetString(schema::kType, schema::kObject);
+ schema->SetString("properties.foo.type", schema::kString);
+ schema->SetString("properties.bar.type", schema::kInteger);
scoped_ptr<DictionaryValue> instance(new DictionaryValue());
instance->SetString("foo", "foo");
@@ -204,11 +209,13 @@ void JSONSchemaValidatorTestBase::TestObject() {
instance->SetString("bar", "42");
ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "bar",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "integer", "string"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kInteger,
+ schema::kString));
DictionaryValue* additional_properties = new DictionaryValue();
- additional_properties->SetString("type", "any");
- schema->Set("additionalProperties", additional_properties);
+ additional_properties->SetString(schema::kType, schema::kAny);
+ schema->Set(schema::kAdditionalProperties, additional_properties);
instance->SetInteger("bar", 42);
instance->SetBoolean("extra", true);
@@ -217,21 +224,23 @@ void JSONSchemaValidatorTestBase::TestObject() {
instance->SetString("extra", "foo");
ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
- additional_properties->SetString("type", "boolean");
+ additional_properties->SetString(schema::kType, schema::kBoolean);
instance->SetBoolean("extra", true);
ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
instance->SetString("extra", "foo");
ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
"extra", JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "boolean", "string"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kBoolean,
+ schema::kString));
DictionaryValue* properties = NULL;
DictionaryValue* bar_property = NULL;
- ASSERT_TRUE(schema->GetDictionary("properties", &properties));
+ ASSERT_TRUE(schema->GetDictionary(schema::kProperties, &properties));
ASSERT_TRUE(properties->GetDictionary("bar", &bar_property));
- bar_property->SetBoolean("optional", true);
+ bar_property->SetBoolean(schema::kOptional, true);
instance->Remove("extra", NULL);
ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
instance->Remove("bar", NULL);
@@ -239,11 +248,15 @@ void JSONSchemaValidatorTestBase::TestObject() {
instance->Set("bar", Value::CreateNullValue());
ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
"bar", JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "integer", "null"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kInteger,
+ schema::kNull));
instance->SetString("bar", "42");
ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL,
"bar", JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "integer", "string"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kInteger,
+ schema::kString));
}
void JSONSchemaValidatorTestBase::TestTypeReference() {
@@ -251,16 +264,16 @@ void JSONSchemaValidatorTestBase::TestTypeReference() {
ASSERT_TRUE(types.get());
scoped_ptr<DictionaryValue> schema(new DictionaryValue());
- schema->SetString("type", "object");
- schema->SetString("properties.foo.type", "string");
+ schema->SetString(schema::kType, schema::kObject);
+ schema->SetString("properties.foo.type", schema::kString);
schema->SetString("properties.bar.$ref", "Max10Int");
schema->SetString("properties.baz.$ref", "MinLengthString");
scoped_ptr<DictionaryValue> schema_inline(new DictionaryValue());
- schema_inline->SetString("type", "object");
- schema_inline->SetString("properties.foo.type", "string");
+ schema_inline->SetString(schema::kType, schema::kObject);
+ schema_inline->SetString("properties.foo.type", schema::kString);
schema_inline->SetString("properties.bar.id", "NegativeInt");
- schema_inline->SetString("properties.bar.type", "integer");
+ schema_inline->SetString("properties.bar.type", schema::kInteger);
schema_inline->SetInteger("properties.bar.maximum", 0);
schema_inline->SetString("properties.baz.$ref", "NegativeInt");
@@ -329,29 +342,33 @@ void JSONSchemaValidatorTestBase::TestArrayTuple() {
instance->Append(Value::CreateIntegerValue(42));
ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "string", "integer"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kString,
+ schema::kInteger));
DictionaryValue* additional_properties = new DictionaryValue();
- additional_properties->SetString("type", "any");
- schema->Set("additionalProperties", additional_properties);
+ additional_properties->SetString(schema::kType, schema::kAny);
+ schema->Set(schema::kAdditionalProperties, additional_properties);
instance->Set(0, Value::CreateStringValue("42"));
instance->Append(Value::CreateStringValue("anything"));
ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
instance->Set(2, new ListValue());
ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
- additional_properties->SetString("type", "boolean");
+ additional_properties->SetString(schema::kType, schema::kBoolean);
ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "2",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "boolean", "array"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kBoolean,
+ schema::kArray));
instance->Set(2, Value::CreateBooleanValue(false));
ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
ListValue* items_schema = NULL;
DictionaryValue* item0_schema = NULL;
- ASSERT_TRUE(schema->GetList("items", &items_schema));
+ ASSERT_TRUE(schema->GetList(schema::kItems, &items_schema));
ASSERT_TRUE(items_schema->GetDictionary(0, &item0_schema));
- item0_schema->SetBoolean("optional", true);
+ item0_schema->SetBoolean(schema::kOptional, true);
instance->Remove(2, NULL);
ExpectValid(TEST_SOURCE, instance.get(), schema.get(), NULL);
// TODO(aa): I think this is inconsistent with the handling of NULL+optional
@@ -361,15 +378,17 @@ void JSONSchemaValidatorTestBase::TestArrayTuple() {
instance->Set(0, Value::CreateIntegerValue(42));
ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "0",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "string", "integer"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kString,
+ schema::kInteger));
}
void JSONSchemaValidatorTestBase::TestArrayNonTuple() {
scoped_ptr<DictionaryValue> schema(new DictionaryValue());
- schema->SetString("type", "array");
- schema->SetString("items.type", "string");
- schema->SetInteger("minItems", 2);
- schema->SetInteger("maxItems", 3);
+ schema->SetString(schema::kType, schema::kArray);
+ schema->SetString("items.type", schema::kString);
+ schema->SetInteger(schema::kMinItems, 2);
+ schema->SetInteger(schema::kMaxItems, 3);
scoped_ptr<ListValue> instance(new ListValue());
instance->Append(Value::CreateStringValue("x"));
@@ -394,14 +413,16 @@ void JSONSchemaValidatorTestBase::TestArrayNonTuple() {
instance->Append(Value::CreateIntegerValue(42));
ExpectNotValid(TEST_SOURCE, instance.get(), schema.get(), NULL, "1",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "string", "integer"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kString,
+ schema::kInteger));
}
void JSONSchemaValidatorTestBase::TestString() {
scoped_ptr<DictionaryValue> schema(new DictionaryValue());
- schema->SetString("type", "string");
- schema->SetInteger("minLength", 1);
- schema->SetInteger("maxLength", 10);
+ schema->SetString(schema::kType, schema::kString);
+ schema->SetInteger(schema::kMinLength, 1);
+ schema->SetInteger(schema::kMaxLength, 10);
ExpectValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateStringValue("x")).get(),
@@ -426,9 +447,9 @@ void JSONSchemaValidatorTestBase::TestString() {
void JSONSchemaValidatorTestBase::TestNumber() {
scoped_ptr<DictionaryValue> schema(new DictionaryValue());
- schema->SetString("type", "number");
- schema->SetInteger("minimum", 1);
- schema->SetInteger("maximum", 100);
+ schema->SetString(schema::kType, schema::kNumber);
+ schema->SetInteger(schema::kMinimum, 1);
+ schema->SetInteger(schema::kMaximum, 100);
schema->SetInteger("maxDecimal", 2);
ExpectValid(TEST_SOURCE,
@@ -459,66 +480,76 @@ void JSONSchemaValidatorTestBase::TestNumber() {
}
void JSONSchemaValidatorTestBase::TestTypeClassifier() {
- EXPECT_EQ("boolean", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(Value::CreateBooleanValue(true)).get()));
- EXPECT_EQ("boolean", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(Value::CreateBooleanValue(false)).get()));
+ EXPECT_EQ(std::string(schema::kBoolean),
+ JSONSchemaValidator::GetJSONSchemaType(
+ scoped_ptr<Value>(Value::CreateBooleanValue(true)).get()));
+ EXPECT_EQ(std::string(schema::kBoolean),
+ JSONSchemaValidator::GetJSONSchemaType(
+ scoped_ptr<Value>(Value::CreateBooleanValue(false)).get()));
// It doesn't matter whether the C++ type is 'integer' or 'real'. If the
// number is integral and within the representable range of integers in
// double, it's classified as 'integer'.
- EXPECT_EQ("integer", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(Value::CreateIntegerValue(42)).get()));
- EXPECT_EQ("integer", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(Value::CreateIntegerValue(0)).get()));
- EXPECT_EQ("integer", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(Value::CreateDoubleValue(42)).get()));
- EXPECT_EQ("integer", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(
- Value::CreateDoubleValue(pow(2.0, DBL_MANT_DIG))).get()));
- EXPECT_EQ("integer", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(
- Value::CreateDoubleValue(pow(-2.0, DBL_MANT_DIG))).get()));
+ EXPECT_EQ(std::string(schema::kInteger),
+ JSONSchemaValidator::GetJSONSchemaType(
+ scoped_ptr<Value>(Value::CreateIntegerValue(42)).get()));
+ EXPECT_EQ(std::string(schema::kInteger),
+ JSONSchemaValidator::GetJSONSchemaType(
+ scoped_ptr<Value>(Value::CreateIntegerValue(0)).get()));
+ EXPECT_EQ(std::string(schema::kInteger),
+ JSONSchemaValidator::GetJSONSchemaType(
+ scoped_ptr<Value>(Value::CreateDoubleValue(42)).get()));
+ EXPECT_EQ(std::string(schema::kInteger),
+ JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<Value>(
+ Value::CreateDoubleValue(pow(2.0, DBL_MANT_DIG))).get()));
+ EXPECT_EQ(std::string(schema::kInteger),
+ JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<Value>(
+ Value::CreateDoubleValue(pow(-2.0, DBL_MANT_DIG))).get()));
// "number" is only used for non-integral numbers, or numbers beyond what
// double can accurately represent.
- EXPECT_EQ("number", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get()));
- EXPECT_EQ("number", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(Value::CreateDoubleValue(
- pow(2.0, DBL_MANT_DIG) * 2)).get()));
- EXPECT_EQ("number", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(Value::CreateDoubleValue(
- pow(-2.0, DBL_MANT_DIG) * 2)).get()));
-
- EXPECT_EQ("string", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(Value::CreateStringValue("foo")).get()));
- EXPECT_EQ("array", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(new ListValue()).get()));
- EXPECT_EQ("object", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(new DictionaryValue()).get()));
- EXPECT_EQ("null", JSONSchemaValidator::GetJSONSchemaType(
- scoped_ptr<Value>(Value::CreateNullValue()).get()));
+ EXPECT_EQ(std::string(schema::kNumber),
+ JSONSchemaValidator::GetJSONSchemaType(
+ scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get()));
+ EXPECT_EQ(std::string(schema::kNumber),
+ JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<Value>(
+ Value::CreateDoubleValue(pow(2.0, DBL_MANT_DIG) * 2)).get()));
+ EXPECT_EQ(std::string(schema::kNumber),
+ JSONSchemaValidator::GetJSONSchemaType(scoped_ptr<Value>(
+ Value::CreateDoubleValue(pow(-2.0, DBL_MANT_DIG) * 2)).get()));
+
+ EXPECT_EQ(std::string(schema::kString),
+ JSONSchemaValidator::GetJSONSchemaType(
+ scoped_ptr<Value>(Value::CreateStringValue("foo")).get()));
+ EXPECT_EQ(std::string(schema::kArray),
+ JSONSchemaValidator::GetJSONSchemaType(
+ scoped_ptr<Value>(new ListValue()).get()));
+ EXPECT_EQ(std::string(schema::kObject),
+ JSONSchemaValidator::GetJSONSchemaType(
+ scoped_ptr<Value>(new DictionaryValue()).get()));
+ EXPECT_EQ(std::string(schema::kNull),
+ JSONSchemaValidator::GetJSONSchemaType(
+ scoped_ptr<Value>(Value::CreateNullValue()).get()));
}
void JSONSchemaValidatorTestBase::TestTypes() {
scoped_ptr<DictionaryValue> schema(new DictionaryValue());
// valid
- schema->SetString("type", "object");
+ schema->SetString(schema::kType, schema::kObject);
ExpectValid(TEST_SOURCE, scoped_ptr<Value>(new DictionaryValue()).get(),
schema.get(), NULL);
- schema->SetString("type", "array");
+ schema->SetString(schema::kType, schema::kArray);
ExpectValid(TEST_SOURCE, scoped_ptr<Value>(new ListValue()).get(),
schema.get(), NULL);
- schema->SetString("type", "string");
+ schema->SetString(schema::kType, schema::kString);
ExpectValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateStringValue("foobar")).get(),
schema.get(), NULL);
- schema->SetString("type", "number");
+ schema->SetString(schema::kType, schema::kNumber);
ExpectValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get(),
schema.get(), NULL);
@@ -532,7 +563,7 @@ void JSONSchemaValidatorTestBase::TestTypes() {
scoped_ptr<Value>(Value::CreateIntegerValue(0)).get(),
schema.get(), NULL);
- schema->SetString("type", "integer");
+ schema->SetString(schema::kType, schema::kInteger);
ExpectValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(),
schema.get(), NULL);
@@ -551,7 +582,7 @@ void JSONSchemaValidatorTestBase::TestTypes() {
Value::CreateDoubleValue(pow(-2.0, DBL_MANT_DIG))).get(),
schema.get(), NULL);
- schema->SetString("type", "boolean");
+ schema->SetString(schema::kType, schema::kBoolean);
ExpectValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateBooleanValue(false)).get(),
schema.get(), NULL);
@@ -559,70 +590,88 @@ void JSONSchemaValidatorTestBase::TestTypes() {
scoped_ptr<Value>(Value::CreateBooleanValue(true)).get(),
schema.get(), NULL);
- schema->SetString("type", "null");
+ schema->SetString(schema::kType, schema::kNull);
ExpectValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateNullValue()).get(),
schema.get(), NULL);
// not valid
- schema->SetString("type", "object");
+ schema->SetString(schema::kType, schema::kObject);
ExpectNotValid(TEST_SOURCE, scoped_ptr<Value>(new ListValue()).get(),
schema.get(), NULL, "",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "object", "array"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kObject,
+ schema::kArray));
- schema->SetString("type", "object");
+ schema->SetString(schema::kType, schema::kObject);
ExpectNotValid(TEST_SOURCE, scoped_ptr<Value>(Value::CreateNullValue()).get(),
schema.get(), NULL, "",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "object", "null"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kObject,
+ schema::kNull));
- schema->SetString("type", "array");
+ schema->SetString(schema::kType, schema::kArray);
ExpectNotValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(),
schema.get(), NULL, "",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "array", "integer"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kArray,
+ schema::kInteger));
- schema->SetString("type", "string");
+ schema->SetString(schema::kType, schema::kString);
ExpectNotValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateIntegerValue(42)).get(),
schema.get(), NULL, "",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "string", "integer"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kString,
+ schema::kInteger));
- schema->SetString("type", "number");
+ schema->SetString(schema::kType, schema::kNumber);
ExpectNotValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateStringValue("42")).get(),
schema.get(), NULL, "",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "number", "string"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kNumber,
+ schema::kString));
- schema->SetString("type", "integer");
+ schema->SetString(schema::kType, schema::kInteger);
ExpectNotValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get(),
schema.get(), NULL, "",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "integer", "number"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kInteger,
+ schema::kNumber));
- schema->SetString("type", "integer");
+ schema->SetString(schema::kType, schema::kInteger);
ExpectNotValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateDoubleValue(88.8)).get(),
schema.get(), NULL, "",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "integer", "number"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kInteger,
+ schema::kNumber));
- schema->SetString("type", "boolean");
+ schema->SetString(schema::kType, schema::kBoolean);
ExpectNotValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateIntegerValue(1)).get(),
schema.get(), NULL, "",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "boolean", "integer"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kBoolean,
+ schema::kInteger));
- schema->SetString("type", "null");
+ schema->SetString(schema::kType, schema::kNull);
ExpectNotValid(TEST_SOURCE,
scoped_ptr<Value>(Value::CreateBooleanValue(false)).get(),
schema.get(), NULL, "",
JSONSchemaValidator::FormatErrorMessage(
- JSONSchemaValidator::kInvalidType, "null", "boolean"));
+ JSONSchemaValidator::kInvalidType,
+ schema::kNull,
+ schema::kBoolean));
}