Index: chrome/common/json_schema_validator.h |
diff --git a/chrome/common/json_schema_validator.h b/chrome/common/json_schema_validator.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a6a9d0f8cd801e0f4e9efe2f755b5e7fa5e285b1 |
--- /dev/null |
+++ b/chrome/common/json_schema_validator.h |
@@ -0,0 +1,144 @@ |
+// Copyright (c) 2010 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_VALIDATOR_H_ |
+#define CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_ |
+ |
+#include <map> |
+#include <string> |
+#include <vector> |
+ |
+#include "base/basictypes.h" |
+ |
+class DictionaryValue; |
+class FundamentalValue; |
+class ListValue; |
+class StringValue; |
+class Value; |
+ |
+class JSONSchemaValidator { |
asargent_no_longer_on_chrome
2010/11/08 23:47:18
It would probably be worth having a top-level comm
Aaron Boodman
2010/11/10 19:03:35
Done.
|
+ public: |
+ // Details about a validation error. |
+ struct Error { |
+ Error(); |
+ |
+ explicit Error(const std::string& message); |
+ |
+ Error(const std::string& path, const std::string& message); |
+ |
+ // The path to the location of the error in the JSON structure. |
+ std::string path; |
+ |
+ // An english message describing the error. |
+ std::string message; |
+ }; |
+ |
+ // Error messages. |
+ static const char kUnknownTypeReference[]; |
+ static const char kInvalidChoice[]; |
+ static const char kInvalidEnum[]; |
+ static const char kObjectPropertyIsRequired[]; |
+ static const char kUnexpectedProperty[]; |
+ static const char kArrayMinItems[]; |
+ static const char kArrayMaxItems[]; |
+ static const char kArrayItemRequired[]; |
+ static const char kStringMinLength[]; |
+ static const char kStringMaxLength[]; |
+ static const char kStringPattern[]; |
+ static const char kInfinityNaNNotSupported[]; |
+ static const char kNumberMinimum[]; |
+ static const char kNumberMaximum[]; |
+ static const char kNumberDecimalPlaces[]; |
+ static const char kInvalidType[]; |
+ |
+ static std::string FormatErrorMessage(const std::string& format, |
asargent_no_longer_on_chrome
2010/11/08 23:47:18
nit: Document what s1 (and below, s1,s2) are used
Aaron Boodman
2010/11/10 19:03:35
Done.
|
+ const std::string& s1); |
+ static std::string FormatErrorMessage(const std::string& format, |
+ const std::string& s1, |
+ const std::string& s2); |
+ |
+ // Creates a validator for the specified schema. |
+ JSONSchemaValidator(DictionaryValue* schema); |
+ |
+ // Creates a validator for the specified schema and user-defined types. Each |
+ // type must have an "id" property that can be referred to within the schema |
asargent_no_longer_on_chrome
2010/11/08 23:47:18
nit: In this comment, consider replacing
"Each typ
Aaron Boodman
2010/11/10 19:03:35
Done.
|
+ // with "ref". |
+ JSONSchemaValidator(DictionaryValue* schema, ListValue* types); |
+ |
+ // Whether the validator allows additional items for objects and lists, beyond |
+ // those defined by their schema, by default. |
+ // |
+ // This setting defaults to false: all items in an instance list or object |
+ // must be defined by the corresponding schema. |
+ // |
+ // This setting can be overridden on individual object and list schemas by |
+ // setting the "additionalProperties" field. |
+ bool default_allow_additional_properties() const { |
+ return default_allow_additional_properties_; |
+ } |
+ |
+ void set_default_allow_additional_properties(bool val) { |
+ default_allow_additional_properties_ = val; |
+ } |
+ |
+ // Returns any errors from the last call to to Validate(). |
+ const std::vector<Error>& errors() const { |
+ return errors_; |
+ } |
+ |
+ // Validates a JSON value. Returns true if the instance is valid, false |
+ // otherwise. If false is returned any errors are available from the errors() |
+ // getter. |
+ bool Validate(Value* instance); |
+ |
+ private: |
+ typedef std::map<std::string, DictionaryValue*> TypeMap; |
+ |
+ void Validate(Value* instance, DictionaryValue* schema, |
+ const std::string& path); |
asargent_no_longer_on_chrome
2010/11/08 23:47:18
The way that paths work is a little bit subtle, an
Aaron Boodman
2010/11/10 19:03:35
Done.
|
+ |
+ void ValidateChoices(Value* instance, ListValue* choices, |
+ const std::string& path); |
+ |
+ void ValidateEnum(Value* instance, ListValue* choices, |
asargent_no_longer_on_chrome
2010/11/08 23:47:18
consider adding a comment here about the differenc
Aaron Boodman
2010/11/10 19:03:35
Done.
|
+ const std::string& path); |
+ |
+ void ValidateObject(DictionaryValue* instance, DictionaryValue* schema, |
+ const std::string& path); |
+ |
+ void ValidateArray(ListValue* instance, DictionaryValue* schema, |
+ const std::string& path); |
+ |
+ void ValidateString(StringValue* instance, DictionaryValue* schema, |
+ const std::string& path); |
+ |
+ void ValidateNumber(Value* instance, DictionaryValue* schema, |
+ const std::string& path); |
+ |
+ void AddError(int error_id, const std::vector<std::string>& replacements); |
+ |
+ bool ValidateType(Value* instance, const std::string& expected_type, |
+ const std::string& path); |
+ |
+ bool SchemaAllowsAnyAdditionalItems( |
+ DictionaryValue* schema, DictionaryValue** addition_items_schema); |
+ |
+ // The root schema node. |
+ DictionaryValue* schema_root_; |
+ |
+ // Map of user-defined name to type. |
+ TypeMap types_; |
+ |
+ // Whether we allow additional properties on objects by default. This can be |
+ // overridden by the allow_additional_properties flag on an Object schema. |
+ bool default_allow_additional_properties_; |
+ |
+ // Errors accumulated since the last call to Validate(). |
+ std::vector<Error> errors_; |
+ |
+ |
+ DISALLOW_COPY_AND_ASSIGN(JSONSchemaValidator); |
+}; |
+ |
+#endif // CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_ |