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

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

Issue 6901084: Use new APIs in base/values.h: Value::GetAsNumber and DictionaryValue::GetNumber. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase, 2010->2011 Created 9 years, 7 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/extensions/extension.cc ('k') | chrome/test/webdriver/cookie.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) 2010 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"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "ui/base/l10n/l10n_util.h" 13 #include "ui/base/l10n/l10n_util.h"
14 14
15 namespace { 15 namespace {
16 16
17 double GetNumberValue(Value* value) { 17 double GetNumberValue(Value* value) {
18 double result = 0; 18 double result = 0;
19 if (value->GetAsDouble(&result)) 19 CHECK(value->GetAsDouble(&result))
20 return result; 20 << "Unexpected value type: " << value->GetType();
21 21 return result;
22 int int_result = 0;
23 if (value->GetAsInteger(&int_result)) {
24 return int_result;
25 }
26
27 CHECK(false) << "Unexpected value type: " << value->GetType();
28 return 0;
29 }
30
31 bool GetNumberFromDictionary(DictionaryValue* value, const std::string& key,
32 double* number) {
33 if (value->GetDouble(key, number))
34 return true;
35
36 int int_value = 0;
37 if (value->GetInteger(key, &int_value)) {
38 *number = int_value;
39 return true;
40 }
41
42 return false;
43 } 22 }
44 23
45 } // namespace 24 } // namespace
46 25
47 26
48 JSONSchemaValidator::Error::Error() { 27 JSONSchemaValidator::Error::Error() {
49 } 28 }
50 29
51 JSONSchemaValidator::Error::Error(const std::string& message) 30 JSONSchemaValidator::Error::Error(const std::string& message)
52 : path(message) { 31 : path(message) {
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 431
453 void JSONSchemaValidator::ValidateNumber(Value* instance, 432 void JSONSchemaValidator::ValidateNumber(Value* instance,
454 DictionaryValue* schema, 433 DictionaryValue* schema,
455 const std::string& path) { 434 const std::string& path) {
456 double value = GetNumberValue(instance); 435 double value = GetNumberValue(instance);
457 436
458 // TODO(aa): It would be good to test that the double is not infinity or nan, 437 // TODO(aa): It would be good to test that the double is not infinity or nan,
459 // but isnan and isinf aren't defined on Windows. 438 // but isnan and isinf aren't defined on Windows.
460 439
461 double minimum = 0; 440 double minimum = 0;
462 if (GetNumberFromDictionary(schema, "minimum", &minimum)) { 441 if (schema->GetDouble("minimum", &minimum)) {
463 if (value < minimum) 442 if (value < minimum)
464 errors_.push_back(Error(path, FormatErrorMessage( 443 errors_.push_back(Error(path, FormatErrorMessage(
465 kNumberMinimum, base::DoubleToString(minimum)))); 444 kNumberMinimum, base::DoubleToString(minimum))));
466 } 445 }
467 446
468 double maximum = 0; 447 double maximum = 0;
469 if (GetNumberFromDictionary(schema, "maximum", &maximum)) { 448 if (schema->GetDouble("maximum", &maximum)) {
470 if (value > maximum) 449 if (value > maximum)
471 errors_.push_back(Error(path, FormatErrorMessage( 450 errors_.push_back(Error(path, FormatErrorMessage(
472 kNumberMaximum, base::DoubleToString(maximum)))); 451 kNumberMaximum, base::DoubleToString(maximum))));
473 } 452 }
474 } 453 }
475 454
476 bool JSONSchemaValidator::ValidateType(Value* instance, 455 bool JSONSchemaValidator::ValidateType(Value* instance,
477 const std::string& expected_type, 456 const std::string& expected_type,
478 const std::string& path) { 457 const std::string& path) {
479 std::string actual_type = GetJSONSchemaType(instance); 458 std::string actual_type = GetJSONSchemaType(instance);
(...skipping 15 matching lines...) Expand all
495 474
496 if (*additional_properties_schema) { 475 if (*additional_properties_schema) {
497 std::string additional_properties_type("any"); 476 std::string additional_properties_type("any");
498 CHECK((*additional_properties_schema)->GetString( 477 CHECK((*additional_properties_schema)->GetString(
499 "type", &additional_properties_type)); 478 "type", &additional_properties_type));
500 return additional_properties_type == "any"; 479 return additional_properties_type == "any";
501 } else { 480 } else {
502 return default_allow_additional_properties_; 481 return default_allow_additional_properties_;
503 } 482 }
504 } 483 }
OLDNEW
« no previous file with comments | « chrome/common/extensions/extension.cc ('k') | chrome/test/webdriver/cookie.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698