1#ifndef SOURCEMETA_CORE_JSONSCHEMA_VOCABULARIES_H_
2#define SOURCEMETA_CORE_JSONSCHEMA_VOCABULARIES_H_
4#ifndef SOURCEMETA_CORE_JSONSCHEMA_EXPORT
5#include <sourcemeta/core/jsonschema_export.h>
8#include <sourcemeta/core/error.h>
9#include <sourcemeta/core/json.h>
20#include <unordered_map>
21#include <unordered_set>
26namespace sourcemeta::core {
31struct SOURCEMETA_CORE_JSONSCHEMA_EXPORT SchemaVocabularies {
33 enum class Known : std::uint8_t {
36 JSON_SCHEMA_DRAFT_0 = 0,
40 JSON_SCHEMA_DRAFT_1 = 2,
44 JSON_SCHEMA_DRAFT_2 = 4,
65 JSON_SCHEMA_2019_09_CORE = 14,
67 JSON_SCHEMA_2019_09_APPLICATOR = 15,
69 JSON_SCHEMA_2019_09_VALIDATION = 16,
71 JSON_SCHEMA_2019_09_META_DATA = 17,
73 JSON_SCHEMA_2019_09_FORMAT = 18,
75 JSON_SCHEMA_2019_09_CONTENT = 19,
77 JSON_SCHEMA_2019_09_HYPER_SCHEMA = 20,
80 JSON_SCHEMA_2020_12_CORE = 21,
82 JSON_SCHEMA_2020_12_APPLICATOR = 22,
84 JSON_SCHEMA_2020_12_UNEVALUATED = 23,
86 JSON_SCHEMA_2020_12_VALIDATION = 24,
88 JSON_SCHEMA_2020_12_META_DATA = 25,
90 JSON_SCHEMA_2020_12_FORMAT_ANNOTATION = 26,
92 JSON_SCHEMA_2020_12_FORMAT_ASSERTION = 27,
94 JSON_SCHEMA_2020_12_CONTENT = 28,
99 OPENAPI_3_0_BASE = 29,
102 OPENAPI_3_1_BASE = 30,
105 OPENAPI_3_2_BASE = 31,
108 SOURCEMETA_EXTENSION_V1 = 32
120 using URI = std::variant<Known, sourcemeta::core::JSON::String>;
124 using URIView = std::variant<Known, std::string_view>;
127 SchemaVocabularies() =
default;
132 auto operator=(const SchemaVocabularies &) -> SchemaVocabularies & = default;
133 auto operator=(SchemaVocabularies &&) noexcept
134 -> SchemaVocabularies & = default;
135 ~SchemaVocabularies() = default;
139 std::initializer_list<std::pair<sourcemeta::core::
JSON::String,
bool>>
143 SchemaVocabularies(std::initializer_list<std::pair<
Known,
bool>> init);
147 contains(const sourcemeta::core::
JSON::String &uri) const noexcept ->
bool;
158 auto
insert(const sourcemeta::core::
JSON::String &uri,
bool required) noexcept
166 get(const sourcemeta::core::
JSON::String &uri) const noexcept
167 -> std::optional<
bool>;
170 [[nodiscard]] auto
get(
Known vocabulary) const noexcept
171 -> std::optional<
bool>;
174 [[nodiscard]] auto
size() const noexcept -> std::
size_t;
177 [[nodiscard]] auto
empty() const noexcept ->
bool;
183 template <typename Callback>
184 auto
for_each(const Callback &callback) const ->
void {
186 if (this->required_known_[index]) {
187 callback(
URI{
static_cast<Known>(index)},
true);
188 }
else if (this->optional_known_[index]) {
189 callback(
URI{
static_cast<Known>(index)},
false);
193 if (this->unknown_.has_value()) {
194 for (const auto &[uri, required] : this->unknown_.value()) {
195 callback(URI{uri}, required);
210#pragma warning(disable : 4251)
212 std::bitset<KNOWN_VOCABULARY_COUNT> required_known_{};
213 std::bitset<KNOWN_VOCABULARY_COUNT> optional_known_{};
215 std::optional<std::unordered_map<sourcemeta::core::JSON::String, bool>>
216 unknown_{std::nullopt};
223SOURCEMETA_CORE_JSONSCHEMA_EXPORT
auto
224operator<<(std::ostream &stream, SchemaVocabularies::Known vocabulary)
228SOURCEMETA_CORE_JSONSCHEMA_EXPORT
auto
229operator<<(std::ostream &stream,
const SchemaVocabularies::URI &vocabulary)
235template <>
struct std::formatter<sourcemeta::core::SchemaVocabularies::Known> {
236 constexpr auto parse(std::format_parse_context &context)
237 ->
decltype(context.begin()) {
238 return context.begin();
242 std::format_context &context)
const ->
decltype(context.out()) {
243 std::ostringstream stream;
245 return std::format_to(context.out(),
"{}", stream.str());
249template <>
struct std::formatter<sourcemeta::core::SchemaVocabularies::URI> {
250 constexpr auto parse(std::format_parse_context &context)
251 ->
decltype(context.begin()) {
252 return context.begin();
256 std::format_context &context)
const ->
decltype(context.out()) {
257 std::ostringstream stream;
259 return std::format_to(context.out(),
"{}", stream.str());
auto has_unknown() const noexcept -> bool
Check if there are any unknown vocabularies.
Known
Every vocabulary that this implementation recognises out of the box.
Definition jsonschema_vocabularies.h:33
auto throw_if_any_unsupported(const std::unordered_set< URI > &supported, StaticString message) const -> void
auto size() const noexcept -> std::size_t
Get the number of vocabularies (required + optional + custom).
std::variant< Known, sourcemeta::core::JSON::String > URI
Definition jsonschema_vocabularies.h:120
auto contains_any(std::initializer_list< Known > vocabularies) const noexcept -> bool
Check if any of the given known vocabularies are enabled.
auto get(const sourcemeta::core::JSON::String &uri) const noexcept -> std::optional< bool >
Get vocabulary status by URI.
auto empty() const noexcept -> bool
Check if there are no vocabularies.
auto for_each(const Callback &callback) const -> void
Iterate over every vocabulary, along with whether it is required.
Definition jsonschema_vocabularies.h:184
SchemaVocabularies(SchemaVocabularies &&) noexcept=default
Move a vocabulary set.
std::variant< Known, std::string_view > URIView
Definition jsonschema_vocabularies.h:124
auto insert(const sourcemeta::core::JSON::String &uri, bool required) noexcept -> void
Insert a vocabulary with its required/optional status.
static constexpr std::size_t KNOWN_VOCABULARY_COUNT
How many vocabularies this implementation recognises out of the box.
Definition jsonschema_vocabularies.h:116
SchemaVocabularies(const SchemaVocabularies &)=default
Copy a vocabulary set.
auto contains(const sourcemeta::core::JSON::String &uri) const noexcept -> bool
Check if a vocabulary is enabled.
@ JSON_SCHEMA_DRAFT_4
The Draft 4 validation base dialect.
Definition jsonschema_types.h:72
@ JSON_SCHEMA_DRAFT_3
The Draft 3 validation base dialect.
Definition jsonschema_types.h:76
@ JSON_SCHEMA_DRAFT_7
The Draft 7 validation base dialect.
Definition jsonschema_types.h:64
@ JSON_SCHEMA_DRAFT_6
The Draft 6 validation base dialect.
Definition jsonschema_types.h:68
@ JSON_SCHEMA_DRAFT_0_HYPER
The Draft 0 hyper-schema base dialect.
Definition jsonschema_types.h:84
@ JSON_SCHEMA_DRAFT_4_HYPER
The Draft 4 hyper-schema base dialect.
Definition jsonschema_types.h:74
@ JSON_SCHEMA_DRAFT_3_HYPER
The Draft 3 hyper-schema base dialect.
Definition jsonschema_types.h:78
@ JSON_SCHEMA_DRAFT_7_HYPER
The Draft 7 hyper-schema base dialect.
Definition jsonschema_types.h:66
@ JSON_SCHEMA_DRAFT_2_HYPER
The Draft 2 hyper-schema base dialect.
Definition jsonschema_types.h:80
@ JSON_SCHEMA_DRAFT_6_HYPER
The Draft 6 hyper-schema base dialect.
Definition jsonschema_types.h:70
@ JSON_SCHEMA_DRAFT_1_HYPER
The Draft 1 hyper-schema base dialect.
Definition jsonschema_types.h:82
Definition text_static_string.h:20