Chromium Code Reviews| 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 class JSONSchemaValidator { | |
|
asargent_no_longer_on_chrome
2010/11/08 23:47:18
It would probably be worth having a top-level comm
Aaron Boodman
2010/11/10 19:03:35
Done.
| |
| 21 public: | |
| 22 // Details about a validation error. | |
| 23 struct Error { | |
| 24 Error(); | |
| 25 | |
| 26 explicit Error(const std::string& message); | |
| 27 | |
| 28 Error(const std::string& path, const std::string& message); | |
| 29 | |
| 30 // The path to the location of the error in the JSON structure. | |
| 31 std::string path; | |
| 32 | |
| 33 // An english message describing the error. | |
| 34 std::string message; | |
| 35 }; | |
| 36 | |
| 37 // Error messages. | |
| 38 static const char kUnknownTypeReference[]; | |
| 39 static const char kInvalidChoice[]; | |
| 40 static const char kInvalidEnum[]; | |
| 41 static const char kObjectPropertyIsRequired[]; | |
| 42 static const char kUnexpectedProperty[]; | |
| 43 static const char kArrayMinItems[]; | |
| 44 static const char kArrayMaxItems[]; | |
| 45 static const char kArrayItemRequired[]; | |
| 46 static const char kStringMinLength[]; | |
| 47 static const char kStringMaxLength[]; | |
| 48 static const char kStringPattern[]; | |
| 49 static const char kInfinityNaNNotSupported[]; | |
| 50 static const char kNumberMinimum[]; | |
| 51 static const char kNumberMaximum[]; | |
| 52 static const char kNumberDecimalPlaces[]; | |
| 53 static const char kInvalidType[]; | |
| 54 | |
| 55 static std::string FormatErrorMessage(const std::string& format, | |
|
asargent_no_longer_on_chrome
2010/11/08 23:47:18
nit: Document what s1 (and below, s1,s2) are used
Aaron Boodman
2010/11/10 19:03:35
Done.
| |
| 56 const std::string& s1); | |
| 57 static std::string FormatErrorMessage(const std::string& format, | |
| 58 const std::string& s1, | |
| 59 const std::string& s2); | |
| 60 | |
| 61 // Creates a validator for the specified schema. | |
| 62 JSONSchemaValidator(DictionaryValue* schema); | |
| 63 | |
| 64 // Creates a validator for the specified schema and user-defined types. Each | |
| 65 // type must have an "id" property that can be referred to within the schema | |
|
asargent_no_longer_on_chrome
2010/11/08 23:47:18
nit: In this comment, consider replacing
"Each typ
Aaron Boodman
2010/11/10 19:03:35
Done.
| |
| 66 // with "ref". | |
| 67 JSONSchemaValidator(DictionaryValue* schema, ListValue* types); | |
| 68 | |
| 69 // Whether the validator allows additional items for objects and lists, beyond | |
| 70 // those defined by their schema, by default. | |
| 71 // | |
| 72 // This setting defaults to false: all items in an instance list or object | |
| 73 // must be defined by the corresponding schema. | |
| 74 // | |
| 75 // This setting can be overridden on individual object and list schemas by | |
| 76 // setting the "additionalProperties" field. | |
| 77 bool default_allow_additional_properties() const { | |
| 78 return default_allow_additional_properties_; | |
| 79 } | |
| 80 | |
| 81 void set_default_allow_additional_properties(bool val) { | |
| 82 default_allow_additional_properties_ = val; | |
| 83 } | |
| 84 | |
| 85 // Returns any errors from the last call to to Validate(). | |
| 86 const std::vector<Error>& errors() const { | |
| 87 return errors_; | |
| 88 } | |
| 89 | |
| 90 // Validates a JSON value. Returns true if the instance is valid, false | |
| 91 // otherwise. If false is returned any errors are available from the errors() | |
| 92 // getter. | |
| 93 bool Validate(Value* instance); | |
| 94 | |
| 95 private: | |
| 96 typedef std::map<std::string, DictionaryValue*> TypeMap; | |
| 97 | |
| 98 void Validate(Value* instance, DictionaryValue* schema, | |
| 99 const std::string& path); | |
|
asargent_no_longer_on_chrome
2010/11/08 23:47:18
The way that paths work is a little bit subtle, an
Aaron Boodman
2010/11/10 19:03:35
Done.
| |
| 100 | |
| 101 void ValidateChoices(Value* instance, ListValue* choices, | |
| 102 const std::string& path); | |
| 103 | |
| 104 void ValidateEnum(Value* instance, ListValue* choices, | |
|
asargent_no_longer_on_chrome
2010/11/08 23:47:18
consider adding a comment here about the differenc
Aaron Boodman
2010/11/10 19:03:35
Done.
| |
| 105 const std::string& path); | |
| 106 | |
| 107 void ValidateObject(DictionaryValue* instance, DictionaryValue* schema, | |
| 108 const std::string& path); | |
| 109 | |
| 110 void ValidateArray(ListValue* instance, DictionaryValue* schema, | |
| 111 const std::string& path); | |
| 112 | |
| 113 void ValidateString(StringValue* instance, DictionaryValue* schema, | |
| 114 const std::string& path); | |
| 115 | |
| 116 void ValidateNumber(Value* instance, DictionaryValue* schema, | |
| 117 const std::string& path); | |
| 118 | |
| 119 void AddError(int error_id, const std::vector<std::string>& replacements); | |
| 120 | |
| 121 bool ValidateType(Value* instance, const std::string& expected_type, | |
| 122 const std::string& path); | |
| 123 | |
| 124 bool SchemaAllowsAnyAdditionalItems( | |
| 125 DictionaryValue* schema, DictionaryValue** addition_items_schema); | |
| 126 | |
| 127 // The root schema node. | |
| 128 DictionaryValue* schema_root_; | |
| 129 | |
| 130 // Map of user-defined name to type. | |
| 131 TypeMap types_; | |
| 132 | |
| 133 // Whether we allow additional properties on objects by default. This can be | |
| 134 // overridden by the allow_additional_properties flag on an Object schema. | |
| 135 bool default_allow_additional_properties_; | |
| 136 | |
| 137 // Errors accumulated since the last call to Validate(). | |
| 138 std::vector<Error> errors_; | |
| 139 | |
| 140 | |
| 141 DISALLOW_COPY_AND_ASSIGN(JSONSchemaValidator); | |
| 142 }; | |
| 143 | |
| 144 #endif // CHROME_COMMON_JSON_SCHEMA_VALIDATOR_H_ | |
| OLD | NEW |