| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- #pragma once
- #include <string_view>
- #include <unordered_map>
- #include <unordered_set>
- #include <jvalidate/enum.h>
- #include <jvalidate/forward.h>
- namespace jvalidate::detail {
- template <Adapter A> struct ParserContext;
- template <Adapter A> class Vocabulary {
- public:
- friend class ConstraintFactory<A>;
- using pConstraint = std::unique_ptr<constraint::Constraint>;
- using MakeConstraint = std::function<pConstraint(ParserContext<A> const &)>;
- private:
- schema::Version version_;
- std::unordered_map<std::string_view, MakeConstraint> make_;
- std::unordered_set<std::string_view> permitted_;
- std::unordered_set<std::string> vocabularies_;
- // TODO(samjaffe): Migrate this back to constraintsfactory
- std::unordered_set<std::string_view> keywords_{"$defs",
- "additionalItems",
- "additionalProperties",
- "allOf",
- "anyOf",
- "definitions",
- "dependencies",
- "dependentSchemas",
- "else",
- "extends",
- "if",
- "items",
- "not",
- "oneOf",
- "patternProperties",
- "prefixItems",
- "properties",
- "then",
- "unevaluatedItems",
- "unevaluatedProperties"};
- std::unordered_set<std::string_view> property_keywords_{
- "$defs", "definitions", "dependencies", "dependentSchemas", "patternProperties",
- "properties"};
- std::unordered_set<std::string_view> post_constraints_{"unevaluatedItems",
- "unevaluatedProperties"};
- public:
- Vocabulary() = default;
- Vocabulary(schema::Version version, std::unordered_map<std::string_view, MakeConstraint> make)
- : version_(version), make_(std::move(make)) {
- for (auto const & [keyword, _] : make_) {
- permitted_.emplace(keyword);
- }
- }
- void restrict(std::unordered_set<std::string> const & permitted_keywords,
- std::unordered_set<std::string> const & vocabularies) & {
- permitted_.clear();
- vocabularies_ = vocabularies;
- for (auto const & [keyword, _] : make_) {
- if (permitted_keywords.contains(std::string(keyword))) {
- permitted_.insert(keyword);
- }
- }
- }
- schema::Version version() const { return version_; }
- bool is_format_assertion() const {
- // In Draft07 and prior - format assertions were considered enabled by
- // default. This is - of course - problematic because very few
- // implementations actually had full support for format constraints.
- if (version_ < schema::Version::Draft2019_09) {
- return true;
- }
- // Some implementations wouldn't even bother with format constraints, and
- // others would provide implementations that either missed a number of edge
- // cases or were flat-out wrong on certail matters.
- // Therefore - starting in Draft 2019-09, the format keyword is an
- // annotation by default, instead of an assertion.
- if (version_ == schema::Version::Draft2019_09) {
- return permitted_.contains("/vocab/format");
- }
- // Draft 2020-12 makes this even more explicit - having separate vocabulary
- // documents for "format as assertion" and "format as annotation". Allowing
- // validators to add format constraints that are only used for annotating
- // results.
- return vocabularies_.contains("/vocab/format-assertion");
- }
- /**
- * @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 permitted_.contains(word) && 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 permitted_.contains(word) && make_.contains(word) && make_.at(word);
- }
- auto constraint(std::string_view word, ParserContext<A> const & context) const {
- return std::make_pair(is_constraint(word) ? make_.at(word)(context) : nullptr,
- post_constraints_.contains(word));
- }
- };
- }
|