OLD | NEW |
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/json_schema_validator.h" | 5 #include "components/json_schema/json_schema_validator.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cfloat> | 8 #include <cfloat> |
9 #include <cmath> | 9 #include <cmath> |
10 | 10 |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/common/json_schema/json_schema_constants.h" | 16 #include "components/json_schema/json_schema_constants.h" |
17 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
18 | 18 |
19 namespace schema = json_schema_constants; | 19 namespace schema = json_schema_constants; |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 double GetNumberValue(const base::Value* value) { | 23 double GetNumberValue(const base::Value* value) { |
24 double result = 0; | 24 double result = 0; |
25 CHECK(value->GetAsDouble(&result)) | 25 CHECK(value->GetAsDouble(&result)) |
26 << "Unexpected value type: " << value->GetType(); | 26 << "Unexpected value type: " << value->GetType(); |
(...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
572 errors_.push_back(Error(path, FormatErrorMessage( | 572 errors_.push_back(Error(path, FormatErrorMessage( |
573 kArrayMaxItems, base::IntToString(max_items)))); | 573 kArrayMaxItems, base::IntToString(max_items)))); |
574 } | 574 } |
575 } | 575 } |
576 | 576 |
577 // If the items property is a single schema, each item in the array must | 577 // If the items property is a single schema, each item in the array must |
578 // validate against that schema. | 578 // validate against that schema. |
579 for (size_t i = 0; i < instance_size; ++i) { | 579 for (size_t i = 0; i < instance_size; ++i) { |
580 const base::Value* item = NULL; | 580 const base::Value* item = NULL; |
581 CHECK(instance->Get(i, &item)); | 581 CHECK(instance->Get(i, &item)); |
582 std::string i_str = base::UintToString(i); | 582 std::string i_str = base::Uint64ToString(i); |
583 std::string item_path = path.empty() ? i_str : (path + "." + i_str); | 583 std::string item_path = path.empty() ? i_str : (path + "." + i_str); |
584 Validate(item, single_type, item_path); | 584 Validate(item, single_type, item_path); |
585 } | 585 } |
586 | 586 |
587 return; | 587 return; |
588 } | 588 } |
589 | 589 |
590 // Otherwise, the list must be a tuple type, where each item in the list has a | 590 // Otherwise, the list must be a tuple type, where each item in the list has a |
591 // particular schema. | 591 // particular schema. |
592 ValidateTuple(instance, schema, path); | 592 ValidateTuple(instance, schema, path); |
593 } | 593 } |
594 | 594 |
595 void JSONSchemaValidator::ValidateTuple(const base::ListValue* instance, | 595 void JSONSchemaValidator::ValidateTuple(const base::ListValue* instance, |
596 const base::DictionaryValue* schema, | 596 const base::DictionaryValue* schema, |
597 const std::string& path) { | 597 const std::string& path) { |
598 const base::ListValue* tuple_type = NULL; | 598 const base::ListValue* tuple_type = NULL; |
599 schema->GetList(schema::kItems, &tuple_type); | 599 schema->GetList(schema::kItems, &tuple_type); |
600 size_t tuple_size = tuple_type ? tuple_type->GetSize() : 0; | 600 size_t tuple_size = tuple_type ? tuple_type->GetSize() : 0; |
601 if (tuple_type) { | 601 if (tuple_type) { |
602 for (size_t i = 0; i < tuple_size; ++i) { | 602 for (size_t i = 0; i < tuple_size; ++i) { |
603 std::string i_str = base::UintToString(i); | 603 std::string i_str = base::Uint64ToString(i); |
604 std::string item_path = path.empty() ? i_str : (path + "." + i_str); | 604 std::string item_path = path.empty() ? i_str : (path + "." + i_str); |
605 const base::DictionaryValue* item_schema = NULL; | 605 const base::DictionaryValue* item_schema = NULL; |
606 CHECK(tuple_type->GetDictionary(i, &item_schema)); | 606 CHECK(tuple_type->GetDictionary(i, &item_schema)); |
607 const base::Value* item_value = NULL; | 607 const base::Value* item_value = NULL; |
608 instance->Get(i, &item_value); | 608 instance->Get(i, &item_value); |
609 if (item_value && item_value->GetType() != base::Value::TYPE_NULL) { | 609 if (item_value && item_value->GetType() != base::Value::TYPE_NULL) { |
610 Validate(item_value, item_schema, item_path); | 610 Validate(item_value, item_schema, item_path); |
611 } else { | 611 } else { |
612 bool is_optional = false; | 612 bool is_optional = false; |
613 item_schema->GetBoolean(schema::kOptional, &is_optional); | 613 item_schema->GetBoolean(schema::kOptional, &is_optional); |
614 if (!is_optional) { | 614 if (!is_optional) { |
615 errors_.push_back(Error(item_path, kArrayItemRequired)); | 615 errors_.push_back(Error(item_path, kArrayItemRequired)); |
616 return; | 616 return; |
617 } | 617 } |
618 } | 618 } |
619 } | 619 } |
620 } | 620 } |
621 | 621 |
622 const base::DictionaryValue* additional_properties_schema = NULL; | 622 const base::DictionaryValue* additional_properties_schema = NULL; |
623 if (SchemaAllowsAnyAdditionalItems(schema, &additional_properties_schema)) | 623 if (SchemaAllowsAnyAdditionalItems(schema, &additional_properties_schema)) |
624 return; | 624 return; |
625 | 625 |
626 size_t instance_size = instance->GetSize(); | 626 size_t instance_size = instance->GetSize(); |
627 if (additional_properties_schema) { | 627 if (additional_properties_schema) { |
628 // Any additional properties must validate against the additionalProperties | 628 // Any additional properties must validate against the additionalProperties |
629 // schema. | 629 // schema. |
630 for (size_t i = tuple_size; i < instance_size; ++i) { | 630 for (size_t i = tuple_size; i < instance_size; ++i) { |
631 std::string i_str = base::UintToString(i); | 631 std::string i_str = base::Uint64ToString(i); |
632 std::string item_path = path.empty() ? i_str : (path + "." + i_str); | 632 std::string item_path = path.empty() ? i_str : (path + "." + i_str); |
633 const base::Value* item_value = NULL; | 633 const base::Value* item_value = NULL; |
634 CHECK(instance->Get(i, &item_value)); | 634 CHECK(instance->Get(i, &item_value)); |
635 Validate(item_value, additional_properties_schema, item_path); | 635 Validate(item_value, additional_properties_schema, item_path); |
636 } | 636 } |
637 } else if (instance_size > tuple_size) { | 637 } else if (instance_size > tuple_size) { |
638 errors_.push_back(Error(path, FormatErrorMessage( | 638 errors_.push_back(Error(path, FormatErrorMessage( |
639 kArrayMaxItems, base::UintToString(tuple_size)))); | 639 kArrayMaxItems, base::Uint64ToString(tuple_size)))); |
640 } | 640 } |
641 } | 641 } |
642 | 642 |
643 void JSONSchemaValidator::ValidateString(const base::Value* instance, | 643 void JSONSchemaValidator::ValidateString(const base::Value* instance, |
644 const base::DictionaryValue* schema, | 644 const base::DictionaryValue* schema, |
645 const std::string& path) { | 645 const std::string& path) { |
646 std::string value; | 646 std::string value; |
647 CHECK(instance->GetAsString(&value)); | 647 CHECK(instance->GetAsString(&value)); |
648 | 648 |
649 int min_length = 0; | 649 int min_length = 0; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
718 | 718 |
719 if (*additional_properties_schema) { | 719 if (*additional_properties_schema) { |
720 std::string additional_properties_type(schema::kAny); | 720 std::string additional_properties_type(schema::kAny); |
721 CHECK((*additional_properties_schema)->GetString( | 721 CHECK((*additional_properties_schema)->GetString( |
722 schema::kType, &additional_properties_type)); | 722 schema::kType, &additional_properties_type)); |
723 return additional_properties_type == schema::kAny; | 723 return additional_properties_type == schema::kAny; |
724 } else { | 724 } else { |
725 return default_allow_additional_properties_; | 725 return default_allow_additional_properties_; |
726 } | 726 } |
727 } | 727 } |
OLD | NEW |