Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: chrome/common/json_schema_validator.h

Issue 7259019: Move base/values.h into the base namespace. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/common/json_pref_store.h ('k') | chrome/common/json_schema_validator_unittest_base.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_ 5 #ifndef CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_
6 #define CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_ 6 #define CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_
7 7
8 #include <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 13
14 namespace base {
14 class DictionaryValue; 15 class DictionaryValue;
15 class FundamentalValue; 16 class FundamentalValue;
16 class ListValue; 17 class ListValue;
17 class StringValue; 18 class StringValue;
18 class Value; 19 class Value;
20 }
19 21
20 //============================================================================== 22 //==============================================================================
21 // This class implements a subset of JSON Schema. 23 // This class implements a subset of JSON Schema.
22 // See: http://www.json.com/json-schema-proposal/ for more details. 24 // See: http://www.json.com/json-schema-proposal/ for more details.
23 // 25 //
24 // 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
25 // chrome/renderer/resources/json_schema.js. 27 // chrome/renderer/resources/json_schema.js.
26 // 28 //
27 // The following features of JSON Schema are not implemented: 29 // The following features of JSON Schema are not implemented:
28 // - requires 30 // - requires
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 static const char kArrayMaxItems[]; 80 static const char kArrayMaxItems[];
79 static const char kArrayItemRequired[]; 81 static const char kArrayItemRequired[];
80 static const char kStringMinLength[]; 82 static const char kStringMinLength[];
81 static const char kStringMaxLength[]; 83 static const char kStringMaxLength[];
82 static const char kStringPattern[]; 84 static const char kStringPattern[];
83 static const char kNumberMinimum[]; 85 static const char kNumberMinimum[];
84 static const char kNumberMaximum[]; 86 static const char kNumberMaximum[];
85 static const char kInvalidType[]; 87 static const char kInvalidType[];
86 88
87 // Classifies a Value as one of the JSON schema primitive types. 89 // Classifies a Value as one of the JSON schema primitive types.
88 static std::string GetJSONSchemaType(Value* value); 90 static std::string GetJSONSchemaType(base::Value* value);
89 91
90 // Utility methods to format error messages. The first method can have one 92 // Utility methods to format error messages. The first method can have one
91 // wildcard represented by '*', which is replaced with s1. The second method 93 // wildcard represented by '*', which is replaced with s1. The second method
92 // can have two, which are replaced by s1 and s2. 94 // can have two, which are replaced by s1 and s2.
93 static std::string FormatErrorMessage(const std::string& format, 95 static std::string FormatErrorMessage(const std::string& format,
94 const std::string& s1); 96 const std::string& s1);
95 static std::string FormatErrorMessage(const std::string& format, 97 static std::string FormatErrorMessage(const std::string& format,
96 const std::string& s1, 98 const std::string& s1,
97 const std::string& s2); 99 const std::string& s2);
98 100
99 // Creates a validator for the specified schema. 101 // Creates a validator for the specified schema.
100 // 102 //
101 // NOTE: This constructor assumes that |schema| is well formed and valid. 103 // NOTE: This constructor assumes that |schema| is well formed and valid.
102 // Errors will result in CHECK at runtime; this constructor should not be used 104 // Errors will result in CHECK at runtime; this constructor should not be used
103 // with untrusted schemas. 105 // with untrusted schemas.
104 explicit JSONSchemaValidator(DictionaryValue* schema); 106 explicit JSONSchemaValidator(base::DictionaryValue* schema);
105 107
106 // Creates a validator for the specified schema and user-defined types. Each 108 // Creates a validator for the specified schema and user-defined types. Each
107 // type must be a valid JSONSchema type description with an additional "id" 109 // type must be a valid JSONSchema type description with an additional "id"
108 // field. Schema objects in |schema| can refer to these types with the "$ref" 110 // field. Schema objects in |schema| can refer to these types with the "$ref"
109 // property. 111 // property.
110 // 112 //
111 // NOTE: This constructor assumes that |schema| and |types| are well-formed 113 // NOTE: This constructor assumes that |schema| and |types| are well-formed
112 // and valid. Errors will result in CHECK at runtime; this constructor should 114 // and valid. Errors will result in CHECK at runtime; this constructor should
113 // not be used with untrusted schemas. 115 // not be used with untrusted schemas.
114 JSONSchemaValidator(DictionaryValue* schema, ListValue* types); 116 JSONSchemaValidator(base::DictionaryValue* schema, base::ListValue* types);
115 117
116 ~JSONSchemaValidator(); 118 ~JSONSchemaValidator();
117 119
118 // Whether the validator allows additional items for objects and lists, beyond 120 // Whether the validator allows additional items for objects and lists, beyond
119 // those defined by their schema, by default. 121 // those defined by their schema, by default.
120 // 122 //
121 // This setting defaults to false: all items in an instance list or object 123 // This setting defaults to false: all items in an instance list or object
122 // must be defined by the corresponding schema. 124 // must be defined by the corresponding schema.
123 // 125 //
124 // This setting can be overridden on individual object and list schemas by 126 // This setting can be overridden on individual object and list schemas by
125 // setting the "additionalProperties" field. 127 // setting the "additionalProperties" field.
126 bool default_allow_additional_properties() const { 128 bool default_allow_additional_properties() const {
127 return default_allow_additional_properties_; 129 return default_allow_additional_properties_;
128 } 130 }
129 131
130 void set_default_allow_additional_properties(bool val) { 132 void set_default_allow_additional_properties(bool val) {
131 default_allow_additional_properties_ = val; 133 default_allow_additional_properties_ = val;
132 } 134 }
133 135
134 // Returns any errors from the last call to to Validate(). 136 // Returns any errors from the last call to to Validate().
135 const std::vector<Error>& errors() const { 137 const std::vector<Error>& errors() const {
136 return errors_; 138 return errors_;
137 } 139 }
138 140
139 // Validates a JSON value. Returns true if the instance is valid, false 141 // Validates a JSON value. Returns true if the instance is valid, false
140 // otherwise. If false is returned any errors are available from the errors() 142 // otherwise. If false is returned any errors are available from the errors()
141 // getter. 143 // getter.
142 bool Validate(Value* instance); 144 bool Validate(base::Value* instance);
143 145
144 private: 146 private:
145 typedef std::map<std::string, DictionaryValue*> TypeMap; 147 typedef std::map<std::string, base::DictionaryValue*> TypeMap;
146 148
147 // Each of the below methods handle a subset of the validation process. The 149 // Each of the below methods handle a subset of the validation process. The
148 // path paramater is the path to |instance| from the root of the instance tree 150 // path paramater is the path to |instance| from the root of the instance tree
149 // and is used in error messages. 151 // and is used in error messages.
150 152
151 // Validates any instance node against any schema node. This is called for 153 // Validates any instance node against any schema node. This is called for
152 // every node in the instance tree, and it just decides which of the more 154 // every node in the instance tree, and it just decides which of the more
153 // detailed methods to call. 155 // detailed methods to call.
154 void Validate(Value* instance, DictionaryValue* schema, 156 void Validate(base::Value* instance, base::DictionaryValue* schema,
155 const std::string& path); 157 const std::string& path);
156 158
157 // Validates a node against a list of possible schemas. If any one of the 159 // Validates a node against a list of possible schemas. If any one of the
158 // schemas match, the node is valid. 160 // schemas match, the node is valid.
159 void ValidateChoices(Value* instance, ListValue* choices, 161 void ValidateChoices(base::Value* instance, base::ListValue* choices,
160 const std::string& path); 162 const std::string& path);
161 163
162 // Validates a node against a list of exact primitive values, eg 42, "foobar". 164 // Validates a node against a list of exact primitive values, eg 42, "foobar".
163 void ValidateEnum(Value* instance, ListValue* choices, 165 void ValidateEnum(base::Value* instance, base::ListValue* choices,
164 const std::string& path); 166 const std::string& path);
165 167
166 // Validates a JSON object against an object schema node. 168 // Validates a JSON object against an object schema node.
167 void ValidateObject(DictionaryValue* instance, DictionaryValue* schema, 169 void ValidateObject(base::DictionaryValue* instance,
170 base::DictionaryValue* schema,
168 const std::string& path); 171 const std::string& path);
169 172
170 // Validates a JSON array against an array schema node. 173 // Validates a JSON array against an array schema node.
171 void ValidateArray(ListValue* instance, DictionaryValue* schema, 174 void ValidateArray(base::ListValue* instance, base::DictionaryValue* schema,
172 const std::string& path); 175 const std::string& path);
173 176
174 // Validates a JSON array against an array schema node configured to be a 177 // Validates a JSON array against an array schema node configured to be a
175 // tuple. In a tuple, there is one schema node for each item expected in the 178 // tuple. In a tuple, there is one schema node for each item expected in the
176 // array. 179 // array.
177 void ValidateTuple(ListValue* instance, DictionaryValue* schema, 180 void ValidateTuple(base::ListValue* instance, base::DictionaryValue* schema,
178 const std::string& path); 181 const std::string& path);
179 182
180 // Validate a JSON string against a string schema node. 183 // Validate a JSON string against a string schema node.
181 void ValidateString(StringValue* instance, DictionaryValue* schema, 184 void ValidateString(base::StringValue* instance,
185 base::DictionaryValue* schema,
182 const std::string& path); 186 const std::string& path);
183 187
184 // Validate a JSON number against a number schema node. 188 // Validate a JSON number against a number schema node.
185 void ValidateNumber(Value* instance, DictionaryValue* schema, 189 void ValidateNumber(base::Value* instance,
190 base::DictionaryValue* schema,
186 const std::string& path); 191 const std::string& path);
187 192
188 // Validates that the JSON node |instance| has |expected_type|. 193 // Validates that the JSON node |instance| has |expected_type|.
189 bool ValidateType(Value* instance, const std::string& expected_type, 194 bool ValidateType(base::Value* instance, const std::string& expected_type,
190 const std::string& path); 195 const std::string& path);
191 196
192 // Returns true if |schema| will allow additional items of any type. 197 // Returns true if |schema| will allow additional items of any type.
193 bool SchemaAllowsAnyAdditionalItems( 198 bool SchemaAllowsAnyAdditionalItems(
194 DictionaryValue* schema, DictionaryValue** addition_items_schema); 199 base::DictionaryValue* schema,
200 base::DictionaryValue** addition_items_schema);
195 201
196 // The root schema node. 202 // The root schema node.
197 DictionaryValue* schema_root_; 203 base::DictionaryValue* schema_root_;
198 204
199 // Map of user-defined name to type. 205 // Map of user-defined name to type.
200 TypeMap types_; 206 TypeMap types_;
201 207
202 // Whether we allow additional properties on objects by default. This can be 208 // Whether we allow additional properties on objects by default. This can be
203 // overridden by the allow_additional_properties flag on an Object schema. 209 // overridden by the allow_additional_properties flag on an Object schema.
204 bool default_allow_additional_properties_; 210 bool default_allow_additional_properties_;
205 211
206 // Errors accumulated since the last call to Validate(). 212 // Errors accumulated since the last call to Validate().
207 std::vector<Error> errors_; 213 std::vector<Error> errors_;
208 214
209 215
210 DISALLOW_COPY_AND_ASSIGN(JSONSchemaValidator); 216 DISALLOW_COPY_AND_ASSIGN(JSONSchemaValidator);
211 }; 217 };
212 218
213 #endif // CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_ 219 #endif // CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_
OLDNEW
« no previous file with comments | « chrome/common/json_pref_store.h ('k') | chrome/common/json_schema_validator_unittest_base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698