| 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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 const std::string& s1); | 104 const std::string& s1); |
| 104 static std::string FormatErrorMessage(const std::string& format, | 105 static std::string FormatErrorMessage(const std::string& format, |
| 105 const std::string& s1, | 106 const std::string& s1, |
| 106 const std::string& s2); | 107 const std::string& s2); |
| 107 | 108 |
| 108 // Verifies if |schema| is a valid JSON v3 schema. When this validation passes | 109 // 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, | 110 // then |schema| is valid JSON that can be parsed into a DictionaryValue, |
| 110 // and that DictionaryValue can be used to build a JSONSchemaValidator. | 111 // and that DictionaryValue can be used to build a JSONSchemaValidator. |
| 111 // Returns the parsed DictionaryValue when |schema| validated, otherwise | 112 // Returns the parsed DictionaryValue when |schema| validated, otherwise |
| 112 // returns NULL. In that case, |error| contains an error description. | 113 // returns NULL. In that case, |error| contains an error description. |
| 114 // For performance reason, currently IsValidSchema() won't check the |
| 115 // correctness of regular expression used in "pattern" and "patternProperties" |
| 116 // and in Validate() invalid regular expression don't accepts any strings. |
| 113 static scoped_ptr<base::DictionaryValue> IsValidSchema( | 117 static scoped_ptr<base::DictionaryValue> IsValidSchema( |
| 114 const std::string& schema, | 118 const std::string& schema, |
| 115 std::string* error); | 119 std::string* error); |
| 116 | 120 |
| 117 // Same as above but with |options|, which is a bitwise-OR combination of the | 121 // Same as above but with |options|, which is a bitwise-OR combination of the |
| 118 // Options above. | 122 // Options above. |
| 119 static scoped_ptr<base::DictionaryValue> IsValidSchema( | 123 static scoped_ptr<base::DictionaryValue> IsValidSchema( |
| 120 const std::string& schema, | 124 const std::string& schema, |
| 121 int options, | 125 int options, |
| 122 std::string* error); | 126 std::string* error); |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 bool default_allow_additional_properties_; | 243 bool default_allow_additional_properties_; |
| 240 | 244 |
| 241 // Errors accumulated since the last call to Validate(). | 245 // Errors accumulated since the last call to Validate(). |
| 242 std::vector<Error> errors_; | 246 std::vector<Error> errors_; |
| 243 | 247 |
| 244 | 248 |
| 245 DISALLOW_COPY_AND_ASSIGN(JSONSchemaValidator); | 249 DISALLOW_COPY_AND_ASSIGN(JSONSchemaValidator); |
| 246 }; | 250 }; |
| 247 | 251 |
| 248 #endif // COMPONENTS_JSON_SCHEMA_JSON_SCHEMA_VALIDATOR_H_ | 252 #endif // COMPONENTS_JSON_SCHEMA_JSON_SCHEMA_VALIDATOR_H_ |
| OLD | NEW |