OLD | NEW |
(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 // Classifies a Value as one of the JSON schema primitive types. |
| 89 static std::string GetJSONSchemaType(Value* value); |
| 90 |
| 91 // Utility methods to format error messages. The first method can have one |
| 92 // wildcard represented by '*', which is replaced with s1. The second method |
| 93 // can have two, which are replaced by s1 and s2. |
| 94 static std::string FormatErrorMessage(const std::string& format, |
| 95 const std::string& s1); |
| 96 static std::string FormatErrorMessage(const std::string& format, |
| 97 const std::string& s1, |
| 98 const std::string& s2); |
| 99 |
| 100 // Creates a validator for the specified schema. |
| 101 // |
| 102 // NOTE: This constructor assumes that |schema| is well formed and valid. |
| 103 // Errors will result in CHECK at runtime; this constructor should not be used |
| 104 // with untrusted schemas. |
| 105 explicit JSONSchemaValidator(DictionaryValue* schema); |
| 106 |
| 107 // Creates a validator for the specified schema and user-defined types. Each |
| 108 // type must be a valid JSONSchema type description with an additional "id" |
| 109 // field. Schema objects in |schema| can refer to these types with the "$ref" |
| 110 // property. |
| 111 // |
| 112 // NOTE: This constructor assumes that |schema| and |types| are well-formed |
| 113 // and valid. Errors will result in CHECK at runtime; this constructor should |
| 114 // not be used with untrusted schemas. |
| 115 JSONSchemaValidator(DictionaryValue* schema, ListValue* types); |
| 116 |
| 117 // Whether the validator allows additional items for objects and lists, beyond |
| 118 // those defined by their schema, by default. |
| 119 // |
| 120 // This setting defaults to false: all items in an instance list or object |
| 121 // must be defined by the corresponding schema. |
| 122 // |
| 123 // This setting can be overridden on individual object and list schemas by |
| 124 // setting the "additionalProperties" field. |
| 125 bool default_allow_additional_properties() const { |
| 126 return default_allow_additional_properties_; |
| 127 } |
| 128 |
| 129 void set_default_allow_additional_properties(bool val) { |
| 130 default_allow_additional_properties_ = val; |
| 131 } |
| 132 |
| 133 // Returns any errors from the last call to to Validate(). |
| 134 const std::vector<Error>& errors() const { |
| 135 return errors_; |
| 136 } |
| 137 |
| 138 // Validates a JSON value. Returns true if the instance is valid, false |
| 139 // otherwise. If false is returned any errors are available from the errors() |
| 140 // getter. |
| 141 bool Validate(Value* instance); |
| 142 |
| 143 private: |
| 144 typedef std::map<std::string, DictionaryValue*> TypeMap; |
| 145 |
| 146 // Each of the below methods handle a subset of the validation process. The |
| 147 // path paramater is the path to |instance| from the root of the instance tree |
| 148 // and is used in error messages. |
| 149 |
| 150 // Validates any instance node against any schema node. This is called for |
| 151 // every node in the instance tree, and it just decides which of the more |
| 152 // detailed methods to call. |
| 153 void Validate(Value* instance, DictionaryValue* schema, |
| 154 const std::string& path); |
| 155 |
| 156 // Validates a node against a list of possible schemas. If any one of the |
| 157 // schemas match, the node is valid. |
| 158 void ValidateChoices(Value* instance, ListValue* choices, |
| 159 const std::string& path); |
| 160 |
| 161 // Validates a node against a list of exact primitive values, eg 42, "foobar". |
| 162 void ValidateEnum(Value* instance, ListValue* choices, |
| 163 const std::string& path); |
| 164 |
| 165 // Validates a JSON object against an object schema node. |
| 166 void ValidateObject(DictionaryValue* instance, DictionaryValue* schema, |
| 167 const std::string& path); |
| 168 |
| 169 // Validates a JSON array against an array schema node. |
| 170 void ValidateArray(ListValue* instance, DictionaryValue* schema, |
| 171 const std::string& path); |
| 172 |
| 173 // Validates a JSON array against an array schema node configured to be a |
| 174 // tuple. In a tuple, there is one schema node for each item expected in the |
| 175 // array. |
| 176 void ValidateTuple(ListValue* instance, DictionaryValue* schema, |
| 177 const std::string& path); |
| 178 |
| 179 // Validate a JSON string against a string schema node. |
| 180 void ValidateString(StringValue* instance, DictionaryValue* schema, |
| 181 const std::string& path); |
| 182 |
| 183 // Validate a JSON number against a number schema node. |
| 184 void ValidateNumber(Value* instance, DictionaryValue* schema, |
| 185 const std::string& path); |
| 186 |
| 187 // Validates that the JSON node |instance| has |expected_type|. |
| 188 bool ValidateType(Value* instance, const std::string& expected_type, |
| 189 const std::string& path); |
| 190 |
| 191 // Returns true if |schema| will allow additional items of any type. |
| 192 bool SchemaAllowsAnyAdditionalItems( |
| 193 DictionaryValue* schema, DictionaryValue** addition_items_schema); |
| 194 |
| 195 // The root schema node. |
| 196 DictionaryValue* schema_root_; |
| 197 |
| 198 // Map of user-defined name to type. |
| 199 TypeMap types_; |
| 200 |
| 201 // Whether we allow additional properties on objects by default. This can be |
| 202 // overridden by the allow_additional_properties flag on an Object schema. |
| 203 bool default_allow_additional_properties_; |
| 204 |
| 205 // Errors accumulated since the last call to Validate(). |
| 206 std::vector<Error> errors_; |
| 207 |
| 208 |
| 209 DISALLOW_COPY_AND_ASSIGN(JSONSchemaValidator); |
| 210 }; |
| 211 |
| 212 #endif // CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_ |
OLD | NEW |