Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
jsonschema_frame.h
1#ifndef SOURCEMETA_CORE_JSONSCHEMA_FRAME_H_
2#define SOURCEMETA_CORE_JSONSCHEMA_FRAME_H_
3
4#ifndef SOURCEMETA_CORE_JSONSCHEMA_EXPORT
5#include <sourcemeta/core/jsonschema_export.h>
6#endif
7
8// NOLINTBEGIN(misc-include-cleaner)
9#include <sourcemeta/core/jsonschema_error.h>
10#include <sourcemeta/core/jsonschema_types.h>
11// NOLINTEND(misc-include-cleaner)
12
13#include <sourcemeta/core/json.h>
14#include <sourcemeta/core/jsonpointer.h>
15
16#include <concepts> // std::invocable
17#include <cstdint> // std::uint8_t
18#include <deque> // std::deque
19#include <functional> // std::reference_wrapper
20#include <limits> // std::numeric_limits
21#include <map> // std::map
22#include <memory> // std::unique_ptr
23#include <optional> // std::optional
24#include <utility> // std::pair
25#include <vector> // std::vector
26
27namespace sourcemeta::core {
28
57class SOURCEMETA_CORE_JSONSCHEMA_EXPORT SchemaFrame {
58public:
71 enum class Mode : std::uint8_t {
73 Root,
75 Locations,
77 References,
79 Pointers
80 };
81
84 enum class IdentifierMode : std::uint8_t {
86 Additional,
88 Fallback
89 };
90
92
93 // We rely on internal caches that would be dangling otherwise
94 SchemaFrame(const SchemaFrame &) = delete;
95 auto operator=(const SchemaFrame &) -> SchemaFrame & = delete;
96 SchemaFrame(SchemaFrame &&) = delete;
97 auto operator=(SchemaFrame &&) -> SchemaFrame & = delete;
98
100 [[nodiscard]] auto mode() const noexcept -> Mode { return this->mode_; }
101
103 struct Reference {
105 std::string_view original;
106 // TODO: This one is tricky to turn into a view, as there is no
107 // location entry to point to if it is an external unresolved reference
114 std::string_view base;
116 std::optional<std::string_view> fragment;
117 };
118
119#if defined(__GNUC__)
120#pragma GCC diagnostic push
121// GCC believes that a member of an enum class (which is namespaced by
122// definition), can shadow an alias defined even on a different namespace.
123#pragma GCC diagnostic ignored "-Wshadow"
124#endif
127 enum class LocationType : std::uint8_t {
129 Resource,
131 Anchor,
135 Subschema
136 };
137#if defined(__GNUC__)
138#pragma GCC diagnostic pop
139#endif
140
142 struct Location {
144 std::optional<sourcemeta::core::WeakPointer> parent;
148 std::string_view base;
152 std::size_t relative_pointer;
154 std::string_view dialect;
160 bool orphan;
161 };
162
164 using Paths = std::vector<sourcemeta::core::WeakPointer>;
165
167 [[nodiscard]] auto to_json(
168 const SchemaResolver &resolver,
169 const std::optional<sourcemeta::core::PointerPositionTracker> &tracker =
170 std::nullopt) const -> sourcemeta::core::JSON;
171
203 const Mode mode, const sourcemeta::core::JSON &root,
204 const SchemaWalker &walker, const SchemaResolver &resolver,
205 std::string_view default_dialect = "", std::string_view default_id = "",
208 std::string_view default_base = "",
209 std::uint64_t max_locations = std::numeric_limits<std::uint64_t>::max());
210
212 [[nodiscard]] auto
213 reference(const SchemaReferenceType type,
214 const sourcemeta::core::WeakPointer &pointer) const
215 -> std::optional<std::reference_wrapper<const Reference>>;
216
220 [[nodiscard]] auto location(const SchemaReferenceType type,
221 const std::string_view uri) const
222 -> std::optional<std::reference_wrapper<const Location>>;
223
225 [[nodiscard]] auto location_count() const noexcept -> std::size_t;
226
228 [[nodiscard]] auto reference_count() const noexcept -> std::size_t;
229
231 [[nodiscard]] auto has_dynamic_references() const noexcept -> bool;
232
234 [[nodiscard]] auto standalone() const noexcept -> bool;
235
241 [[nodiscard]] auto standalone_ignoring_metaschemas() const noexcept -> bool;
242
244 [[nodiscard]] auto root() const noexcept
245 -> const sourcemeta::core::JSON::String &;
246
249 [[nodiscard]] auto root_location() const
250 -> std::optional<std::reference_wrapper<const Location>>;
251
254 [[nodiscard]] auto metaschema(const SchemaResolver &resolver) const
255 -> const sourcemeta::core::JSON &;
256
262 [[nodiscard]] auto vocabularies(const Location &location,
263 const SchemaResolver &resolver) const
264 -> const SchemaVocabularies &;
265
267 [[nodiscard]] auto
269 const sourcemeta::core::WeakPointer &relative_schema_location =
270 sourcemeta::core::EMPTY_WEAK_POINTER) const
271 -> sourcemeta::core::JSON::String;
272
274 [[nodiscard]] auto
276 const sourcemeta::core::WeakPointer &relative_schema_location) const
277 -> std::optional<std::reference_wrapper<const Location>>;
278
280 [[nodiscard]] auto traverse(const std::string_view uri) const
281 -> std::optional<std::reference_wrapper<const Location>>;
282
284 [[nodiscard]] auto
285 traverse(const sourcemeta::core::WeakPointer &pointer) const
286 -> std::optional<std::reference_wrapper<const Location>>;
287
289 [[nodiscard]] auto traverse(const sourcemeta::core::WeakPointer &pointer,
290 const LocationType type) const
291 -> std::optional<std::reference_wrapper<const Location>>;
292
294 [[nodiscard]] auto uri(const sourcemeta::core::WeakPointer &pointer) const
295 -> std::optional<
296 std::reference_wrapper<const sourcemeta::core::JSON::String>>;
297
299 [[nodiscard]] auto
301 const sourcemeta::core::WeakPointer &relative_schema_location =
302 sourcemeta::core::EMPTY_WEAK_POINTER) const
303 -> std::pair<SchemaReferenceType,
304 std::optional<std::reference_wrapper<const Location>>>;
305
308 template <std::invocable<const Location &> F>
309 auto for_each_subschema(const F &callback) const -> void {
310 for (const auto &entry : this->locations_) {
311 if (entry.second.type == LocationType::Resource ||
312 entry.second.type == LocationType::Subschema) {
313 callback(entry.second);
314 }
315 }
316 }
317
320 template <std::invocable<const Location &> F>
322 const F &callback) const -> void {
323 for (const auto &entry : this->locations_) {
324 if ((entry.second.type == LocationType::Resource ||
325 entry.second.type == LocationType::Subschema) &&
326 entry.second.pointer.size() > pointer.size() &&
327 entry.second.pointer.starts_with(pointer)) {
328 callback(entry.second);
329 }
330 }
331 }
332
334 template <std::predicate<const Location &> F>
335 [[nodiscard]] auto any_subschema(const F &predicate) const -> bool {
336 for (const auto &entry : this->locations_) {
337 if ((entry.second.type == LocationType::Resource ||
338 entry.second.type == LocationType::Subschema) &&
339 predicate(entry.second)) {
340 return true;
341 }
342 }
343
344 return false;
345 }
346
349 template <std::predicate<const Location &> F>
350 [[nodiscard]] auto
352 const F &predicate) const -> bool {
353 for (const auto &entry : this->locations_) {
354 if ((entry.second.type == LocationType::Resource ||
355 entry.second.type == LocationType::Subschema) &&
356 entry.second.pointer.size() > pointer.size() &&
357 entry.second.pointer.starts_with(pointer) &&
358 predicate(entry.second)) {
359 return true;
360 }
361 }
362
363 return false;
364 }
365
367 template <std::invocable<std::string_view, const Location &> F>
368 auto for_each_anchor(const SchemaReferenceType type, const F &callback) const
369 -> void {
370 for (const auto &entry : this->locations_) {
371 if (entry.first.first == type &&
372 entry.second.type == LocationType::Anchor) {
373 callback(entry.first.second, entry.second);
374 }
375 }
376 }
377
379 template <std::predicate<std::string_view, const Location &> F>
380 [[nodiscard]] auto any_anchor(const SchemaReferenceType type,
381 const F &predicate) const -> bool {
382 for (const auto &entry : this->locations_) {
383 if (entry.first.first == type &&
384 entry.second.type == LocationType::Anchor &&
385 predicate(entry.first.second, entry.second)) {
386 return true;
387 }
388 }
389
390 return false;
391 }
392
394 template <std::invocable<std::string_view, const Location &> F>
395 auto for_each_resource(const F &callback) const -> void {
396 for (const auto &entry : this->locations_) {
397 if (entry.second.type == LocationType::Resource) {
398 callback(entry.first.second, entry.second);
399 }
400 }
401 }
402
404 template <
405 std::invocable<SchemaReferenceType, std::string_view, const Location &> F>
406 auto for_each_location(const F &callback) const -> void {
407 for (const auto &entry : this->locations_) {
408 callback(entry.first.first, entry.first.second, entry.second);
409 }
410 }
411
413 template <
414 std::predicate<SchemaReferenceType, std::string_view, const Location &> F>
415 [[nodiscard]] auto any_location(const F &predicate) const -> bool {
416 for (const auto &entry : this->locations_) {
417 if (predicate(entry.first.first, entry.first.second, entry.second)) {
418 return true;
419 }
420 }
421
422 return false;
423 }
424
426 template <
428 const Reference &>
429 F>
430 auto for_each_reference(const F &callback) const -> void {
431 for (const auto &entry : this->references_) {
432 callback(entry.first.first, entry.first.second, entry.second);
433 }
434 }
435
437 template <
439 const Reference &>
440 F>
441 [[nodiscard]] auto any_reference(const F &predicate) const -> bool {
442 for (const auto &entry : this->references_) {
443 if (predicate(entry.first.first, entry.first.second, entry.second)) {
444 return true;
445 }
446 }
447
448 return false;
449 }
450
452 template <
454 const Reference &>
455 F>
457 const F &callback) const -> void {
458 for (const auto &entry : this->references_) {
459 if (entry.first.second.starts_with(pointer)) {
460 callback(entry.first.first, entry.first.second, entry.second);
461 }
462 }
463 }
464
467 template <
469 const Reference &>
470 F>
471 [[nodiscard]] auto
473 const F &predicate) const -> bool {
474 for (const auto &entry : this->references_) {
475 if (entry.first.second.starts_with(pointer) &&
476 predicate(entry.first.first, entry.first.second, entry.second)) {
477 return true;
478 }
479 }
480
481 return false;
482 }
483
488 template <
490 const Reference &>
491 F>
493 const F &callback) const -> void {
494 for (const auto &entry : this->references_) {
495 const auto destination{this->traverse(entry.second.destination)};
496 if (destination.has_value() &&
497 destination.value().get().pointer.starts_with(pointer)) {
498 callback(entry.first.first, entry.first.second, entry.second);
499 }
500 }
501 }
502
505 template <
507 const Reference &>
508 F>
509 [[nodiscard]] auto
511 const F &predicate) const -> bool {
512 for (const auto &entry : this->references_) {
513 const auto destination{this->traverse(entry.second.destination)};
514 if (destination.has_value() &&
515 destination.value().get().pointer.starts_with(pointer) &&
516 predicate(entry.first.first, entry.first.second, entry.second)) {
517 return true;
518 }
519 }
520
521 return false;
522 }
523
525 template <std::invocable<std::string_view> F>
526 auto for_each_resource_uri(const F &callback) const -> void {
527 for (const auto &[key, location] : this->locations_) {
528 if (location.type == LocationType::Resource) {
529 callback(key.second);
530 }
531 }
532 }
533
536 template <
537 std::invocable<const sourcemeta::core::WeakPointer &, const Reference &>
538 F>
539 auto for_each_unresolved_reference(const F &callback) const -> void {
540 for (const auto &[key, reference] : this->references_) {
541 if (!this->traverse(reference.destination).has_value()) {
542 callback(key.second, reference);
543 }
544 }
545 }
546
548 [[nodiscard]] auto
550
552 [[nodiscard]] auto
554 -> bool;
557 [[nodiscard]] auto
559 const sourcemeta::core::WeakPointer::Token &tail) const
560 -> bool;
561
563 [[nodiscard]] auto relative_instance_location(const Location &location) const
565
567 [[nodiscard]] auto is_reachable(const Location &base,
568 const Location &location,
569 const SchemaWalker &walker,
570 const SchemaResolver &resolver) const -> bool;
571
572private:
580 using References =
581 std::map<std::pair<SchemaReferenceType, sourcemeta::core::WeakPointer>,
582 Reference>;
583
584 using Locations =
585 // While it might seem weird that we namespace the location URIs with a
586 // reference type, it is essential for distinguishing schema resource URIs
587 // from `$recursiveRef: true` on another place of the schema schema
588 // resource, as otherwise they would both have the exact same URI, but
589 // point to different places.
590 std::map<std::pair<SchemaReferenceType, sourcemeta::core::JSON::String>,
591 Location>;
592
593 Mode mode_;
594// Exporting symbols that depends on the standard C++ library is considered
595// safe.
596// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
597#if defined(_MSC_VER)
598#pragma warning(disable : 4251 4275)
599#endif
601 // A reference may target a place that no schema location covers, in which
602 // case framing materialises one for it. Unlike every other location, the
603 // tokens of those pointers are not borrowed from the analysed document, so
604 // they have to be declared here to outlive the locations that borrow them
605 std::deque<sourcemeta::core::Pointer> reference_pointers_;
606 Locations locations_;
607 References references_;
608 // What the frame derives rather than is, kept out of line so that this
609 // declaration stays down to the schema it framed
610 struct Cache;
611 std::unique_ptr<Cache> cache_;
612#if defined(_MSC_VER)
613#pragma warning(default : 4251 4275)
614#endif
615};
616
617} // namespace sourcemeta::core
618
619#endif
std::basic_string< Char, CharTraits, Allocator< Char > > String
The string type used by the JSON document.
Definition json_value.h:52
Definition json_value.h:39
GenericToken< std::reference_wrapper< const std::string >, PropertyHashJSON< JSON::String > > Token
Definition jsonpointer_pointer.h:28
const WeakPointer EMPTY_WEAK_POINTER
Definition jsonpointer.h:56
GenericPointer< JSON::String, PropertyHashJSON< JSON::String > > Pointer
Definition jsonpointer.h:41
GenericPointer< std::reference_wrapper< const std::string >, PropertyHashJSON< JSON::String > > WeakPointer
Definition jsonpointer.h:46
std::vector< sourcemeta::core::WeakPointer > Paths
A list of paths to frame within a schema wrapper.
Definition jsonschema_frame.h:164
auto for_each_resource(const F &callback) const -> void
Iterate over every schema resource, along with its URI.
Definition jsonschema_frame.h:395
auto for_each_subschema_under(const sourcemeta::core::WeakPointer &pointer, const F &callback) const -> void
Definition jsonschema_frame.h:321
auto for_each_anchor(const SchemaReferenceType type, const F &callback) const -> void
Iterate over every anchor of the given kind, along with its URI.
Definition jsonschema_frame.h:368
auto vocabularies(const Location &location, const SchemaResolver &resolver) const -> const SchemaVocabularies &
auto for_each_reference(const F &callback) const -> void
Iterate over every reference, along with the pointer it originates from.
Definition jsonschema_frame.h:430
auto traverse(const Location &location, const sourcemeta::core::WeakPointer &relative_schema_location) const -> std::optional< std::reference_wrapper< const Location > >
Get the location associated by traversing a pointer from another location.
auto any_reference_from(const sourcemeta::core::WeakPointer &pointer, const F &predicate) const -> bool
Definition jsonschema_frame.h:472
auto standalone() const noexcept -> bool
Check whether the analysed schema has no external references.
auto metaschema(const SchemaResolver &resolver) const -> const sourcemeta::core::JSON &
auto for_each_location(const F &callback) const -> void
Iterate over every location, whatever kind it is.
Definition jsonschema_frame.h:406
auto any_anchor(const SchemaReferenceType type, const F &predicate) const -> bool
Check whether any anchor of the given kind satisfies the predicate.
Definition jsonschema_frame.h:380
auto standalone_ignoring_metaschemas() const noexcept -> bool
auto reference_count() const noexcept -> std::size_t
The number of references in the frame.
auto any_reference_into(const sourcemeta::core::WeakPointer &pointer, const F &predicate) const -> bool
Definition jsonschema_frame.h:510
auto has_references_through(const sourcemeta::core::WeakPointer &pointer, const sourcemeta::core::WeakPointer::Token &tail) const -> bool
auto to_json(const SchemaResolver &resolver, const std::optional< sourcemeta::core::PointerPositionTracker > &tracker=std::nullopt) const -> sourcemeta::core::JSON
Export the frame as JSON.
IdentifierMode
Definition jsonschema_frame.h:84
@ Additional
Register the default identifier in addition to the schema's own.
Definition jsonschema_frame.h:86
auto for_each_reference_from(const sourcemeta::core::WeakPointer &pointer, const F &callback) const -> void
Iterate over every reference that originates at or below the given pointer.
Definition jsonschema_frame.h:456
auto has_dynamic_references() const noexcept -> bool
Check whether the analysed schema makes use of dynamic referencing at all.
auto dereference(const Location &location, const sourcemeta::core::WeakPointer &relative_schema_location=sourcemeta::core::EMPTY_WEAK_POINTER) const -> std::pair< SchemaReferenceType, std::optional< std::reference_wrapper< const Location > > >
Try to dereference a reference location into its destination location.
auto is_reachable(const Location &base, const Location &location, const SchemaWalker &walker, const SchemaResolver &resolver) const -> bool
Determines if a location could be evaluated during validation.
auto any_reference(const F &predicate) const -> bool
Check whether any reference satisfies the predicate.
Definition jsonschema_frame.h:441
SchemaFrame(const Mode mode, const sourcemeta::core::JSON &root, const SchemaWalker &walker, const SchemaResolver &resolver, std::string_view default_dialect="", std::string_view default_id="", IdentifierMode identifier_mode=IdentifierMode::Additional, const Paths &paths={sourcemeta::core::EMPTY_WEAK_POINTER}, std::string_view default_base="", std::uint64_t max_locations=std::numeric_limits< std::uint64_t >::max())
auto any_location(const F &predicate) const -> bool
Check whether any location satisfies the predicate.
Definition jsonschema_frame.h:415
Mode
Definition jsonschema_frame.h:71
auto mode() const noexcept -> Mode
Query the current mode that the schema frame was configured with.
Definition jsonschema_frame.h:100
auto for_each_subschema(const F &callback) const -> void
Definition jsonschema_frame.h:309
auto has_references_through(const sourcemeta::core::WeakPointer &pointer) const -> bool
Check if there are any references that go through a given location pointer.
auto any_subschema_under(const sourcemeta::core::WeakPointer &pointer, const F &predicate) const -> bool
Definition jsonschema_frame.h:351
auto for_each_unresolved_reference(const F &callback) const -> void
Definition jsonschema_frame.h:539
auto has_references_to(const sourcemeta::core::WeakPointer &pointer) const -> bool
Check if there are any references to a given location pointer.
auto root_location() const -> std::optional< std::reference_wrapper< const Location > >
auto any_subschema(const F &predicate) const -> bool
Check whether any schema resource or subschema satisfies the predicate.
Definition jsonschema_frame.h:335
auto uri(const Location &location, const sourcemeta::core::WeakPointer &relative_schema_location=sourcemeta::core::EMPTY_WEAK_POINTER) const -> sourcemeta::core::JSON::String
Get the URI associated with a location entry.
auto location(const SchemaReferenceType type, const std::string_view uri) const -> std::optional< std::reference_wrapper< const Location > >
auto relative_instance_location(const Location &location) const -> sourcemeta::core::WeakPointer
Get the relative instance location pointer for a given location entry.
auto root() const noexcept -> const sourcemeta::core::JSON::String &
Get the root schema identifier (empty if none).
auto for_each_reference_into(const sourcemeta::core::WeakPointer &pointer, const F &callback) const -> void
Definition jsonschema_frame.h:492
auto location_count() const noexcept -> std::size_t
The number of locations in the frame.
auto for_each_resource_uri(const F &callback) const -> void
Iterate over all resource URIs in the frame.
Definition jsonschema_frame.h:526
Definition jsonschema_frame.h:57
LocationType
Definition jsonschema_frame.h:127
SchemaReferenceType
Definition jsonschema_types.h:45
std::function< SchemaResolverResult(std::string_view)> SchemaResolver
Definition jsonschema_types.h:41
std::function< const SchemaWalkerResult &( std::string_view, const SchemaVocabularies &)> SchemaWalker
Definition jsonschema_types.h:254
SchemaBaseDialect
Definition jsonschema_types.h:54
@ Subschema
A subschema that declares no identifier of its own.
Definition jsonschema_frame.h:135
@ Anchor
An anchor declared within a schema resource.
Definition jsonschema_frame.h:131
@ Resource
A schema resource, as introduced by an identifier.
Definition jsonschema_frame.h:129
@ Reference
The JSON Schema keyword is a reference.
Definition jsonschema_types.h:110
Definition jsonschema_vocabularies.h:31
A location entry.
Definition jsonschema_frame.h:142
std::size_t relative_pointer
How many tokens of the pointer fall inside the enclosing resource.
Definition jsonschema_frame.h:152
std::optional< sourcemeta::core::WeakPointer > parent
The location of the closest enclosing schema resource, if any.
Definition jsonschema_frame.h:144
std::string_view dialect
The meta-schema in effect at this entry.
Definition jsonschema_frame.h:154
std::string_view base
The base URI that this entry resolves relative references against.
Definition jsonschema_frame.h:148
bool orphan
Whether nothing in the document reaches this entry.
Definition jsonschema_frame.h:160
bool property_name
Whether this entry sits under a keyword that applies to property names.
Definition jsonschema_frame.h:158
LocationType type
What kind of place in the document this entry describes.
Definition jsonschema_frame.h:146
SchemaBaseDialect base_dialect
The base dialect in effect at this entry.
Definition jsonschema_frame.h:156
A reference that the schema declares, as found by framing.
Definition jsonschema_frame.h:103
std::string_view original
The reference exactly as the schema spells it.
Definition jsonschema_frame.h:105
sourcemeta::core::JSON::String destination
The absolute URI that the reference resolves to.
Definition jsonschema_frame.h:109
std::optional< std::string_view > fragment
The fragment of the destination, if it declares one.
Definition jsonschema_frame.h:116
std::string_view base
Definition jsonschema_frame.h:114