#pragma once #include #include #include #include #include namespace jvalidate::detail { template struct ParserContext; template class Vocabulary { public: friend class ConstraintFactory; using pConstraint = std::unique_ptr; using MakeConstraint = std::function const &)>; private: schema::Version version_; std::unordered_map make_; std::unordered_set special_keywords_; // TODO(samjaffe): Migrate this back to constraintsfactory std::unordered_set keywords_{"$defs", "additionalItems", "additionalProperties", "allOf", "anyOf", "definitions", "dependencies", "dependentSchemas", "else", "if", "items", "not", "oneOf", "patternProperties", "prefixItems", "properties", "then", "unevaluatedItems", "unevaluatedProperties"}; std::unordered_set property_keywords_{ "$defs", "definitions", "dependencies", "dependentSchemas", "patternProperties", "properties"}; std::unordered_set post_constraints_{"unevaluatedItems", "unevaluatedProperties"}; public: Vocabulary(schema::Version version, std::unordered_map make) : version_(version), make_(std::move(make)) {} schema::Version version() const { return version_; } /** * @brief Is the given "key"word actually a keyword? As in, would * I expect to resolve a constraint out of it. */ bool is_keyword(std::string_view word) const { return make_.contains(word) && keywords_.contains(word); } /** * @brief Does the given "key"word represent a property object - that is to * say, an object containing some number of schemas mapped by arbitrary keys */ bool is_property_keyword(std::string_view word) const { return is_keyword(word) && property_keywords_.contains(word); } bool is_constraint(std::string_view word) const { return make_.contains(word) && make_.at(word); } auto constraint(std::string_view word, ParserContext const & context) const { return std::make_pair(is_constraint(word) ? make_.at(word)(context) : nullptr, post_constraints_.contains(word)); } }; }