|
|
@@ -23,6 +23,25 @@
|
|
|
#include <jvalidate/uri.h>
|
|
|
|
|
|
namespace jvalidate::detail {
|
|
|
+/**
|
|
|
+ * @brief An object responsible for owning/managing the various documents,
|
|
|
+ * references, and related functionality for ensuring that we properly construct
|
|
|
+ * things.
|
|
|
+ *
|
|
|
+ * In order to support this we store information on:
|
|
|
+ * - A {@see jvalidate::detail::ReferenceCache} that maps various absolute
|
|
|
+ * Reference paths to their Canonical forms.
|
|
|
+ * - "Vocabularies", which describe the the set of legal keywords for
|
|
|
+ * constraint parsing.
|
|
|
+ * - "Anchor Locations", a non-owning store of the Adapters associated with
|
|
|
+ * "$id"/"$anchor" tags to allow quick lookups without having to re-walk the
|
|
|
+ * document.
|
|
|
+ * - "Dynamic Anchors", a list of all of the "$dynamicAnchor" tags that exist
|
|
|
+ * under a given "$id" tag, and those bindings which are active in the current
|
|
|
+ * scope.
|
|
|
+ *
|
|
|
+ * @tparam A The adapter type being operated upon
|
|
|
+ */
|
|
|
template <Adapter A> class ReferenceManager {
|
|
|
private:
|
|
|
static inline std::map<std::string_view, schema::Version> const g_schema_ids{
|
|
|
@@ -48,12 +67,36 @@ private:
|
|
|
DynamicReferenceContext active_dynamic_anchors_;
|
|
|
|
|
|
public:
|
|
|
+ /**
|
|
|
+ * @brief Construct a new ReferenceManager around a given root schema
|
|
|
+ *
|
|
|
+ * @param external A cache/loader of external documents. Due to the way that
|
|
|
+ * {@see jvalidate::Schema} is implemented, the cache may have the same
|
|
|
+ * lifetime as this object, despite being owned by mutable reference.
|
|
|
+ *
|
|
|
+ * @param root The root schema being operated on.
|
|
|
+ *
|
|
|
+ * @param version The version of the schema being used for determining the
|
|
|
+ * base vocabulary to work with (see the definition of schema::Version for
|
|
|
+ * more details on how the base vocabulary changes).
|
|
|
+ *
|
|
|
+ * @param constraints A factory for turning JSON schema information into
|
|
|
+ * constraints.
|
|
|
+ */
|
|
|
ReferenceManager(DocumentCache<A> & external, A const & root, schema::Version version,
|
|
|
ConstraintFactory<A> const & constraints)
|
|
|
: external_(external), constraints_(constraints), roots_{{{}, root}} {
|
|
|
prime(root, {}, &vocab(version));
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @brief Turn a schema version into a vocabulary, ignoring user-defined
|
|
|
+ * vocabularies
|
|
|
+ *
|
|
|
+ * @param version The schema version
|
|
|
+ *
|
|
|
+ * @returns The default vocabulary for a given draft version
|
|
|
+ */
|
|
|
Vocabulary<A> const & vocab(schema::Version version) {
|
|
|
if (not vocabularies_.contains(version)) {
|
|
|
vocabularies_.emplace(version, constraints_.keywords(version));
|
|
|
@@ -61,6 +104,18 @@ public:
|
|
|
return vocabularies_.at(version);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @brief Fetch the vocabulary information associated with a given "$schema"
|
|
|
+ * tag. Unlike the enum version of this function, we can also load
|
|
|
+ * user-defined schemas using the ReferenceCache object, if supported. This
|
|
|
+ * allows us to define custom constraints or remove some that we want to
|
|
|
+ * forbid.
|
|
|
+ *
|
|
|
+ * @param schema The location of the schema being fetched
|
|
|
+ *
|
|
|
+ * @returns If schema is a draft version - then one of the default
|
|
|
+ * vocabularies, else a user-schema is loaded.
|
|
|
+ */
|
|
|
Vocabulary<A> const & vocab(URI schema) {
|
|
|
if (auto it = g_schema_ids.find(schema.resource()); it != g_schema_ids.end()) {
|
|
|
return vocab(it->second);
|
|
|
@@ -75,6 +130,12 @@ public:
|
|
|
EXPECT_M(external->type() == adapter::Type::Object, "meta-schema must be an object");
|
|
|
|
|
|
auto metaschema = external->as_object();
|
|
|
+ // All user-defined schemas MUST have a parent schema they point to
|
|
|
+ // Furthermore - in order to be well-formed, the schema chain must
|
|
|
+ // eventually point to one of the draft schemas. However - if a metaschema
|
|
|
+ // ends up in a recusive situation (e.g. A -> B -> A), it will not fail in
|
|
|
+ // the parsing step, but instead produce a malformed Schema object for
|
|
|
+ // validation.
|
|
|
EXPECT_M(metaschema.contains("$schema"),
|
|
|
"user-defined meta-schema must reference a base schema");
|
|
|
|
|
|
@@ -83,6 +144,10 @@ public:
|
|
|
parent = vocab(URI(metaschema["$schema"].as_string()));
|
|
|
|
|
|
if (metaschema.contains("$vocabulary")) {
|
|
|
+ // This is a silly thing we have to do because rather than have some kind
|
|
|
+ // of annotation/assertion divide marker for the format constraint, we
|
|
|
+ // instead use true/false in Draft2019-09, and have format-assertion/
|
|
|
+ // format-annotation vocabularies in Draft2020-12.
|
|
|
auto [keywords, vocabularies] = extract_keywords(metaschema["$vocabulary"].as_object());
|
|
|
parent.restrict(keywords, vocabularies);
|
|
|
}
|
|
|
@@ -90,12 +155,39 @@ public:
|
|
|
return parent;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @brief Load the current location into the stack of dynamic ref/anchors so
|
|
|
+ * that we are able to properly resolve them (e.g. because an anchor got
|
|
|
+ * disabled).
|
|
|
+ *
|
|
|
+ * @param ref The current parsing location in the schema, which should
|
|
|
+ * correspond with an "$id" tag.
|
|
|
+ *
|
|
|
+ * @returns A scope object that will remove this set of dynamic ref/anchor
|
|
|
+ * resolutions from the stack when it exits scope.
|
|
|
+ */
|
|
|
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]);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @breif "Load" a requested document reference, which may exist in the
|
|
|
+ * current document, or in an external one.
|
|
|
+ *
|
|
|
+ * @param ref The location to load. Since there is no guarantee of direct
|
|
|
+ * relation between the current scope and this reference, we treat this like a
|
|
|
+ * jump.
|
|
|
+ *
|
|
|
+ * @param vocab The current vocabulary being used for parsing. It may be
|
|
|
+ * changed when loading the new reference if there is a "$schema" tag at the
|
|
|
+ * root.
|
|
|
+ *
|
|
|
+ * @returns The schema corresponding to the reference, if it can be located.
|
|
|
+ * As long as ref contains a valid URI/Anchor, we will return an Adapter, even
|
|
|
+ * if that adapter might point to a null JSON.
|
|
|
+ */
|
|
|
std::optional<A> load(Reference const & ref, Vocabulary<A> const * vocab) {
|
|
|
if (auto it = roots_.find(ref.root()); it != roots_.end()) {
|
|
|
return ref.pointer().walk(it->second);
|
|
|
@@ -114,22 +206,47 @@ public:
|
|
|
return ref.pointer().walk(it->second);
|
|
|
}
|
|
|
|
|
|
- // Will get called if the external schema does not declare a root document id?
|
|
|
+ // Will get called if the external schema does not declare a root id?
|
|
|
return ref.pointer().walk(*external);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @brief Transform a reference into its "canonical" form, in the context of
|
|
|
+ * the calling context (parent).
|
|
|
+ *
|
|
|
+ * @param ref The value of a "$ref" or "$dynamicRef" token, that is being
|
|
|
+ * looked up.
|
|
|
+ *
|
|
|
+ * @param parent The current lexical scope being operated in.
|
|
|
+ *
|
|
|
+ * @param dynamic_reference As an input, indicates that we are requesting a
|
|
|
+ * dynamic reference instead of a normal $ref.
|
|
|
+ * As an output, indicates that we effectively did resolve a dynamicRef and
|
|
|
+ * therefore should alter the dynamic scope in order to prevent infinite
|
|
|
+ * recursions in schema parsing.
|
|
|
+ *
|
|
|
+ * @returns ref, but in its canonical/lexical form.
|
|
|
+ */
|
|
|
Reference canonicalize(Reference const & ref, Reference const & parent,
|
|
|
inout<bool> dynamic_reference) {
|
|
|
URI const uri = [this, &ref, &parent]() {
|
|
|
+ // If there are no URIs involed (root schema does not set "$id")
|
|
|
+ // then we don't need to do anything clever
|
|
|
if (ref.uri().empty() && parent.uri().empty()) {
|
|
|
return references_.actual_parent_uri(parent);
|
|
|
}
|
|
|
|
|
|
+ // At least one of ref and parent have a real URI/"$id" value. If it has a
|
|
|
+ // "root" (e.g. file:// or http://), then we don't need to do any clever
|
|
|
+ // alterations to identify the root.
|
|
|
URI uri = ref.uri().empty() ? parent.uri() : ref.uri();
|
|
|
if (not uri.is_rootless()) {
|
|
|
return uri;
|
|
|
}
|
|
|
|
|
|
+ // Now we need to compute that URI into the context of its parent, such
|
|
|
+ // as if ref := "file.json" and
|
|
|
+ // parent := "http://localhost:8000/schemas/root.json"
|
|
|
URI base = references_.actual_parent_uri(parent);
|
|
|
EXPECT_M(base.resource().rfind('/') != std::string::npos,
|
|
|
"Unable to deduce root for relative uri " << uri << " (" << base << ")");
|
|
|
@@ -161,6 +278,25 @@ public:
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
+ /**
|
|
|
+ * @brief Locate the dynamic reference being requested (if it is being
|
|
|
+ * requested).
|
|
|
+ *
|
|
|
+ * @param uri The dynamic reference uri being requested, generally empty.
|
|
|
+ *
|
|
|
+ * @param ref The value of a "$ref" or "$dynamicRef" token, that is being
|
|
|
+ * looked up. Primarily used for the anchor value, which is relevant for
|
|
|
+ * $dynamicRef/$dynamicAnchor.
|
|
|
+ *
|
|
|
+ * @param dynamic_reference As an input, indicates that we are requesting a
|
|
|
+ * dynamic reference instead of a normal $ref.
|
|
|
+ * As an output, indicates that we effectively did resolve a dynamicRef and
|
|
|
+ * therefore should alter the dynamic scope in order to prevent infinite
|
|
|
+ * recursions in schema parsing.
|
|
|
+ *
|
|
|
+ * @returns If there is a dynamic reference for the requested anchor, we
|
|
|
+ * return it.
|
|
|
+ */
|
|
|
std::optional<Reference> dynamic(URI const & uri, Reference const & ref,
|
|
|
inout<bool> dynamic_reference) {
|
|
|
bool const anchor_is_dynamic = active_dynamic_anchors_.contains(ref.anchor());
|
|
|
@@ -184,12 +320,22 @@ private:
|
|
|
return active_dynamic_anchors_.lookup(uri, ref.anchor());
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @brief Prepare a newly loaded document, importing schema information,
|
|
|
+ * ids, anchors, and dynamic anchors recursively.
|
|
|
+ *
|
|
|
+ * @param json The document being loaded
|
|
|
+ *
|
|
|
+ * @param vocab The vocabulary of legitimate keywords to iterate through to
|
|
|
+ * locate ids etc.
|
|
|
+ */
|
|
|
void prime(Adapter auto const & json, Reference where, Vocabulary<A> const * vocab) {
|
|
|
if (json.type() != adapter::Type::Object) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
auto schema = json.as_object();
|
|
|
+ // Update vocabulary to the latest form
|
|
|
if (schema.contains("$schema")) {
|
|
|
vocab = &this->vocab(URI(schema["$schema"].as_string()));
|
|
|
}
|
|
|
@@ -204,16 +350,24 @@ private:
|
|
|
}
|
|
|
switch (value.type()) {
|
|
|
case adapter::Type::Array: {
|
|
|
+ // Recurse through array-type schemas, such as anyOf, allOf, and oneOf
|
|
|
+ // we don't actually check that the key is one of those, because if we
|
|
|
+ // do something stupid like "not": [] then the parsing phase will return
|
|
|
+ // an error.
|
|
|
for (auto const & [index, elem] : detail::enumerate(value.as_array())) {
|
|
|
prime(elem, where / key / index, vocab);
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
case adapter::Type::Object:
|
|
|
+ // Normal schema-type data such as not, additionalItems, etc. hold a
|
|
|
+ // schema as their immidiate child.
|
|
|
if (not vocab->is_property_keyword(key)) {
|
|
|
prime(value, where / key, vocab);
|
|
|
break;
|
|
|
}
|
|
|
+ // Special schemas are key-value stores, where the key is arbitrary and
|
|
|
+ // the value is the schema. Therefore we need to skip over the props.
|
|
|
for (auto const & [prop, elem] : value.as_object()) {
|
|
|
prime(elem, where / key / prop, vocab);
|
|
|
}
|
|
|
@@ -223,6 +377,19 @@ private:
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @brief Optionally register any root document at this location, as
|
|
|
+ * designated by things like the "$id" and "$anchor" tags.
|
|
|
+ *
|
|
|
+ * @param where The current lexical location in the schema - if there is an
|
|
|
+ * id/anchor tag, then we overwrite this value with the newly indicated root.
|
|
|
+ *
|
|
|
+ * @param version The current schema version - used to denote the name of the
|
|
|
+ * id tag, whether anchors are available, and how dynamic anchors function
|
|
|
+ * (Draft2019-09's recursiveAnchor vs. Draft2020-12's dynamicAnchor).
|
|
|
+ *
|
|
|
+ * @param json The document being primed.
|
|
|
+ */
|
|
|
void prime_roots(Reference & where, schema::Version version, A const & json) {
|
|
|
std::string const id = version <= schema::Version::Draft04 ? "id" : "$id";
|
|
|
auto const schema = json.as_object();
|
|
|
@@ -233,7 +400,7 @@ private:
|
|
|
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
|
|
|
+ // By definition - rooted URIs cannot be relative
|
|
|
} else if (root.uri().is_relative()) {
|
|
|
root = RootReference(where.uri().parent() / root.uri(), root.anchor());
|
|
|
} else {
|
|
|
@@ -255,6 +422,8 @@ private:
|
|
|
where = references_.emplace(where, root);
|
|
|
}
|
|
|
|
|
|
+ // Unfortunately - $recursiveAnchor and $dynamicAnchor use very different
|
|
|
+ // handling mechanisms, so it is not convenient to merge together
|
|
|
if (version == schema::Version::Draft2019_09 && schema.contains("$recursiveAnchor") &&
|
|
|
schema["$recursiveAnchor"].as_boolean()) {
|
|
|
Anchor anchor;
|
|
|
@@ -283,6 +452,15 @@ private:
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * @brief Extract the supported keywords of a given selection of vocabularies
|
|
|
+ *
|
|
|
+ * @param vocabularies A map of the form (VocabularyURI => Enabled)
|
|
|
+ *
|
|
|
+ * @returns A pair containing:
|
|
|
+ * - All of the enabled keywords in the vocabulary
|
|
|
+ * - The list of enabled vocabulary metaschema (used for is_format_assertion)
|
|
|
+ */
|
|
|
auto extract_keywords(ObjectAdapter auto const & vocabularies) const
|
|
|
-> std::pair<std::unordered_set<std::string>, std::unordered_set<std::string>> {
|
|
|
std::unordered_set<std::string> keywords;
|