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

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

Issue 4673001: Implements a C++ version of JSONSchemaValidator. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: all done Created 10 years, 1 month 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
OLDNEW
(Empty)
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
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 class DictionaryValue;
15 class FundamentalValue;
16 class ListValue;
17 class StringValue;
18 class Value;
19
20 //==============================================================================
21 // This class implements a subset of JSON Schema.
22 // See: http://www.json.com/json-schema-proposal/ for more details.
23 //
24 // There is also an older JavaScript implementation of the same functionality in
25 // chrome/renderer/resources/json_schema.js.
26 //
27 // The following features of JSON Schema are not implemented:
28 // - requires
29 // - unique
30 // - disallow
31 // - union types (but replaced with 'choices')
32 // - number.maxDecimal
33 // - string.pattern
34 //
35 // The following properties are not applicable to the interface exposed by
36 // this class:
37 // - options
38 // - readonly
39 // - title
40 // - description
41 // - format
42 // - default
43 // - transient
44 // - hidden
45 //
46 // There are also these departures from the JSON Schema proposal:
47 // - null counts as 'unspecified' for optional values
48 // - added the 'choices' property, to allow specifying a list of possible types
49 // for a value
50 // - by default an "object" typed schema does not allow additional properties.
51 // if present, "additionalProperties" is to be a schema against which all
52 // additional properties will be validated.
53 //==============================================================================
54 class JSONSchemaValidator {
55 public:
56 // Details about a validation error.
57 struct Error {
58 Error();
59
60 explicit Error(const std::string& message);
61
62 Error(const std::string& path, const std::string& message);
63
64 // The path to the location of the error in the JSON structure.
65 std::string path;
66
67 // An english message describing the error.
68 std::string message;
69 };
70
71 // Error messages.
72 static const char kUnknownTypeReference[];
73 static const char kInvalidChoice[];
74 static const char kInvalidEnum[];
75 static const char kObjectPropertyIsRequired[];
76 static const char kUnexpectedProperty[];
77 static const char kArrayMinItems[];
78 static const char kArrayMaxItems[];
79 static const char kArrayItemRequired[];
80 static const char kStringMinLength[];
81 static const char kStringMaxLength[];
82 static const char kStringPattern[];
83 static const char kInfinityNaNNotSupported[];
84 static const char kNumberMinimum[];
85 static const char kNumberMaximum[];
86 static const char kInvalidType[];
87
88 // Utility methods to format error messages.
89 static std::string FormatErrorMessage(const std::string& format,
90 const std::string& s1);
91 static std::string FormatErrorMessage(const std::string& format,
92 const std::string& s1,
93 const std::string& s2);
94
95 // Creates a validator for the specified schema.
96 explicit JSONSchemaValidator(DictionaryValue* schema);
97
98 // Creates a validator for the specified schema and user-defined types. Each
99 // type must have an "id" property that can be referred to within the schema
100 // with "ref".
101 JSONSchemaValidator(DictionaryValue* schema, ListValue* types);
102
103 // Whether the validator allows additional items for objects and lists, beyond
104 // those defined by their schema, by default.
105 //
106 // This setting defaults to false: all items in an instance list or object
107 // must be defined by the corresponding schema.
108 //
109 // This setting can be overridden on individual object and list schemas by
110 // setting the "additionalProperties" field.
111 bool default_allow_additional_properties() const {
112 return default_allow_additional_properties_;
113 }
114
115 void set_default_allow_additional_properties(bool val) {
116 default_allow_additional_properties_ = val;
117 }
118
119 // Returns any errors from the last call to to Validate().
120 const std::vector<Error>& errors() const {
121 return errors_;
122 }
123
124 // Validates a JSON value. Returns true if the instance is valid, false
125 // otherwise. If false is returned any errors are available from the errors()
126 // getter.
127 bool Validate(Value* instance);
128
129 private:
130 typedef std::map<std::string, DictionaryValue*> TypeMap;
131
132 void Validate(Value* instance, DictionaryValue* schema,
133 const std::string& path);
134
135 void ValidateChoices(Value* instance, ListValue* choices,
136 const std::string& path);
137
138 void ValidateEnum(Value* instance, ListValue* choices,
139 const std::string& path);
140
141 void ValidateObject(DictionaryValue* instance, DictionaryValue* schema,
142 const std::string& path);
143
144 void ValidateArray(ListValue* instance, DictionaryValue* schema,
145 const std::string& path);
146
147 void ValidateString(StringValue* instance, DictionaryValue* schema,
148 const std::string& path);
149
150 void ValidateNumber(Value* instance, DictionaryValue* schema,
151 const std::string& path);
152
153 bool ValidateType(Value* instance, const std::string& expected_type,
154 const std::string& path);
155
156 // Returns true if |schema| will allow additional items of any type.
157 bool SchemaAllowsAnyAdditionalItems(
158 DictionaryValue* schema, DictionaryValue** addition_items_schema);
159
160 // The root schema node.
161 DictionaryValue* schema_root_;
162
163 // Map of user-defined name to type.
164 TypeMap types_;
165
166 // Whether we allow additional properties on objects by default. This can be
167 // overridden by the allow_additional_properties flag on an Object schema.
168 bool default_allow_additional_properties_;
169
170 // Errors accumulated since the last call to Validate().
171 std::vector<Error> errors_;
172
173
174 DISALLOW_COPY_AND_ASSIGN(JSONSchemaValidator);
175 };
176
177 #endif // CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698