Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
jsonschema_vocabularies.h
1#ifndef SOURCEMETA_CORE_JSONSCHEMA_VOCABULARIES_H_
2#define SOURCEMETA_CORE_JSONSCHEMA_VOCABULARIES_H_
3
4#ifndef SOURCEMETA_CORE_JSONSCHEMA_EXPORT
5#include <sourcemeta/core/jsonschema_export.h>
6#endif
7
8#include <sourcemeta/core/error.h>
9#include <sourcemeta/core/json.h>
10
11#include <bitset> // std::bitset
12#include <cassert> // assert
13#include <cstdint> // std::uint32_t, std::size_t
14#include <format> // std::formatter, std::format_context, std::format_parse_context, std::format_to
15#include <optional> // std::optional
16#include <ostream> // std::ostream
17#include <sstream> // std::ostringstream
18#include <stdexcept> // std::out_of_range
19#include <string_view> // std::string_view
20#include <unordered_map> // std::unordered_map
21#include <unordered_set> // std::unordered_set
22#include <utility> // std::pair
23#include <variant> // std::variant
24#include <vector> // std::vector
25
26namespace sourcemeta::core {
27
31struct SOURCEMETA_CORE_JSONSCHEMA_EXPORT SchemaVocabularies {
33 enum class Known : std::uint8_t {
34 // Pre-vocabulary dialects (treated as vocabularies)
36 JSON_SCHEMA_DRAFT_0 = 0,
40 JSON_SCHEMA_DRAFT_1 = 2,
44 JSON_SCHEMA_DRAFT_2 = 4,
63 // 2019-09 vocabularies
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,
78 // 2020-12 vocabularies
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,
95 // OpenAPI
96 // The OpenAPI Initiative never published a vocabulary for the Schema
97 // Object of this revision, so this one is of our own making
99 OPENAPI_3_0_BASE = 29,
100 // https://spec.openapis.org/oas/v3.1.0.html#fixed-fields-19
102 OPENAPI_3_1_BASE = 30,
103 // https://spec.openapis.org/oas/v3.2.0.html#base-vocabulary
105 OPENAPI_3_2_BASE = 31,
106 // Sourcemeta
108 SOURCEMETA_EXTENSION_V1 = 32
109 };
110
111 // Membership is a bit per vocabulary, indexed by the value of the
112 // enumerator, so one that falls outside this bound reads and writes past the
113 // end of the sets below. The implementation file asserts that none does,
114 // over the same list the compiler already holds it to
116 static constexpr std::size_t KNOWN_VOCABULARY_COUNT = 33;
117
120 using URI = std::variant<Known, sourcemeta::core::JSON::String>;
121
124 using URIView = std::variant<Known, std::string_view>;
125
126public:
127 SchemaVocabularies() = default;
129 SchemaVocabularies(const SchemaVocabularies &) = default;
131 SchemaVocabularies(SchemaVocabularies &&) noexcept = default;
132 auto operator=(const SchemaVocabularies &) -> SchemaVocabularies & = default;
133 auto operator=(SchemaVocabularies &&) noexcept
134 -> SchemaVocabularies & = default;
135 ~SchemaVocabularies() = default;
136
138 SchemaVocabularies(
139 std::initializer_list<std::pair<sourcemeta::core::JSON::String, bool>>
140 init);
141
143 SchemaVocabularies(std::initializer_list<std::pair<Known, bool>> init);
144
146 [[nodiscard]] auto
147 contains(const sourcemeta::core::JSON::String &uri) const noexcept -> bool;
148
150 [[nodiscard]] auto contains(Known vocabulary) const noexcept -> bool;
151
153 [[nodiscard]] auto
154 contains_any(std::initializer_list<Known> vocabularies) const noexcept
155 -> bool;
156
158 auto insert(const sourcemeta::core::JSON::String &uri, bool required) noexcept
159 -> void;
160
162 auto insert(Known vocabulary, bool required) noexcept -> void;
163
165 [[nodiscard]] auto
166 get(const sourcemeta::core::JSON::String &uri) const noexcept
167 -> std::optional<bool>;
168
170 [[nodiscard]] auto get(Known vocabulary) const noexcept
171 -> std::optional<bool>;
172
174 [[nodiscard]] auto size() const noexcept -> std::size_t;
175
177 [[nodiscard]] auto empty() const noexcept -> bool;
178
180 [[nodiscard]] auto has_unknown() const noexcept -> bool;
181
183 template <typename Callback>
184 auto for_each(const Callback &callback) const -> void {
185 for (std::size_t index = 0; index < KNOWN_VOCABULARY_COUNT; ++index) {
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);
190 }
191 }
192
193 if (this->unknown_.has_value()) {
194 for (const auto &[uri, required] : this->unknown_.value()) {
195 callback(URI{uri}, required);
196 }
197 }
198 }
199
202 auto throw_if_any_unsupported(const std::unordered_set<URI> &supported,
203 StaticString message) const -> void;
204
205private:
206 // Invariant: required_known and optional_known must be mutually exclusive
207 // A vocabulary can be either required (true) OR optional (false), never both
208#ifdef _MSC_VER
209#pragma warning(push)
210#pragma warning(disable : 4251)
211#endif
212 std::bitset<KNOWN_VOCABULARY_COUNT> required_known_{};
213 std::bitset<KNOWN_VOCABULARY_COUNT> optional_known_{};
214 // Lazily initialized only when unknown (non-official) vocabularies are used
215 std::optional<std::unordered_map<sourcemeta::core::JSON::String, bool>>
216 unknown_{std::nullopt};
217#ifdef _MSC_VER
218#pragma warning(pop)
219#endif
220};
221
223SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto
224operator<<(std::ostream &stream, SchemaVocabularies::Known vocabulary)
225 -> std::ostream &;
226
228SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto
229operator<<(std::ostream &stream, const SchemaVocabularies::URI &vocabulary)
230 -> std::ostream &;
231
232} // namespace sourcemeta::core
233
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();
239 }
240
241 auto format(const sourcemeta::core::SchemaVocabularies::Known value,
242 std::format_context &context) const -> decltype(context.out()) {
243 std::ostringstream stream;
244 stream << value;
245 return std::format_to(context.out(), "{}", stream.str());
246 }
247};
248
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();
253 }
254
255 auto format(const sourcemeta::core::SchemaVocabularies::URI &value,
256 std::format_context &context) const -> decltype(context.out()) {
257 std::ostringstream stream;
258 stream << value;
259 return std::format_to(context.out(), "{}", stream.str());
260 }
261};
263
264#endif
Definition json_value.h:39
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