2
0

JsonValidator.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. //Internal class for Json validation. Mostly compilant with json-schema v6 draft
  14. namespace Validation
  15. {
  16. /// struct used to pass data around during validation
  17. struct ValidationData
  18. {
  19. /// path from root node to current one.
  20. /// JsonNode is used as variant - either string (name of node) or as float (index in list)
  21. std::vector<JsonNode> currentPath;
  22. /// Stack of used schemas. Last schema is the one used currently.
  23. /// May contain multiple items in case if remote references were found
  24. std::vector<std::string> usedSchemas;
  25. /// generates error message
  26. std::string makeErrorMessage(const std::string &message);
  27. };
  28. using TFormatValidator = std::function<std::string(const JsonNode &)>;
  29. using TFormatMap = std::unordered_map<std::string, TFormatValidator>;
  30. using TFieldValidator = std::function<std::string(ValidationData &, const JsonNode &, const JsonNode &, const JsonNode &)>;
  31. using TValidatorMap = std::unordered_map<std::string, TFieldValidator>;
  32. /// map of known fields in schema
  33. const TValidatorMap & getKnownFieldsFor(JsonNode::JsonType type);
  34. const TFormatMap & getKnownFormats();
  35. std::string check(const std::string & schemaName, const JsonNode & data);
  36. std::string check(const std::string & schemaName, const JsonNode & data, ValidationData & validator);
  37. std::string check(const JsonNode & schema, const JsonNode & data, ValidationData & validator);
  38. }
  39. VCMI_LIB_NAMESPACE_END