1#ifndef SOURCEMETA_CORE_JSONSCHEMA_FRAME_H_
2#define SOURCEMETA_CORE_JSONSCHEMA_FRAME_H_
4#ifndef SOURCEMETA_CORE_JSONSCHEMA_EXPORT
5#include <sourcemeta/core/jsonschema_export.h>
9#include <sourcemeta/core/jsonschema_error.h>
10#include <sourcemeta/core/jsonschema_types.h>
13#include <sourcemeta/core/json.h>
14#include <sourcemeta/core/jsonpointer.h>
27namespace sourcemeta::core {
57class SOURCEMETA_CORE_JSONSCHEMA_EXPORT SchemaFrame {
71 enum class Mode : std::uint8_t {
100 [[nodiscard]]
auto mode() const noexcept ->
Mode {
return this->mode_; }
120#pragma GCC diagnostic push
123#pragma GCC diagnostic ignored "-Wshadow"
138#pragma GCC diagnostic pop
144 std::optional<sourcemeta::core::WeakPointer>
parent;
164 using Paths = std::vector<sourcemeta::core::WeakPointer>;
169 const std::optional<sourcemeta::core::PointerPositionTracker> &tracker =
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());
215 -> std::optional<std::reference_wrapper<const Reference>>;
221 const std::string_view
uri)
const
222 -> std::optional<std::reference_wrapper<const Location>>;
244 [[nodiscard]] auto
root() const noexcept
245 -> const sourcemeta::core::
JSON::String &;
250 -> std::optional<std::reference_wrapper<const
Location>>;
255 -> const sourcemeta::core::
JSON &;
269 const sourcemeta::core::
WeakPointer &relative_schema_location =
271 -> sourcemeta::core::
JSON::String;
276 const sourcemeta::core::
WeakPointer &relative_schema_location) const
277 -> std::optional<std::reference_wrapper<const
Location>>;
281 -> std::optional<std::reference_wrapper<const
Location>>;
286 -> std::optional<std::reference_wrapper<const
Location>>;
291 -> std::optional<std::reference_wrapper<const
Location>>;
296 std::reference_wrapper<const sourcemeta::core::
JSON::String>>;
301 const sourcemeta::core::
WeakPointer &relative_schema_location =
304 std::optional<std::reference_wrapper<const
Location>>>;
308 template <std::invocable<const
Location &> F>
310 for (
const auto &entry : this->locations_) {
313 callback(entry.second);
320 template <std::invocable<const Location &> F>
322 const F &callback)
const ->
void {
323 for (
const auto &entry : this->locations_) {
326 entry.second.pointer.size() > pointer.size() &&
327 entry.second.pointer.starts_with(pointer)) {
328 callback(entry.second);
334 template <std::predicate<const Location &> F>
336 for (
const auto &entry : this->locations_) {
339 predicate(entry.second)) {
349 template <std::predicate<const Location &> F>
352 const F &predicate)
const ->
bool {
353 for (
const auto &entry : this->locations_) {
356 entry.second.pointer.size() > pointer.size() &&
357 entry.second.pointer.starts_with(pointer) &&
358 predicate(entry.second)) {
367 template <std::invocable<std::
string_view, const Location &> F>
370 for (
const auto &entry : this->locations_) {
371 if (entry.first.first == type &&
373 callback(entry.first.second, entry.second);
379 template <std::predicate<std::
string_view, const Location &> F>
381 const F &predicate)
const ->
bool {
382 for (
const auto &entry : this->locations_) {
383 if (entry.first.first == type &&
385 predicate(entry.first.second, entry.second)) {
394 template <std::invocable<std::
string_view, const Location &> F>
396 for (
const auto &entry : this->locations_) {
398 callback(entry.first.second, entry.second);
405 std::invocable<SchemaReferenceType, std::string_view, const Location &> F>
407 for (
const auto &entry : this->locations_) {
408 callback(entry.first.first, entry.first.second, entry.second);
414 std::predicate<SchemaReferenceType, std::string_view, const Location &> F>
416 for (
const auto &entry : this->locations_) {
417 if (predicate(entry.first.first, entry.first.second, entry.second)) {
431 for (
const auto &entry : this->references_) {
432 callback(entry.first.first, entry.first.second, entry.second);
442 for (
const auto &entry : this->references_) {
443 if (predicate(entry.first.first, entry.first.second, entry.second)) {
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);
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)) {
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);
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)) {
525 template <std::invocable<std::
string_view> F>
527 for (
const auto &[key,
location] : this->locations_) {
529 callback(key.second);
537 std::invocable<const sourcemeta::core::WeakPointer &, const Reference &>
540 for (
const auto &[key, reference] : this->references_) {
541 if (!this->
traverse(reference.destination).has_value()) {
542 callback(key.second, reference);
581 std::map<std::pair<SchemaReferenceType, sourcemeta::core::WeakPointer>,
590 std::map<std::pair<SchemaReferenceType, sourcemeta::core::JSON::String>,
598#pragma warning(disable : 4251 4275)
605 std::deque<sourcemeta::core::Pointer> reference_pointers_;
606 Locations locations_;
607 References references_;
611 std::unique_ptr<Cache> cache_;
613#pragma warning(default : 4251 4275)
std::basic_string< Char, CharTraits, Allocator< Char > > String
The string type used by the JSON document.
Definition json_value.h:52
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
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