validator.h 966 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #pragma once
  2. #include <regex>
  3. #include <unordered_map>
  4. #include <jvalidate/forward.h>
  5. #include <jvalidate/status.h>
  6. #include <jvalidate/validation_visitor.h>
  7. namespace jvalidate::detail {
  8. class StdRegexEngine {
  9. public:
  10. std::regex regex_;
  11. public:
  12. StdRegexEngine(std::string const & regex) : regex_(regex) {}
  13. bool search(std::string const & text) const { return std::regex_search(text, regex_); }
  14. };
  15. }
  16. namespace jvalidate {
  17. template <RegexEngine RE = detail::StdRegexEngine> class ValidatorT {
  18. private:
  19. schema::Node const & schema_;
  20. std::unordered_map<std::string, RE> regex_cache_;
  21. public:
  22. ValidatorT(schema::Node const & schema) : schema_(schema) {}
  23. template <Adapter A>
  24. Status validate(A const & json, ValidationResult const * result = nullptr) const {
  25. return ValidationVisitor<A, RE>(json, schema_, regex_cache_, result).validate();
  26. }
  27. };
  28. class Validator : public ValidatorT<> {
  29. public:
  30. using Validator::ValidatorT::ValidatorT;
  31. };
  32. }