#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace jvalidate::detail { template class ReferenceManager { public: using Keywords = std::unordered_map>; private: static inline std::map const g_schema_ids{ {"json-schema.org/draft-03/schema", schema::Version::Draft03}, {"json-schema.org/draft-04/schema", schema::Version::Draft04}, {"json-schema.org/draft-06/schema", schema::Version::Draft06}, {"json-schema.org/draft-07/schema", schema::Version::Draft07}, {"json-schema.org/draft/2019-09/schema", schema::Version::Draft2019_09}, {"json-schema.org/draft/2020-12/schema", schema::Version::Draft2020_12}, }; ConstraintFactory const & constraints_; DocumentCache & external_; ReferenceCache references_; std::map> vocabularies_; std::map> user_vocabularies_; std::map roots_; std::map> dynamic_anchors_; DynamicReferenceContext active_dynamic_anchors_; public: ReferenceManager(DocumentCache & external, A const & root, schema::Version version, ConstraintFactory const & constraints) : external_(external), constraints_(constraints), roots_{{{}, root}} { prime(root, {}, &vocab(version)); } Vocabulary const & vocab(schema::Version version) { if (not vocabularies_.contains(version)) { vocabularies_.emplace(version, constraints_.keywords(version)); } return vocabularies_.at(version); } Vocabulary const & vocab(URI schema) { if (auto it = g_schema_ids.find(schema.resource()); it != g_schema_ids.end()) { return vocab(it->second); } if (auto it = user_vocabularies_.find(schema); it != user_vocabularies_.end()) { return it->second; } std::optional external = external_.try_load(schema); EXPECT_M(external.has_value(), "Unable to load external meta-schema " << schema); EXPECT_M(external->type() == adapter::Type::Object, "meta-schema must be an object"); auto metaschema = external->as_object(); EXPECT_M(metaschema.contains("$schema"), "meta-schema must reference an"); // Initialize first to prevent recursion Vocabulary & parent = user_vocabularies_[schema]; parent = vocab(URI(metaschema["$schema"].as_string())); if (metaschema.contains("$vocabulary")) { auto [keywords, vocabularies] = extract_keywords(metaschema["$vocabulary"].as_object()); parent.restrict(keywords, vocabularies); } return parent; } auto dynamic_scope(Reference const & ref) { URI const uri = ref.pointer().empty() ? ref.uri() : references_.relative_to_nearest_anchor(ref).uri(); return active_dynamic_anchors_.scope(uri, dynamic_anchors_[uri]); } std::optional load(Reference const & ref, schema::Version version) { if (auto it = roots_.find(ref.root()); it != roots_.end()) { return ref.pointer().walk(it->second); } std::optional external = external_.try_load(ref.uri()); if (not external) { return std::nullopt; } // TODO(samjaffe): Change Versions if needed... references_.emplace(ref.uri()); prime(*external, ref, &vocab(version)); // May have a sub-id that we map to if (auto it = roots_.find(ref.root()); it != roots_.end()) { return ref.pointer().walk(it->second); } // Will get called if the external schema does not declare a root document id? return ref.pointer().walk(*external); } Reference canonicalize(Reference const & ref, Reference const & parent, inout dynamic_reference) { URI const uri = [this, &ref, &parent]() { if (ref.uri().empty() && parent.uri().empty()) { return references_.actual_parent_uri(parent); } URI uri = ref.uri().empty() ? parent.uri() : ref.uri(); if (not uri.is_rootless()) { return uri; } URI base = references_.actual_parent_uri(parent); EXPECT_M(base.resource().rfind('/') != std::string::npos, "Unable to deduce root for relative uri " << uri << " (" << base << ")"); if (not uri.is_relative()) { return base.root() / uri; } if (auto br = base.resource(), ur = uri.resource(); br.ends_with(ur) && br[br.size() - ur.size() - 1] == '/') { return base; } return base.parent() / uri; }(); URI const dyn_uri = ref.uri().empty() ? ref.uri() : uri; if (std::optional dynref = dynamic(dyn_uri, ref, dynamic_reference)) { return *dynref; } dynamic_reference = dynamic_reference || active_dynamic_anchors_.empty(); // Relative URI, not in the HEREDOC (or we set an $id) if (ref.uri().empty() and ref.anchor().empty()) { return Reference(references_.relative_to_nearest_anchor(parent).root(), ref.pointer()); } return Reference(uri, ref.anchor(), ref.pointer()); } private: std::optional dynamic(URI const & uri, Reference const & ref, inout dynamic_reference) { bool const anchor_is_dynamic = active_dynamic_anchors_.contains(ref.anchor()); if (not dynamic_reference) { // A normal $ref to an $anchor that matches a $dynamicAnchor breaks the // dynamic recursion pattern. This requires that we are not looking for a // subschema of the anchor AND that we are not targetting an anchor in a // different root document. dynamic_reference = (anchor_is_dynamic && ref.uri().empty() && ref.pointer().empty()); return std::nullopt; } OnBlockExit scope; if (not ref.uri().empty() && anchor_is_dynamic) { // Register the scope of this (potential) $dynamicAnchor BEFORE we attempt // to enter the reference, in case we end up pointing to an otherwise // suppressed $dynamicAnchor in a higher scope. scope = dynamic_scope(Reference(uri)); } return active_dynamic_anchors_.lookup(uri, ref.anchor()); } void prime(Adapter auto const & json, Reference where, Vocabulary const * vocab) { if (json.type() != adapter::Type::Object) { return; } canonicalize(where, vocab->version(), json); auto schema = json.as_object(); if (schema.contains("$schema")) { vocab = &this->vocab(URI(schema["$schema"].as_string())); } for (auto const & [key, value] : schema) { if (not vocab->is_keyword(key)) { continue; } switch (value.type()) { case adapter::Type::Array: { size_t index = 0; for (auto const & elem : value.as_array()) { prime(elem, where / key / index, vocab); ++index; } break; } case adapter::Type::Object: if (not vocab->is_property_keyword(key)) { prime(value, where / key, vocab); break; } for (auto const & [prop, elem] : value.as_object()) { prime(elem, where / key / prop, vocab); } default: break; } } } void canonicalize(Reference & where, schema::Version version, A const & json) { std::string const id = version <= schema::Version::Draft04 ? "id" : "$id"; auto const schema = json.as_object(); RootReference root = where.root(); if (schema.contains(id)) { root = RootReference(schema[id].as_string()); if (root.uri().empty()) { root = RootReference(where.uri(), root.anchor()); } else if (not root.uri().is_rootless() || where.uri().empty()) { // By definition - rootless URIs cannot be relative } else if (root.uri().is_relative()) { root = RootReference(where.uri().parent() / root.uri(), root.anchor()); } else { root = RootReference(where.uri().root() / root.uri(), root.anchor()); } roots_.emplace(root, json); where = references_.emplace(where, root); } // $anchor and its related keywords were introduced in Draft 2019-09 if (version < schema::Version::Draft2019_09) { return; } if (schema.contains("$anchor")) { root = RootReference(root.uri(), Anchor(schema["$anchor"].as_string())); roots_.emplace(root, json); where = references_.emplace(where, root); } if (version == schema::Version::Draft2019_09 && schema.contains("$recursiveAnchor") && schema["$recursiveAnchor"].as_boolean()) { Anchor anchor; root = RootReference(root.uri(), anchor); roots_.emplace(root, json); where = references_.emplace(where, root); if (Reference & dynamic = dynamic_anchors_[root.uri()][anchor]; dynamic == Reference() || where < dynamic) { dynamic = where; } } if (schema.contains("$dynamicAnchor") && version > schema::Version::Draft2019_09) { Anchor anchor(schema["$dynamicAnchor"].as_string()); root = RootReference(root.uri(), anchor); roots_.emplace(root, json); where = references_.emplace(where, root); if (Reference & dynamic = dynamic_anchors_[root.uri()][anchor]; dynamic == Reference() || where < dynamic) { dynamic = where; } } } auto extract_keywords(ObjectAdapter auto const & vocabularies) const -> std::pair, std::unordered_set> { std::unordered_set keywords; std::unordered_set vocab_docs; for (auto [vocab, enabled] : vocabularies) { if (not enabled.as_boolean()) { continue; } size_t n = vocab.find("/vocab/"); vocab_docs.emplace(vocab.substr(n)); vocab.replace(n, 7, "/meta/"); auto vocab_object = external_.try_load(URI(vocab)); for (auto const & [keyword, _] : vocab_object->as_object()["properties"].as_object()) { keywords.insert(keyword); } } return std::make_pair(keywords, vocab_docs); } }; }