| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef COMPONENTS_JSON_SCHEMA_JSON_SCHEMA_VALIDATOR_H_ | 5 #ifndef COMPONENTS_JSON_SCHEMA_JSON_SCHEMA_VALIDATOR_H_ |
| 6 #define COMPONENTS_JSON_SCHEMA_JSON_SCHEMA_VALIDATOR_H_ | 6 #define COMPONENTS_JSON_SCHEMA_JSON_SCHEMA_VALIDATOR_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 // | 25 // |
| 26 // There is also an older JavaScript implementation of the same functionality in | 26 // There is also an older JavaScript implementation of the same functionality in |
| 27 // chrome/renderer/resources/json_schema.js. | 27 // chrome/renderer/resources/json_schema.js. |
| 28 // | 28 // |
| 29 // The following features of JSON Schema are not implemented: | 29 // The following features of JSON Schema are not implemented: |
| 30 // - requires | 30 // - requires |
| 31 // - unique | 31 // - unique |
| 32 // - disallow | 32 // - disallow |
| 33 // - union types (but replaced with 'choices') | 33 // - union types (but replaced with 'choices') |
| 34 // - number.maxDecimal | 34 // - number.maxDecimal |
| 35 // - string.pattern | |
| 36 // | 35 // |
| 37 // The following properties are not applicable to the interface exposed by | 36 // The following properties are not applicable to the interface exposed by |
| 38 // this class: | 37 // this class: |
| 39 // - options | 38 // - options |
| 40 // - readonly | 39 // - readonly |
| 41 // - title | 40 // - title |
| 42 // - description | 41 // - description |
| 43 // - format | 42 // - format |
| 44 // - default | 43 // - default |
| 45 // - transient | 44 // - transient |
| 46 // - hidden | 45 // - hidden |
| 47 // | 46 // |
| 48 // There are also these departures from the JSON Schema proposal: | 47 // There are also these departures from the JSON Schema proposal: |
| 49 // - null counts as 'unspecified' for optional values | 48 // - null counts as 'unspecified' for optional values |
| 50 // - added the 'choices' property, to allow specifying a list of possible types | 49 // - added the 'choices' property, to allow specifying a list of possible types |
| 51 // for a value | 50 // for a value |
| 52 // - by default an "object" typed schema does not allow additional properties. | 51 // - by default an "object" typed schema does not allow additional properties. |
| 53 // if present, "additionalProperties" is to be a schema against which all | 52 // if present, "additionalProperties" is to be a schema against which all |
| 54 // additional properties will be validated. | 53 // additional properties will be validated. |
| 54 // - regular expression supports all syntaxes that re2 accepts. |
| 55 // See https://code.google.com/p/re2/wiki/Syntax for details. |
| 55 //============================================================================== | 56 //============================================================================== |
| 56 class JSONSchemaValidator { | 57 class JSONSchemaValidator { |
| 57 public: | 58 public: |
| 58 // Details about a validation error. | 59 // Details about a validation error. |
| 59 struct Error { | 60 struct Error { |
| 60 Error(); | 61 Error(); |
| 61 | 62 |
| 62 explicit Error(const std::string& message); | 63 explicit Error(const std::string& message); |
| 63 | 64 |
| 64 Error(const std::string& path, const std::string& message); | 65 Error(const std::string& path, const std::string& message); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 85 static const char kArrayMinItems[]; | 86 static const char kArrayMinItems[]; |
| 86 static const char kArrayMaxItems[]; | 87 static const char kArrayMaxItems[]; |
| 87 static const char kArrayItemRequired[]; | 88 static const char kArrayItemRequired[]; |
| 88 static const char kStringMinLength[]; | 89 static const char kStringMinLength[]; |
| 89 static const char kStringMaxLength[]; | 90 static const char kStringMaxLength[]; |
| 90 static const char kStringPattern[]; | 91 static const char kStringPattern[]; |
| 91 static const char kNumberMinimum[]; | 92 static const char kNumberMinimum[]; |
| 92 static const char kNumberMaximum[]; | 93 static const char kNumberMaximum[]; |
| 93 static const char kInvalidType[]; | 94 static const char kInvalidType[]; |
| 94 static const char kInvalidTypeIntegerNumber[]; | 95 static const char kInvalidTypeIntegerNumber[]; |
| 96 static const char kInvalidRegex[]; |
| 95 | 97 |
| 96 // Classifies a Value as one of the JSON schema primitive types. | 98 // Classifies a Value as one of the JSON schema primitive types. |
| 97 static std::string GetJSONSchemaType(const base::Value* value); | 99 static std::string GetJSONSchemaType(const base::Value* value); |
| 98 | 100 |
| 99 // Utility methods to format error messages. The first method can have one | 101 // Utility methods to format error messages. The first method can have one |
| 100 // wildcard represented by '*', which is replaced with s1. The second method | 102 // wildcard represented by '*', which is replaced with s1. The second method |
| 101 // can have two, which are replaced by s1 and s2. | 103 // can have two, which are replaced by s1 and s2. |
| 102 static std::string FormatErrorMessage(const std::string& format, | 104 static std::string FormatErrorMessage(const std::string& format, |
| 103 const std::string& s1); | 105 const std::string& s1); |
| 104 static std::string FormatErrorMessage(const std::string& format, | 106 static std::string FormatErrorMessage(const std::string& format, |
| 105 const std::string& s1, | 107 const std::string& s1, |
| 106 const std::string& s2); | 108 const std::string& s2); |
| 107 | 109 |
| 108 // Verifies if |schema| is a valid JSON v3 schema. When this validation passes | 110 // Verifies if |schema| is a valid JSON v3 schema. When this validation passes |
| 109 // then |schema| is valid JSON that can be parsed into a DictionaryValue, | 111 // then |schema| is valid JSON that can be parsed into a DictionaryValue, |
| 110 // and that DictionaryValue can be used to build a JSONSchemaValidator. | 112 // and that DictionaryValue can be used to build a JSONSchemaValidator. |
| 111 // Returns the parsed DictionaryValue when |schema| validated, otherwise | 113 // Returns the parsed DictionaryValue when |schema| validated, otherwise |
| 112 // returns NULL. In that case, |error| contains an error description. | 114 // returns NULL. In that case, |error| contains an error description. |
| 115 // For performance reasons, currently IsValidSchema() won't check the |
| 116 // correctness of regular expressions used in "pattern" and |
| 117 // "patternProperties" and in Validate() invalid regular expression don't |
| 118 // accept any strings. |
| 113 static scoped_ptr<base::DictionaryValue> IsValidSchema( | 119 static scoped_ptr<base::DictionaryValue> IsValidSchema( |
| 114 const std::string& schema, | 120 const std::string& schema, |
| 115 std::string* error); | 121 std::string* error); |
| 116 | 122 |
| 117 // Same as above but with |options|, which is a bitwise-OR combination of the | 123 // Same as above but with |options|, which is a bitwise-OR combination of the |
| 118 // Options above. | 124 // Options above. |
| 119 static scoped_ptr<base::DictionaryValue> IsValidSchema( | 125 static scoped_ptr<base::DictionaryValue> IsValidSchema( |
| 120 const std::string& schema, | 126 const std::string& schema, |
| 121 int options, | 127 int options, |
| 122 std::string* error); | 128 std::string* error); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 bool default_allow_additional_properties_; | 245 bool default_allow_additional_properties_; |
| 240 | 246 |
| 241 // Errors accumulated since the last call to Validate(). | 247 // Errors accumulated since the last call to Validate(). |
| 242 std::vector<Error> errors_; | 248 std::vector<Error> errors_; |
| 243 | 249 |
| 244 | 250 |
| 245 DISALLOW_COPY_AND_ASSIGN(JSONSchemaValidator); | 251 DISALLOW_COPY_AND_ASSIGN(JSONSchemaValidator); |
| 246 }; | 252 }; |
| 247 | 253 |
| 248 #endif // COMPONENTS_JSON_SCHEMA_JSON_SCHEMA_VALIDATOR_H_ | 254 #endif // COMPONENTS_JSON_SCHEMA_JSON_SCHEMA_VALIDATOR_H_ |
| OLD | NEW |