JsonValidator.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * JsonValidator.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "JsonNode.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. /// Class for Json validation. Mostly compliant with json-schema v6 draf
  14. struct JsonValidator
  15. {
  16. /// path from root node to current one.
  17. /// JsonNode is used as variant - either string (name of node) or as float (index in list)
  18. std::vector<JsonNode> currentPath;
  19. /// Stack of used schemas. Last schema is the one used currently.
  20. /// May contain multiple items in case if remote references were found
  21. std::vector<std::string> usedSchemas;
  22. /// generates error message
  23. std::string makeErrorMessage(const std::string &message);
  24. using TFormatValidator = std::function<std::string(const JsonNode &)>;
  25. using TFormatMap = std::unordered_map<std::string, TFormatValidator>;
  26. using TFieldValidator = std::function<std::string(JsonValidator &, const JsonNode &, const JsonNode &, const JsonNode &)>;
  27. using TValidatorMap = std::unordered_map<std::string, TFieldValidator>;
  28. /// map of known fields in schema
  29. const TValidatorMap & getKnownFieldsFor(JsonNode::JsonType type);
  30. const TFormatMap & getKnownFormats();
  31. std::string check(const std::string & schemaName, const JsonNode & data);
  32. std::string check(const JsonNode & schema, const JsonNode & data);
  33. };
  34. VCMI_LIB_NAMESPACE_END