| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- #pragma once
- #include <map>
- #include <jvalidate/detail/expect.h>
- #include <jvalidate/enum.h>
- namespace jvalidate::detail {
- inline schema::Version version(std::string_view url) {
- static std::map<std::string_view, schema::Version> const g_schema_ids{
- {"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},
- };
- 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;
- }
- schema::Version 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 version(schema.as_string());
- }
- schema::Version version(Adapter auto const & json, schema::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 version(schema.as_string());
- }
- }
|