OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_ | |
6 #define CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 | |
14 namespace base { | |
15 class DictionaryValue; | |
16 class ListValue; | |
17 class StringValue; | |
18 class Value; | |
19 } | |
20 | |
21 //============================================================================== | |
22 // This class implements a subset of JSON Schema. | |
23 // See: http://www.json.com/json-schema-proposal/ for more details. | |
24 // | |
25 // There is also an older JavaScript implementation of the same functionality in | |
26 // chrome/renderer/resources/json_schema.js. | |
27 // | |
28 // The following features of JSON Schema are not implemented: | |
29 // - requires | |
30 // - unique | |
31 // - disallow | |
32 // - union types (but replaced with 'choices') | |
33 // - number.maxDecimal | |
34 // - string.pattern | |
35 // | |
36 // The following properties are not applicable to the interface exposed by | |
37 // this class: | |
38 // - options | |
39 // - readonly | |
40 // - title | |
41 // - description | |
42 // - format | |
43 // - default | |
44 // - transient | |
45 // - hidden | |
46 // | |
47 // There are also these departures from the JSON Schema proposal: | |
48 // - null counts as 'unspecified' for optional values | |
49 // - added the 'choices' property, to allow specifying a list of possible types | |
50 // for a value | |
51 // - by default an "object" typed schema does not allow additional properties. | |
52 // if present, "additionalProperties" is to be a schema against which all | |
53 // additional properties will be validated. | |
54 //============================================================================== | |
55 class JSONSchemaValidator { | |
56 public: | |
57 // Details about a validation error. | |
58 struct Error { | |
59 Error(); | |
60 | |
61 explicit Error(const std::string& message); | |
62 | |
63 Error(const std::string& path, const std::string& message); | |
64 | |
65 // The path to the location of the error in the JSON structure. | |
66 std::string path; | |
67 | |
68 // An english message describing the error. | |
69 std::string message; | |
70 }; | |
71 | |
72 // Error messages. | |
73 static const char kUnknownTypeReference[]; | |
74 static const char kInvalidChoice[]; | |
75 static const char kInvalidEnum[]; | |
76 static const char kObjectPropertyIsRequired[]; | |
77 static const char kUnexpectedProperty[]; | |
78 static const char kArrayMinItems[]; | |
79 static const char kArrayMaxItems[]; | |
80 static const char kArrayItemRequired[]; | |
81 static const char kStringMinLength[]; | |
82 static const char kStringMaxLength[]; | |
83 static const char kStringPattern[]; | |
84 static const char kNumberMinimum[]; | |
85 static const char kNumberMaximum[]; | |
86 static const char kInvalidType[]; | |
87 static const char kInvalidTypeIntegerNumber[]; | |
88 | |
89 // Classifies a Value as one of the JSON schema primitive types. | |
90 static std::string GetJSONSchemaType(const base::Value* value); | |
91 | |
92 // Utility methods to format error messages. The first method can have one | |
93 // wildcard represented by '*', which is replaced with s1. The second method | |
94 // can have two, which are replaced by s1 and s2. | |
95 static std::string FormatErrorMessage(const std::string& format, | |
96 const std::string& s1); | |
97 static std::string FormatErrorMessage(const std::string& format, | |
98 const std::string& s1, | |
99 const std::string& s2); | |
100 | |
101 // Creates a validator for the specified schema. | |
102 // | |
103 // NOTE: This constructor assumes that |schema| is well formed and valid. | |
104 // Errors will result in CHECK at runtime; this constructor should not be used | |
105 // with untrusted schemas. | |
106 explicit JSONSchemaValidator(base::DictionaryValue* schema); | |
107 | |
108 // Creates a validator for the specified schema and user-defined types. Each | |
109 // type must be a valid JSONSchema type description with an additional "id" | |
110 // field. Schema objects in |schema| can refer to these types with the "$ref" | |
111 // property. | |
112 // | |
113 // NOTE: This constructor assumes that |schema| and |types| are well-formed | |
114 // and valid. Errors will result in CHECK at runtime; this constructor should | |
115 // not be used with untrusted schemas. | |
116 JSONSchemaValidator(base::DictionaryValue* schema, base::ListValue* types); | |
117 | |
118 ~JSONSchemaValidator(); | |
119 | |
120 // Whether the validator allows additional items for objects and lists, beyond | |
121 // those defined by their schema, by default. | |
122 // | |
123 // This setting defaults to false: all items in an instance list or object | |
124 // must be defined by the corresponding schema. | |
125 // | |
126 // This setting can be overridden on individual object and list schemas by | |
127 // setting the "additionalProperties" field. | |
128 bool default_allow_additional_properties() const { | |
129 return default_allow_additional_properties_; | |
130 } | |
131 | |
132 void set_default_allow_additional_properties(bool val) { | |
133 default_allow_additional_properties_ = val; | |
134 } | |
135 | |
136 // Returns any errors from the last call to to Validate(). | |
137 const std::vector<Error>& errors() const { | |
138 return errors_; | |
139 } | |
140 | |
141 // Validates a JSON value. Returns true if the instance is valid, false | |
142 // otherwise. If false is returned any errors are available from the errors() | |
143 // getter. | |
144 bool Validate(const base::Value* instance); | |
145 | |
146 private: | |
147 typedef std::map<std::string, const base::DictionaryValue*> TypeMap; | |
148 | |
149 // Each of the below methods handle a subset of the validation process. The | |
150 // path paramater is the path to |instance| from the root of the instance tree | |
151 // and is used in error messages. | |
152 | |
153 // Validates any instance node against any schema node. This is called for | |
154 // every node in the instance tree, and it just decides which of the more | |
155 // detailed methods to call. | |
156 void Validate(const base::Value* instance, | |
157 const base::DictionaryValue* schema, | |
158 const std::string& path); | |
159 | |
160 // Validates a node against a list of possible schemas. If any one of the | |
161 // schemas match, the node is valid. | |
162 void ValidateChoices(const base::Value* instance, | |
163 const base::ListValue* choices, | |
164 const std::string& path); | |
165 | |
166 // Validates a node against a list of exact primitive values, eg 42, "foobar". | |
167 void ValidateEnum(const base::Value* instance, | |
168 const base::ListValue* choices, | |
169 const std::string& path); | |
170 | |
171 // Validates a JSON object against an object schema node. | |
172 void ValidateObject(const base::DictionaryValue* instance, | |
173 const base::DictionaryValue* schema, | |
174 const std::string& path); | |
175 | |
176 // Validates a JSON array against an array schema node. | |
177 void ValidateArray(const base::ListValue* instance, | |
178 const base::DictionaryValue* schema, | |
179 const std::string& path); | |
180 | |
181 // Validates a JSON array against an array schema node configured to be a | |
182 // tuple. In a tuple, there is one schema node for each item expected in the | |
183 // array. | |
184 void ValidateTuple(const base::ListValue* instance, | |
185 const base::DictionaryValue* schema, | |
186 const std::string& path); | |
187 | |
188 // Validate a JSON string against a string schema node. | |
189 void ValidateString(const base::Value* instance, | |
190 const base::DictionaryValue* schema, | |
191 const std::string& path); | |
192 | |
193 // Validate a JSON number against a number schema node. | |
194 void ValidateNumber(const base::Value* instance, | |
195 const base::DictionaryValue* schema, | |
196 const std::string& path); | |
197 | |
198 // Validates that the JSON node |instance| has |expected_type|. | |
199 bool ValidateType(const base::Value* instance, | |
200 const std::string& expected_type, | |
201 const std::string& path); | |
202 | |
203 // Returns true if |schema| will allow additional items of any type. | |
204 bool SchemaAllowsAnyAdditionalItems( | |
205 const base::DictionaryValue* schema, | |
206 const base::DictionaryValue** addition_items_schema); | |
207 | |
208 // The root schema node. | |
209 base::DictionaryValue* schema_root_; | |
210 | |
211 // Map of user-defined name to type. | |
212 TypeMap types_; | |
213 | |
214 // Whether we allow additional properties on objects by default. This can be | |
215 // overridden by the allow_additional_properties flag on an Object schema. | |
216 bool default_allow_additional_properties_; | |
217 | |
218 // Errors accumulated since the last call to Validate(). | |
219 std::vector<Error> errors_; | |
220 | |
221 | |
222 DISALLOW_COPY_AND_ASSIGN(JSONSchemaValidator); | |
223 }; | |
224 | |
225 #endif // CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_ | |
OLD | NEW |