| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- #pragma once
- #include <regex>
- #include <unordered_map>
- #include <jvalidate/forward.h>
- #include <jvalidate/status.h>
- #include <jvalidate/validation_config.h>
- #include <jvalidate/validation_visitor.h>
- namespace jvalidate::detail {
- class StdRegexEngine {
- public:
- std::regex regex_;
- public:
- StdRegexEngine(std::string const & regex) : regex_(regex) {}
- bool search(std::string const & text) const { return std::regex_search(text, regex_); }
- };
- }
- namespace jvalidate {
- template <RegexEngine RE = detail::StdRegexEngine> class ValidatorT {
- private:
- schema::Node const & schema_;
- ValidationConfig cfg_;
- std::unordered_map<std::string, RE> regex_cache_;
- public:
- ValidatorT(schema::Node const & schema, ValidationConfig const & cfg = {})
- : schema_(schema), cfg_(cfg) {}
- template <Adapter A>
- requires(not MutableAdapter<A>) bool validate(A const & json,
- ValidationResult * result = nullptr) {
- EXPECT_M(not cfg_.construct_default_values,
- "Cannot perform mutations on an immutable JSON Adapter");
- return static_cast<bool>(
- ValidationVisitor<A, RE>(json, schema_, cfg_, regex_cache_, result).validate());
- }
- template <MutableAdapter A> bool validate(A const & json, ValidationResult * result = nullptr) {
- return static_cast<bool>(
- ValidationVisitor<A, RE>(json, schema_, cfg_, regex_cache_, result).validate());
- }
- template <typename JSON>
- requires(not Adapter<JSON>) bool validate(JSON & json, ValidationResult * result = nullptr) {
- return validate(adapter::AdapterFor<JSON>(json), result);
- }
- };
- class Validator : public ValidatorT<> {
- public:
- using Validator::ValidatorT::ValidatorT;
- };
- }
|