|
|
@@ -13,6 +13,7 @@
|
|
|
#include <jvalidate/detail/parser_context.h>
|
|
|
#include <jvalidate/detail/pointer.h>
|
|
|
#include <jvalidate/detail/reference.h>
|
|
|
+#include <jvalidate/detail/version.h>
|
|
|
#include <jvalidate/document_cache.h>
|
|
|
#include <jvalidate/enum.h>
|
|
|
#include <jvalidate/forward.h>
|
|
|
@@ -29,11 +30,6 @@ private:
|
|
|
std::unordered_map<std::string, std::unique_ptr<constraint::Constraint>> constraints_{};
|
|
|
std::unordered_map<std::string, std::unique_ptr<constraint::Constraint>> post_constraints_{};
|
|
|
|
|
|
-protected:
|
|
|
- static Version schema_version(std::string_view url);
|
|
|
- static Version schema_version(Adapter auto const & json);
|
|
|
- static Version schema_version(Adapter auto const & json, Version default_version);
|
|
|
-
|
|
|
public:
|
|
|
Node() = default;
|
|
|
Node(std::string const & rejection_reason) : rejects_all_(rejection_reason) {}
|
|
|
@@ -58,47 +54,6 @@ private:
|
|
|
template <Adapter A> detail::OnBlockExit resolve_anchor(detail::ParserContext<A> const & context);
|
|
|
template <Adapter A> bool resolve_reference(detail::ParserContext<A> const & context);
|
|
|
};
|
|
|
-
|
|
|
-inline Version Node::schema_version(std::string_view url) {
|
|
|
- static std::map<std::string_view, Version> const g_schema_ids{
|
|
|
- {"json-schema.org/draft-04/schema", Version::Draft04},
|
|
|
- {"json-schema.org/draft-06/schema", Version::Draft06},
|
|
|
- {"json-schema.org/draft-07/schema", Version::Draft07},
|
|
|
- {"json-schema.org/draft/2019-09/schema", Version::Draft2019_09},
|
|
|
- {"json-schema.org/draft/2020-12/schema", Version::Draft2020_12},
|
|
|
- };
|
|
|
-
|
|
|
- if (url.ends_with('#')) {
|
|
|
- url.remove_suffix(1);
|
|
|
- }
|
|
|
- if (url.starts_with("http://") || url.starts_with("https://")) {
|
|
|
- url.remove_prefix(url.find(':') + 3);
|
|
|
- }
|
|
|
-
|
|
|
- auto it = g_schema_ids.find(url);
|
|
|
- EXPECT_T(it != g_schema_ids.end(), std::invalid_argument, url);
|
|
|
- return it->second;
|
|
|
-}
|
|
|
-
|
|
|
-Version Node::schema_version(Adapter auto const & json) {
|
|
|
- EXPECT(json.type() == adapter::Type::Object);
|
|
|
- EXPECT(json.as_object().contains("$schema"));
|
|
|
-
|
|
|
- auto const & schema = json.as_object()["$schema"];
|
|
|
- EXPECT(schema.type() == adapter::Type::String);
|
|
|
-
|
|
|
- return schema_version(schema.as_string());
|
|
|
-}
|
|
|
-
|
|
|
-Version Node::schema_version(Adapter auto const & json, Version default_version) {
|
|
|
- RETURN_UNLESS(json.type() == adapter::Type::Object, default_version);
|
|
|
- RETURN_UNLESS(json.as_object().contains("$schema"), default_version);
|
|
|
-
|
|
|
- auto const & schema = json.as_object()["$schema"];
|
|
|
- RETURN_UNLESS(schema.type() == adapter::Type::String, default_version);
|
|
|
-
|
|
|
- return schema_version(schema.as_string());
|
|
|
-}
|
|
|
}
|
|
|
|
|
|
namespace jvalidate {
|
|
|
@@ -204,7 +159,7 @@ public:
|
|
|
*/
|
|
|
template <Adapter A, Not<schema::Version>... Args>
|
|
|
explicit Schema(A const & json, Args &&... args)
|
|
|
- : Schema(json, schema_version(json), std::forward<Args>(args)...) {}
|
|
|
+ : Schema(json, detail::version(json), std::forward<Args>(args)...) {}
|
|
|
|
|
|
/**
|
|
|
* @param json Any non-adapter (JSON) object. Will be immedately converted
|
|
|
@@ -334,7 +289,7 @@ template <Adapter A> void Node::construct(detail::ParserContext<A> context) {
|
|
|
// At any point in the schema, we're allowed to change versions
|
|
|
// This means that we're not version-locked to the latest grammar
|
|
|
// (which is especially important for some breaking changes)
|
|
|
- context.version = schema_version(context.schema);
|
|
|
+ context.version = detail::version(context.schema);
|
|
|
}
|
|
|
|
|
|
auto _ = resolve_anchor(context);
|