#pragma once #include #include #include #include #include #include namespace jvalidate::detail { template struct ParserContext; template class Vocabulary { private: schema::Version version_; std::unordered_map> metadata_; std::unordered_set permitted_; std::unordered_set vocabularies_; public: Vocabulary() = default; Vocabulary(schema::Version version, std::unordered_map> metadata) : version_(version), metadata_(std::move(metadata)) { for (auto const & [keyword, _] : metadata_) { permitted_.emplace(keyword); } } /** * @brief Reset the list of keywords that Vocabulary actually respects * * @param permitted_keywords The selection of keywords to allow for * searches/constraint building. Note that a constraint might be * registered to a null function for compatibility with this. * * @param vocabularies An optional selection of vocabulary schemas, used * as metadata, and deducing {@see is_format_assertion}. */ void restrict(std::unordered_map const & permitted_keywords, std::unordered_set const & vocabularies = {}) & { permitted_.clear(); vocabularies_ = vocabularies; for (auto const & [keyword, _] : metadata_) { // We only file permitted_keywords into this Vocabulary if we have defined // bindings for that keyword if (permitted_keywords.contains(std::string(keyword))) { permitted_.insert(keyword); } } for (auto const & [keyword, required] : permitted_keywords) { EXPECT_M(not required || keyword.starts_with("$") || metadata_.contains(keyword), "Vocabulary Keyword " << keyword << " required, but not implemented"); } } 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 certain 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 vocabularies_.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. This is a slightly more * lenient version of {@see is_constraint} - since it allows keywords that * have a null factory, as long as they've been registered (e.g. then/else). * * @param word The "key"word being looked up (e.g. "if", "properties", ...) */ bool is_keyword(std::string_view word) const { return has(word) && metadata_.at(word).type != vocabulary::KeywordType::None; } /** * @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 * * @param word The "key"word being looked up (e.g. "if", "properties", ...) */ bool is_property_keyword(std::string_view word) const { return has(word) && metadata_.at(word).type == vocabulary::KeywordType::KeywordMap; } /** * @brief Is the given word a real constraint in the Vocabulary. In essence, * it must be an enabled keyword AND it must have a non-null factory function. * * @param word The "key"word being looked up (e.g. "if", "properties", ...) */ bool is_constraint(std::string_view word) const { return has(word) && metadata_.at(word).make; } /** * @brief Fabricate the given constraint if real from the current context * * @param word The "key"word being looked up (e.g. "if", "properties", ...) * * @param context The current context of schema parsing, used for re-entrancy. * * @returns A pair whose first element is either a pointer to a constraint * (if word represents a supported constraint AND the constraint resolves to * something meaningful), else null. * * The second element is a boolean indicating if the constraint needs to be * evaluated after other constraints to use their tracking/annotations. * See the above comments on s_post_constraints for more info. */ auto constraint(std::string_view word, ParserContext const & context) const { return std::make_pair(is_constraint(word) ? metadata_.at(word).make(context) : nullptr, has(word) && metadata_.at(word).is_post_constraint); } private: bool has(std::string_view word) const { return permitted_.contains(word) && metadata_.contains(word); } }; }