Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(515)

Side by Side Diff: chrome/common/json_schema_validator.cc

Issue 8368018: Convert chrome/common non-debug logs to debug logs. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/common/json_pref_store.cc ('k') | chrome/common/logging_chrome.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 return "number"; 86 return "number";
87 } 87 }
88 } 88 }
89 case Value::TYPE_STRING: 89 case Value::TYPE_STRING:
90 return "string"; 90 return "string";
91 case Value::TYPE_DICTIONARY: 91 case Value::TYPE_DICTIONARY:
92 return "object"; 92 return "object";
93 case Value::TYPE_LIST: 93 case Value::TYPE_LIST:
94 return "array"; 94 return "array";
95 default: 95 default:
96 CHECK(false) << "Unexpected value type: " << value->GetType(); 96 NOTREACHED() << "Unexpected value type: " << value->GetType();
97 return ""; 97 return "";
98 } 98 }
99 } 99 }
100 100
101 // static 101 // static
102 std::string JSONSchemaValidator::FormatErrorMessage(const std::string& format, 102 std::string JSONSchemaValidator::FormatErrorMessage(const std::string& format,
103 const std::string& s1) { 103 const std::string& s1) {
104 std::string ret_val = format; 104 std::string ret_val = format;
105 ReplaceFirstSubstringAfterOffset(&ret_val, 0, "*", s1); 105 ReplaceFirstSubstringAfterOffset(&ret_val, 0, "*", s1);
106 return ret_val; 106 return ret_val;
(...skipping 19 matching lines...) Expand all
126 if (!types) 126 if (!types)
127 return; 127 return;
128 128
129 for (size_t i = 0; i < types->GetSize(); ++i) { 129 for (size_t i = 0; i < types->GetSize(); ++i) {
130 DictionaryValue* type = NULL; 130 DictionaryValue* type = NULL;
131 CHECK(types->GetDictionary(i, &type)); 131 CHECK(types->GetDictionary(i, &type));
132 132
133 std::string id; 133 std::string id;
134 CHECK(type->GetString("id", &id)); 134 CHECK(type->GetString("id", &id));
135 135
136 CHECK(types_.find(id) == types_.end()); 136 DCHECK(types_.find(id) == types_.end());
137 types_[id] = type; 137 types_[id] = type;
138 } 138 }
139 } 139 }
140 140
141 JSONSchemaValidator::~JSONSchemaValidator() {} 141 JSONSchemaValidator::~JSONSchemaValidator() {}
142 142
143 bool JSONSchemaValidator::Validate(Value* instance) { 143 bool JSONSchemaValidator::Validate(Value* instance) {
144 errors_.clear(); 144 errors_.clear();
145 Validate(instance, schema_root_, ""); 145 Validate(instance, schema_root_, "");
146 return errors_.empty(); 146 return errors_.empty();
147 } 147 }
148 148
149 void JSONSchemaValidator::Validate(Value* instance, 149 void JSONSchemaValidator::Validate(Value* instance,
150 DictionaryValue* schema, 150 DictionaryValue* schema,
151 const std::string& path) { 151 const std::string& path) {
152 // If this schema defines itself as reference type, save it in this.types. 152 // If this schema defines itself as reference type, save it in this.types.
153 std::string id; 153 std::string id;
154 if (schema->GetString("id", &id)) { 154 if (schema->GetString("id", &id)) {
155 TypeMap::iterator iter = types_.find(id); 155 TypeMap::iterator iter = types_.find(id);
156 if (iter == types_.end()) 156 if (iter == types_.end())
157 types_[id] = schema; 157 types_[id] = schema;
158 else 158 else
159 CHECK(iter->second == schema); 159 DCHECK(iter->second == schema);
160 } 160 }
161 161
162 // If the schema has a $ref property, the instance must validate against 162 // If the schema has a $ref property, the instance must validate against
163 // that schema. It must be present in types_ to be referenced. 163 // that schema. It must be present in types_ to be referenced.
164 std::string ref; 164 std::string ref;
165 if (schema->GetString("$ref", &ref)) { 165 if (schema->GetString("$ref", &ref)) {
166 TypeMap::iterator type = types_.find(ref); 166 TypeMap::iterator type = types_.find(ref);
167 if (type == types_.end()) { 167 if (type == types_.end()) {
168 errors_.push_back( 168 errors_.push_back(
169 Error(path, FormatErrorMessage(kUnknownTypeReference, ref))); 169 Error(path, FormatErrorMessage(kUnknownTypeReference, ref)));
(...skipping 14 matching lines...) Expand all
184 // If the schema has an enum property, the instance must be one of those 184 // If the schema has an enum property, the instance must be one of those
185 // values. 185 // values.
186 ListValue* enumeration = NULL; 186 ListValue* enumeration = NULL;
187 if (schema->GetList("enum", &enumeration)) { 187 if (schema->GetList("enum", &enumeration)) {
188 ValidateEnum(instance, enumeration, path); 188 ValidateEnum(instance, enumeration, path);
189 return; 189 return;
190 } 190 }
191 191
192 std::string type; 192 std::string type;
193 schema->GetString("type", &type); 193 schema->GetString("type", &type);
194 CHECK(!type.empty()); 194 DCHECK(!type.empty());
195 if (type != "any") { 195 if (type != "any") {
196 if (!ValidateType(instance, type, path)) 196 if (!ValidateType(instance, type, path))
197 return; 197 return;
198 198
199 // These casts are safe because of checks in ValidateType(). 199 // These casts are safe because of checks in ValidateType().
200 if (type == "object") 200 if (type == "object")
201 ValidateObject(static_cast<DictionaryValue*>(instance), schema, path); 201 ValidateObject(static_cast<DictionaryValue*>(instance), schema, path);
202 else if (type == "array") 202 else if (type == "array")
203 ValidateArray(static_cast<ListValue*>(instance), schema, path); 203 ValidateArray(static_cast<ListValue*>(instance), schema, path);
204 else if (type == "string") 204 else if (type == "string")
205 ValidateString(static_cast<StringValue*>(instance), schema, path); 205 ValidateString(static_cast<StringValue*>(instance), schema, path);
206 else if (type == "number" || type == "integer") 206 else if (type == "number" || type == "integer")
207 ValidateNumber(instance, schema, path); 207 ValidateNumber(instance, schema, path);
208 else if (type != "boolean" && type != "null") 208 else if (type != "boolean" && type != "null")
209 CHECK(false) << "Unexpected type: " << type; 209 NOTREACHED() << "Unexpected type: " << type;
210 } 210 }
211 } 211 }
212 212
213 void JSONSchemaValidator::ValidateChoices(Value* instance, 213 void JSONSchemaValidator::ValidateChoices(Value* instance,
214 ListValue* choices, 214 ListValue* choices,
215 const std::string& path) { 215 const std::string& path) {
216 size_t original_num_errors = errors_.size(); 216 size_t original_num_errors = errors_.size();
217 217
218 for (size_t i = 0; i < choices->GetSize(); ++i) { 218 for (size_t i = 0; i < choices->GetSize(); ++i) {
219 DictionaryValue* choice = NULL; 219 DictionaryValue* choice = NULL;
(...skipping 30 matching lines...) Expand all
250 case Value::TYPE_INTEGER: 250 case Value::TYPE_INTEGER:
251 case Value::TYPE_DOUBLE: 251 case Value::TYPE_DOUBLE:
252 if (instance->IsType(Value::TYPE_INTEGER) || 252 if (instance->IsType(Value::TYPE_INTEGER) ||
253 instance->IsType(Value::TYPE_DOUBLE)) { 253 instance->IsType(Value::TYPE_DOUBLE)) {
254 if (GetNumberValue(choice) == GetNumberValue(instance)) 254 if (GetNumberValue(choice) == GetNumberValue(instance))
255 return; 255 return;
256 } 256 }
257 break; 257 break;
258 258
259 default: 259 default:
260 CHECK(false) << "Unexpected type in enum: " << choice->GetType(); 260 NOTREACHED() << "Unexpected type in enum: " << choice->GetType();
261 } 261 }
262 } 262 }
263 263
264 errors_.push_back(Error(path, kInvalidEnum)); 264 errors_.push_back(Error(path, kInvalidEnum));
265 } 265 }
266 266
267 void JSONSchemaValidator::ValidateObject(DictionaryValue* instance, 267 void JSONSchemaValidator::ValidateObject(DictionaryValue* instance,
268 DictionaryValue* schema, 268 DictionaryValue* schema,
269 const std::string& path) { 269 const std::string& path) {
270 DictionaryValue* properties = NULL; 270 DictionaryValue* properties = NULL;
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 } 313 }
314 314
315 void JSONSchemaValidator::ValidateArray(ListValue* instance, 315 void JSONSchemaValidator::ValidateArray(ListValue* instance,
316 DictionaryValue* schema, 316 DictionaryValue* schema,
317 const std::string& path) { 317 const std::string& path) {
318 DictionaryValue* single_type = NULL; 318 DictionaryValue* single_type = NULL;
319 size_t instance_size = instance->GetSize(); 319 size_t instance_size = instance->GetSize();
320 if (schema->GetDictionary("items", &single_type)) { 320 if (schema->GetDictionary("items", &single_type)) {
321 int min_items = 0; 321 int min_items = 0;
322 if (schema->GetInteger("minItems", &min_items)) { 322 if (schema->GetInteger("minItems", &min_items)) {
323 CHECK(min_items >= 0); 323 DCHECK(min_items >= 0);
324 if (instance_size < static_cast<size_t>(min_items)) { 324 if (instance_size < static_cast<size_t>(min_items)) {
325 errors_.push_back(Error(path, FormatErrorMessage( 325 errors_.push_back(Error(path, FormatErrorMessage(
326 kArrayMinItems, base::IntToString(min_items)))); 326 kArrayMinItems, base::IntToString(min_items))));
327 } 327 }
328 } 328 }
329 329
330 int max_items = 0; 330 int max_items = 0;
331 if (schema->GetInteger("maxItems", &max_items)) { 331 if (schema->GetInteger("maxItems", &max_items)) {
332 CHECK(max_items >= 0); 332 DCHECK(max_items >= 0);
333 if (instance_size > static_cast<size_t>(max_items)) { 333 if (instance_size > static_cast<size_t>(max_items)) {
334 errors_.push_back(Error(path, FormatErrorMessage( 334 errors_.push_back(Error(path, FormatErrorMessage(
335 kArrayMaxItems, base::IntToString(max_items)))); 335 kArrayMaxItems, base::IntToString(max_items))));
336 } 336 }
337 } 337 }
338 338
339 // If the items property is a single schema, each item in the array must 339 // If the items property is a single schema, each item in the array must
340 // validate against that schema. 340 // validate against that schema.
341 for (size_t i = 0; i < instance_size; ++i) { 341 for (size_t i = 0; i < instance_size; ++i) {
342 Value* item = NULL; 342 Value* item = NULL;
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 474
475 if (*additional_properties_schema) { 475 if (*additional_properties_schema) {
476 std::string additional_properties_type("any"); 476 std::string additional_properties_type("any");
477 CHECK((*additional_properties_schema)->GetString( 477 CHECK((*additional_properties_schema)->GetString(
478 "type", &additional_properties_type)); 478 "type", &additional_properties_type));
479 return additional_properties_type == "any"; 479 return additional_properties_type == "any";
480 } else { 480 } else {
481 return default_allow_additional_properties_; 481 return default_allow_additional_properties_;
482 } 482 }
483 } 483 }
OLDNEW
« no previous file with comments | « chrome/common/json_pref_store.cc ('k') | chrome/common/logging_chrome.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698