|
| 1 | +#ifndef NLOHMANN_JSON_VALIDATOR_HPP__ |
| 2 | +#define NLOHMANN_JSON_VALIDATOR_HPP__ |
| 3 | + |
| 4 | +#include <json.hpp> |
| 5 | + |
| 6 | +#include <regex> |
| 7 | + |
| 8 | +// make yourself a home - welcome to nlohmann's namespace |
| 9 | +namespace nlohmann |
| 10 | +{ |
| 11 | + |
| 12 | +class json_validator |
| 13 | +{ |
| 14 | + // insert default values items into object |
| 15 | + // if the key is not present before checking their |
| 16 | + // validity in regards to their schema |
| 17 | + // |
| 18 | + // breaks JSON-Schema-Test-Suite if true |
| 19 | + // *PARTIALLY IMPLEMENTED* only for properties of objects |
| 20 | + bool default_value_insertion = false; |
| 21 | + |
| 22 | + // recursively insert default values and create parent objects if |
| 23 | + // they would be empty |
| 24 | + // |
| 25 | + // breaks JSON-Schema-Test-Suite if true |
| 26 | + // *NOT YET IMPLEMENTED* -> maybe the same as the above option, need more thoughts |
| 27 | + bool recursive_default_value_insertion = false; |
| 28 | + |
| 29 | + void not_yet_implemented(const json &schema, const std::string &field, const std::string &type) |
| 30 | + { |
| 31 | + if (schema.find(field) != schema.end()) |
| 32 | + throw std::logic_error(field + " for " + type + " is not yet implemented"); |
| 33 | + } |
| 34 | + |
| 35 | + void validate_type(const json &schema, const std::string &expected_type, const std::string &name) |
| 36 | + { |
| 37 | + const auto &type_it = schema.find("type"); |
| 38 | + if (type_it == schema.end()) |
| 39 | + /* TODO guess type for more safety, |
| 40 | + * TODO use definitions |
| 41 | + * TODO valid by not being defined? FIXME not clear - there are |
| 42 | + * schema-test case which are not specifying a type */ |
| 43 | + return; |
| 44 | + |
| 45 | + const auto &type_instance = type_it.value(); |
| 46 | + |
| 47 | + // any of the types in this array |
| 48 | + if (type_instance.type() == json::value_t::array) { |
| 49 | + if (std::find(type_instance.begin(), |
| 50 | + type_instance.end(), |
| 51 | + expected_type) != type_instance.end()) |
| 52 | + return; |
| 53 | + |
| 54 | + std::ostringstream s; |
| 55 | + s << expected_type << " is not any of " << type_instance << " for " << name; |
| 56 | + throw std::invalid_argument(s.str()); |
| 57 | + |
| 58 | + } else { // type_instance is a string |
| 59 | + if (type_instance == expected_type) |
| 60 | + return; |
| 61 | + |
| 62 | + throw std::invalid_argument(type_instance.get<std::string>() + " is not a " + expected_type + " for " + name); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + void validate_enum(json &instance, const json &schema, const std::string &name) |
| 67 | + { |
| 68 | + const auto &enum_value = schema.find("enum"); |
| 69 | + if (enum_value == schema.end()) |
| 70 | + return; |
| 71 | + |
| 72 | + if (std::find(enum_value.value().begin(), enum_value.value().end(), instance) != enum_value.value().end()) |
| 73 | + return; |
| 74 | + |
| 75 | + std::ostringstream s; |
| 76 | + s << "invalid enum-value '" << instance << "' " |
| 77 | + << "for instance '" << name << "'. Candidates are " << enum_value.value() << "."; |
| 78 | + |
| 79 | + throw std::invalid_argument(s.str()); |
| 80 | + } |
| 81 | + |
| 82 | + void validate_string(json &instance, const json &schema, const std::string &name) |
| 83 | + { |
| 84 | + // possibile but unhanled keywords |
| 85 | + not_yet_implemented(schema, "format", "string"); |
| 86 | + not_yet_implemented(schema, "pattern", "string"); |
| 87 | + |
| 88 | + validate_type(schema, "string", name); |
| 89 | + |
| 90 | + auto attr = schema.find("minLength"); |
| 91 | + if (attr != schema.end()) |
| 92 | + if (instance.get<std::string>().size() < attr.value()) { |
| 93 | + std::ostringstream s; |
| 94 | + s << "'" << name << "' of value '" << instance << "' is too short as per minLength (" |
| 95 | + << attr.value() << ")"; |
| 96 | + throw std::out_of_range(s.str()); |
| 97 | + } |
| 98 | + |
| 99 | + attr = schema.find("maxLength"); |
| 100 | + if (attr != schema.end()) |
| 101 | + if (instance.get<std::string>().size() > attr.value()) { |
| 102 | + std::ostringstream s; |
| 103 | + s << "'" << name << "' of value '" << instance << "' is too long as per maxLength (" |
| 104 | + << attr.value() << ")"; |
| 105 | + throw std::out_of_range(s.str()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + void validate_boolean(json & /*instance*/, const json &schema, const std::string &name) |
| 110 | + { |
| 111 | + validate_type(schema, "boolean", name); |
| 112 | + } |
| 113 | + |
| 114 | + void validate_numeric(json &instance, const json &schema, const std::string &name) |
| 115 | + { |
| 116 | + double value = instance; |
| 117 | + |
| 118 | + const auto &multipleOf = schema.find("multipleOf"); |
| 119 | + if (multipleOf != schema.end()) { |
| 120 | + double rem = fmod(value, multipleOf.value()); |
| 121 | + if (rem != 0.0) |
| 122 | + throw std::out_of_range(name + " is not a multiple ..."); |
| 123 | + } |
| 124 | + |
| 125 | + const auto &maximum = schema.find("maximum"); |
| 126 | + if (maximum != schema.end()) { |
| 127 | + double maxi = maximum.value(); |
| 128 | + auto ex = std::out_of_range(name + " exceeds maximum of ..."); |
| 129 | + if (schema.find("exclusiveMaximum") != schema.end()) { |
| 130 | + if (value >= maxi) |
| 131 | + throw ex; |
| 132 | + } else { |
| 133 | + if (value > maxi) |
| 134 | + throw ex; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + const auto &minimum = schema.find("minimum"); |
| 139 | + if (minimum != schema.end()) { |
| 140 | + double mini = minimum.value(); |
| 141 | + auto ex = std::out_of_range(name + " exceeds minimum of ..."); |
| 142 | + if (schema.find("exclusiveMinimum") != schema.end()) { |
| 143 | + if (value <= mini) |
| 144 | + throw ex; |
| 145 | + } else { |
| 146 | + if (value < mini) |
| 147 | + throw ex; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + void validate_integer(json &instance, const json &schema, const std::string &name) |
| 153 | + { |
| 154 | + validate_type(schema, "integer", name); |
| 155 | + validate_numeric(instance, schema, name); |
| 156 | + } |
| 157 | + |
| 158 | + void validate_unsigned(json &instance, const json &schema, const std::string &name) |
| 159 | + { |
| 160 | + validate_type(schema, "integer", name); |
| 161 | + validate_numeric(instance, schema, name); |
| 162 | + } |
| 163 | + |
| 164 | + void validate_float(json &instance, const json &schema, const std::string &name) |
| 165 | + { |
| 166 | + validate_type(schema, "number", name); |
| 167 | + validate_numeric(instance, schema, name); |
| 168 | + } |
| 169 | + |
| 170 | + void validate_null(json & /*instance*/, const json &schema, const std::string &name) |
| 171 | + { |
| 172 | + validate_type(schema, "null", name); |
| 173 | + } |
| 174 | + |
| 175 | + void validate_array(json & /*instance*/, const json &schema, const std::string &name) |
| 176 | + { |
| 177 | + not_yet_implemented(schema, "maxItems", "array"); |
| 178 | + not_yet_implemented(schema, "minItems", "array"); |
| 179 | + not_yet_implemented(schema, "uniqueItems", "array"); |
| 180 | + not_yet_implemented(schema, "items", "array"); |
| 181 | + not_yet_implemented(schema, "additionalItems", "array"); |
| 182 | + |
| 183 | + validate_type(schema, "array", name); |
| 184 | + } |
| 185 | + |
| 186 | + void validate_object(json &instance, const json &schema, const std::string &name) |
| 187 | + { |
| 188 | + not_yet_implemented(schema, "maxProperties", "object"); |
| 189 | + not_yet_implemented(schema, "minProperties", "object"); |
| 190 | + not_yet_implemented(schema, "dependencies", "object"); |
| 191 | + |
| 192 | + validate_type(schema, "object", name); |
| 193 | + |
| 194 | + json properties = {}; |
| 195 | + if (schema.find("properties") != schema.end()) |
| 196 | + properties = schema["properties"]; |
| 197 | + |
| 198 | + // check for default values of properties |
| 199 | + // and insert them into this object, if they don't exists |
| 200 | + // works only for object properties for the moment |
| 201 | + if (default_value_insertion) |
| 202 | + for (auto it = properties.begin(); it != properties.end(); ++it) { |
| 203 | + |
| 204 | + const auto &default_value = it.value().find("default"); |
| 205 | + if (default_value == it.value().end()) |
| 206 | + continue; /* no default value -> continue */ |
| 207 | + |
| 208 | + if (instance.find(it.key()) != instance.end()) |
| 209 | + continue; /* value is present */ |
| 210 | + |
| 211 | + /* create element from default value */ |
| 212 | + instance[it.key()] = default_value.value(); |
| 213 | + } |
| 214 | + |
| 215 | + // additionalProperties |
| 216 | + enum { |
| 217 | + True, |
| 218 | + False, |
| 219 | + Object |
| 220 | + } additionalProperties = True; |
| 221 | + |
| 222 | + const auto &additionalPropertiesVal = schema.find("additionalProperties"); |
| 223 | + if (additionalPropertiesVal != schema.end()) { |
| 224 | + if (additionalPropertiesVal.value().type() == json::value_t::boolean) |
| 225 | + additionalProperties = additionalPropertiesVal.value() == true ? True : False; |
| 226 | + else |
| 227 | + additionalProperties = Object; |
| 228 | + } |
| 229 | + |
| 230 | + json patternProperties = {}; |
| 231 | + if (schema.find("patternProperties") != schema.end()) |
| 232 | + patternProperties = schema["patternProperties"]; |
| 233 | + |
| 234 | + // check all elements in object |
| 235 | + for (auto child = instance.begin(); child != instance.end(); ++child) { |
| 236 | + std::string child_name = name + "." + child.key(); |
| 237 | + |
| 238 | + // is this a property which is described in the schema |
| 239 | + const auto &object_prop = properties.find(child.key()); |
| 240 | + if (object_prop != properties.end()) { |
| 241 | + // validate the element with its schema |
| 242 | + validate(child.value(), object_prop.value(), child_name); |
| 243 | + continue; |
| 244 | + } |
| 245 | + |
| 246 | + bool patternProperties_has_matched = false; |
| 247 | + for (auto pp = patternProperties.begin(); |
| 248 | + pp != patternProperties.end(); ++pp) { |
| 249 | + std::regex re(pp.key(), std::regex::ECMAScript); |
| 250 | + |
| 251 | + if (std::regex_search(child.key(), re)) { |
| 252 | + validate(child.value(), pp.value(), child_name); |
| 253 | + patternProperties_has_matched = true; |
| 254 | + } |
| 255 | + } |
| 256 | + if (patternProperties_has_matched) |
| 257 | + continue; |
| 258 | + |
| 259 | + switch (additionalProperties) { |
| 260 | + case True: |
| 261 | + break; |
| 262 | + |
| 263 | + case Object: |
| 264 | + validate(child.value(), additionalPropertiesVal.value(), child_name); |
| 265 | + break; |
| 266 | + |
| 267 | + case False: |
| 268 | + throw std::invalid_argument("unknown property '" + child.key() + "' in object '" + name + "'"); |
| 269 | + break; |
| 270 | + }; |
| 271 | + } |
| 272 | + |
| 273 | + // check for required elements which are not present |
| 274 | + const auto &required = schema.find("required"); |
| 275 | + if (required == schema.end()) |
| 276 | + return; |
| 277 | + |
| 278 | + for (const auto &element : required.value()) { |
| 279 | + if (instance.find(element) == instance.end()) { |
| 280 | + throw std::invalid_argument("required element '" + element.get<std::string>() + |
| 281 | + "' not found in object '" + name + "'"); |
| 282 | + } |
| 283 | + } |
| 284 | + } |
| 285 | + |
| 286 | +public: |
| 287 | + void validate(json &instance, const json &schema, const std::string &name = "root") |
| 288 | + { |
| 289 | + not_yet_implemented(schema, "allOf", "all"); |
| 290 | + not_yet_implemented(schema, "anyOf", "all"); |
| 291 | + not_yet_implemented(schema, "oneOf", "all"); |
| 292 | + not_yet_implemented(schema, "not", "all"); |
| 293 | + not_yet_implemented(schema, "definitions", "all"); |
| 294 | + not_yet_implemented(schema, "$ref", "all"); |
| 295 | + |
| 296 | + validate_enum(instance, schema, name); |
| 297 | + |
| 298 | + switch (instance.type()) { |
| 299 | + case json::value_t::object: |
| 300 | + validate_object(instance, schema, name); |
| 301 | + break; |
| 302 | + |
| 303 | + case json::value_t::array: |
| 304 | + validate_array(instance, schema, name); |
| 305 | + break; |
| 306 | + |
| 307 | + case json::value_t::string: |
| 308 | + validate_string(instance, schema, name); |
| 309 | + break; |
| 310 | + |
| 311 | + case json::value_t::number_unsigned: |
| 312 | + validate_unsigned(instance, schema, name); |
| 313 | + break; |
| 314 | + |
| 315 | + case json::value_t::number_integer: |
| 316 | + validate_integer(instance, schema, name); |
| 317 | + break; |
| 318 | + |
| 319 | + case json::value_t::number_float: |
| 320 | + validate_float(instance, schema, name); |
| 321 | + break; |
| 322 | + |
| 323 | + case json::value_t::boolean: |
| 324 | + validate_boolean(instance, schema, name); |
| 325 | + break; |
| 326 | + |
| 327 | + case json::value_t::null: |
| 328 | + validate_null(instance, schema, name); |
| 329 | + break; |
| 330 | + |
| 331 | + default: |
| 332 | + throw std::out_of_range("type '" + schema["type"].get<std::string>() + |
| 333 | + "' has no validator yet"); |
| 334 | + break; |
| 335 | + } |
| 336 | + } |
| 337 | +}; |
| 338 | +} |
| 339 | + |
| 340 | +#endif /* NLOHMANN_JSON_VALIDATOR_HPP__ */ |
0 commit comments