summaryrefslogtreecommitdiffstats
path: root/components/json_schema/json_schema_validator.cc
diff options
context:
space:
mode:
authorsammc@chromium.org <sammc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-01 05:27:27 +0000
committersammc@chromium.org <sammc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-01 05:27:27 +0000
commit7f138d4ae1a77eb21fa168639249abf6ac88a30f (patch)
treef96fb0a7c04230ff275077e565df4ceaaca8b59d /components/json_schema/json_schema_validator.cc
parentf89afc12a8a47abaebce4948ffe1656e06680a39 (diff)
downloadchromium_src-7f138d4ae1a77eb21fa168639249abf6ac88a30f.zip
chromium_src-7f138d4ae1a77eb21fa168639249abf6ac88a30f.tar.gz
chromium_src-7f138d4ae1a77eb21fa168639249abf6ac88a30f.tar.bz2
Docserver: Display enum value descriptions in API docs.
This modifies the json schema to allow both primitive types and dictionaries with properties "name" and optional "description" for enum values. BUG=310454 Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=231982 Review URL: https://codereview.chromium.org/39113003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@232297 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components/json_schema/json_schema_validator.cc')
-rw-r--r--components/json_schema/json_schema_validator.cc25
1 files changed, 25 insertions, 0 deletions
diff --git a/components/json_schema/json_schema_validator.cc b/components/json_schema/json_schema_validator.cc
index 3816a76..3cc9e2b 100644
--- a/components/json_schema/json_schema_validator.cc
+++ b/components/json_schema/json_schema_validator.cc
@@ -53,6 +53,18 @@ bool CompareToString(const ExpectedType& entry, const std::string& key) {
return entry.key < key;
}
+// If |value| is a dictionary, returns the "name" attribute of |value| or NULL
+// if |value| does not contain a "name" attribute. Otherwise, returns |value|.
+const base::Value* ExtractNameFromDictionary(const base::Value* value) {
+ const base::DictionaryValue* value_dict = NULL;
+ const base::Value* name_value = NULL;
+ if (value->GetAsDictionary(&value_dict)) {
+ value_dict->Get("name", &name_value);
+ return name_value;
+ }
+ return value;
+}
+
bool IsValidSchema(const base::DictionaryValue* dict, std::string* error) {
// This array must be sorted, so that std::lower_bound can perform a
// binary search.
@@ -195,6 +207,13 @@ bool IsValidSchema(const base::DictionaryValue* dict, std::string* error) {
for (size_t i = 0; i < list_value->GetSize(); ++i) {
const base::Value* value = NULL;
list_value->Get(i, &value);
+ // Sometimes the enum declaration is a dictionary with the enum value
+ // under "name".
+ value = ExtractNameFromDictionary(value);
+ if (!value) {
+ *error = "Invalid value in enum attribute";
+ return false;
+ }
switch (value->GetType()) {
case base::Value::TYPE_NULL:
case base::Value::TYPE_BOOLEAN:
@@ -479,6 +498,12 @@ void JSONSchemaValidator::ValidateEnum(const base::Value* instance,
for (size_t i = 0; i < choices->GetSize(); ++i) {
const base::Value* choice = NULL;
CHECK(choices->Get(i, &choice));
+ // Sometimes the enum declaration is a dictionary with the enum value under
+ // "name".
+ choice = ExtractNameFromDictionary(choice);
+ if (!choice) {
+ NOTREACHED();
+ }
switch (choice->GetType()) {
case base::Value::TYPE_NULL:
case base::Value::TYPE_BOOLEAN: