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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 | 63 |
64 Error(const std::string& path, const std::string& message); | 64 Error(const std::string& path, const std::string& message); |
65 | 65 |
66 // The path to the location of the error in the JSON structure. | 66 // The path to the location of the error in the JSON structure. |
67 std::string path; | 67 std::string path; |
68 | 68 |
69 // An english message describing the error. | 69 // An english message describing the error. |
70 std::string message; | 70 std::string message; |
71 }; | 71 }; |
72 | 72 |
| 73 enum Options { |
| 74 // Ignore unknown attributes. If this option is not set then unknown |
| 75 // attributes will make the schema validation fail. |
| 76 OPTIONS_IGNORE_UNKNOWN_ATTRIBUTES = 1 << 0, |
| 77 }; |
| 78 |
73 // Error messages. | 79 // Error messages. |
74 static const char kUnknownTypeReference[]; | 80 static const char kUnknownTypeReference[]; |
75 static const char kInvalidChoice[]; | 81 static const char kInvalidChoice[]; |
76 static const char kInvalidEnum[]; | 82 static const char kInvalidEnum[]; |
77 static const char kObjectPropertyIsRequired[]; | 83 static const char kObjectPropertyIsRequired[]; |
78 static const char kUnexpectedProperty[]; | 84 static const char kUnexpectedProperty[]; |
79 static const char kArrayMinItems[]; | 85 static const char kArrayMinItems[]; |
80 static const char kArrayMaxItems[]; | 86 static const char kArrayMaxItems[]; |
81 static const char kArrayItemRequired[]; | 87 static const char kArrayItemRequired[]; |
82 static const char kStringMinLength[]; | 88 static const char kStringMinLength[]; |
(...skipping 18 matching lines...) Expand all Loading... |
101 | 107 |
102 // Verifies if |schema| is a valid JSON v3 schema. When this validation passes | 108 // Verifies if |schema| is a valid JSON v3 schema. When this validation passes |
103 // then |schema| is valid JSON that can be parsed into a DictionaryValue, | 109 // then |schema| is valid JSON that can be parsed into a DictionaryValue, |
104 // and that DictionaryValue can be used to build a JSONSchemaValidator. | 110 // and that DictionaryValue can be used to build a JSONSchemaValidator. |
105 // Returns the parsed DictionaryValue when |schema| validated, otherwise | 111 // Returns the parsed DictionaryValue when |schema| validated, otherwise |
106 // returns NULL. In that case, |error| contains an error description. | 112 // returns NULL. In that case, |error| contains an error description. |
107 static scoped_ptr<base::DictionaryValue> IsValidSchema( | 113 static scoped_ptr<base::DictionaryValue> IsValidSchema( |
108 const std::string& schema, | 114 const std::string& schema, |
109 std::string* error); | 115 std::string* error); |
110 | 116 |
| 117 // Same as above but with |options|, which is a bitwise-OR combination of the |
| 118 // Options above. |
| 119 static scoped_ptr<base::DictionaryValue> IsValidSchema( |
| 120 const std::string& schema, |
| 121 int options, |
| 122 std::string* error); |
| 123 |
111 // Creates a validator for the specified schema. | 124 // Creates a validator for the specified schema. |
112 // | 125 // |
113 // NOTE: This constructor assumes that |schema| is well formed and valid. | 126 // NOTE: This constructor assumes that |schema| is well formed and valid. |
114 // Errors will result in CHECK at runtime; this constructor should not be used | 127 // Errors will result in CHECK at runtime; this constructor should not be used |
115 // with untrusted schemas. | 128 // with untrusted schemas. |
116 explicit JSONSchemaValidator(base::DictionaryValue* schema); | 129 explicit JSONSchemaValidator(base::DictionaryValue* schema); |
117 | 130 |
118 // Creates a validator for the specified schema and user-defined types. Each | 131 // Creates a validator for the specified schema and user-defined types. Each |
119 // type must be a valid JSONSchema type description with an additional "id" | 132 // type must be a valid JSONSchema type description with an additional "id" |
120 // field. Schema objects in |schema| can refer to these types with the "$ref" | 133 // field. Schema objects in |schema| can refer to these types with the "$ref" |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
226 bool default_allow_additional_properties_; | 239 bool default_allow_additional_properties_; |
227 | 240 |
228 // Errors accumulated since the last call to Validate(). | 241 // Errors accumulated since the last call to Validate(). |
229 std::vector<Error> errors_; | 242 std::vector<Error> errors_; |
230 | 243 |
231 | 244 |
232 DISALLOW_COPY_AND_ASSIGN(JSONSchemaValidator); | 245 DISALLOW_COPY_AND_ASSIGN(JSONSchemaValidator); |
233 }; | 246 }; |
234 | 247 |
235 #endif // COMPONENTS_JSON_SCHEMA_JSON_SCHEMA_VALIDATOR_H_ | 248 #endif // COMPONENTS_JSON_SCHEMA_JSON_SCHEMA_VALIDATOR_H_ |
OLD | NEW |