Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
JSON Schema

The foundational utilities for operating on JSON Schema documents across dialects. More...

Classes

struct  sourcemeta::core::SchemaBundleOptions
class  sourcemeta::core::SchemaError
class  sourcemeta::core::SchemaResolutionError
class  sourcemeta::core::SchemaRelativeMetaschemaResolutionError
class  sourcemeta::core::SchemaVocabularyError
class  sourcemeta::core::SchemaReferenceError
class  sourcemeta::core::SchemaUnknownDialectError
class  sourcemeta::core::SchemaUnknownBaseDialectError
class  sourcemeta::core::SchemaReferenceObjectResourceError
class  sourcemeta::core::SchemaKeywordError
class  sourcemeta::core::SchemaFrameError
class  sourcemeta::core::SchemaFrameLimitError
class  sourcemeta::core::SchemaAnchorCollisionError
class  sourcemeta::core::SchemaFrame
struct  sourcemeta::core::SchemaWalkerResult
struct  sourcemeta::core::SchemaVocabularies

Typedefs

using sourcemeta::core::SchemaResolverResult
using sourcemeta::core::SchemaResolver = std::function<SchemaResolverResult(std::string_view)>
using sourcemeta::core::SchemaWalker

Enumerations

enum class  sourcemeta::core::SchemaFrame::LocationType : std::uint8_t { sourcemeta::core::SchemaFrame::LocationType::Resource , sourcemeta::core::SchemaFrame::LocationType::Anchor , sourcemeta::core::SchemaFrame::LocationType::Pointer , sourcemeta::core::SchemaFrame::LocationType::Subschema }
enum class  sourcemeta::core::SchemaReferenceType : std::uint8_t { SchemaReferenceType::Static , SchemaReferenceType::Dynamic }
enum class  sourcemeta::core::SchemaBaseDialect : std::uint8_t {
  SchemaBaseDialect::JSON_SCHEMA_2020_12 , SchemaBaseDialect::JSON_SCHEMA_2020_12_HYPER , SchemaBaseDialect::JSON_SCHEMA_2019_09 , SchemaBaseDialect::JSON_SCHEMA_2019_09_HYPER ,
  SchemaBaseDialect::JSON_SCHEMA_DRAFT_7 , SchemaBaseDialect::JSON_SCHEMA_DRAFT_7_HYPER , SchemaBaseDialect::JSON_SCHEMA_DRAFT_6 , SchemaBaseDialect::JSON_SCHEMA_DRAFT_6_HYPER ,
  SchemaBaseDialect::JSON_SCHEMA_DRAFT_4 , SchemaBaseDialect::JSON_SCHEMA_DRAFT_4_HYPER , SchemaBaseDialect::JSON_SCHEMA_DRAFT_3 , SchemaBaseDialect::JSON_SCHEMA_DRAFT_3_HYPER ,
  SchemaBaseDialect::JSON_SCHEMA_DRAFT_2_HYPER , SchemaBaseDialect::JSON_SCHEMA_DRAFT_1_HYPER , SchemaBaseDialect::JSON_SCHEMA_DRAFT_0_HYPER
}
enum class  sourcemeta::core::SchemaKeywordType : std::uint8_t {
  SchemaKeywordType::Unknown , SchemaKeywordType::Assertion , SchemaKeywordType::Annotation , SchemaKeywordType::Reference ,
  SchemaKeywordType::Other , SchemaKeywordType::Comment , SchemaKeywordType::LocationMembers , SchemaKeywordType::ApplicatorMembersTraversePropertyStatic ,
  SchemaKeywordType::ApplicatorMembersTraversePropertyRegex , SchemaKeywordType::ApplicatorValueTraverseSomeProperty , SchemaKeywordType::ApplicatorValueTraverseAnyPropertyKey , SchemaKeywordType::ApplicatorValueTraverseAnyItem ,
  SchemaKeywordType::ApplicatorValueTraverseSomeItem , SchemaKeywordType::ApplicatorValueTraverseParent , SchemaKeywordType::ApplicatorElementsTraverseItem , SchemaKeywordType::ApplicatorValueOrElementsTraverseAnyItemOrItem ,
  SchemaKeywordType::ApplicatorValueOrElementsInPlace , SchemaKeywordType::ApplicatorMembersInPlaceSome , SchemaKeywordType::ApplicatorElementsInPlace , SchemaKeywordType::ApplicatorElementsInPlaceSome ,
  SchemaKeywordType::ApplicatorElementsInPlaceSomeNegate , SchemaKeywordType::ApplicatorValueInPlaceMaybe , SchemaKeywordType::ApplicatorValueInPlaceOther , SchemaKeywordType::ApplicatorValueInPlaceNegate
}

Functions

SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_resolver (const std::string_view identifier) -> SchemaResolverResult
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_is_known (const std::string_view identifier) noexcept -> bool
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_is_official (const std::string_view identifier) noexcept -> bool
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_walker (const std::string_view keyword, const SchemaVocabularies &vocabularies) -> const SchemaWalkerResult &
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_reidentify (sourcemeta::core::JSON &schema, std::string_view new_identifier, const SchemaResolver &resolver, std::string_view default_dialect="") -> void
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_identifier_keyword (const SchemaBaseDialect base_dialect) -> std::string_view
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_reidentify (sourcemeta::core::JSON &schema, std::string_view new_identifier, const SchemaBaseDialect base_dialect) -> void
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_format (sourcemeta::core::JSON &schema, const SchemaFrame &frame) -> void
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_bundle (sourcemeta::core::JSON &schema, const SchemaWalker &walker, const SchemaResolver &resolver, std::string_view default_dialect="", std::string_view default_id="", const SchemaBundleOptions &options={}) -> void
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_bundle (const sourcemeta::core::JSON &schema, const SchemaWalker &walker, const SchemaResolver &resolver, std::string_view default_dialect="", std::string_view default_id="", const SchemaBundleOptions &options={}) -> sourcemeta::core::JSON

Detailed Description

The foundational utilities for operating on JSON Schema documents across dialects.

This module is not a JSON Schema evaluator and does not aim to become one. It offers the building blocks that any operation on a schema needs, independently of what that operation is: identification, dialect and vocabulary detection, resolution of remote schemas, keyword classification across dialects, framing a schema into the locations and references it declares, and bundling a schema into a self-contained document. Evaluation is only one of the consumers of these utilities, alongside linting, transformation, code generation, and documentation tooling.

This functionality is included as follows:

#include <sourcemeta/core/jsonschema.h>

Class Documentation

◆ sourcemeta::core::SchemaBundleOptions

struct sourcemeta::core::SchemaBundleOptions

Everything bundling takes beyond the schema and how to read it

Public Types

enum class  Mode : std::uint8_t { NonOfficialMetaschemas , References }
 The strategies that the bundling process can follow. More...
using Callback

Public Attributes

Mode mode {Mode::NonOfficialMetaschemas}
 The strategy to follow.
std::optional< sourcemeta::core::Pointer > default_container
 Where to embed what bundling pulls in.
SchemaFrame::Paths paths {sourcemeta::core::EMPTY_WEAK_POINTER}
 The paths to bundle within a schema wrapper.
std::string_view default_base
std::uint64_t max_locations {std::numeric_limits<std::uint64_t>::max()}
Callback callback

Member Typedef Documentation

◆ Callback

Initial value:
std::function<void(std::string_view,
GenericPointer< std::reference_wrapper< const std::string >, PropertyHashJSON< JSON::String > > WeakPointer
Definition jsonpointer.h:44

A callback to report which schema got embedded and where, as the identifier that schema answers to and a pointer from the root of the schema being bundled. A schema that declares one of its own answers to that rather than to whichever URI it was resolved by, which are not always the same. The two are given separately because bundling picks a key that is free rather than one that matches, so the last token of that pointer is not always the identifier either

Member Enumeration Documentation

◆ Mode

enum class sourcemeta::core::SchemaBundleOptions::Mode : std::uint8_t
strong

The strategies that the bundling process can follow.

Enumerator
NonOfficialMetaschemas 

Embed every external reference, including any non-official meta-schemas that the schema or its dependencies declare, along with the dependencies of those meta-schemas

References 

Embed every external reference, skipping meta-schema declarations entirely

Member Data Documentation

◆ callback

Callback sourcemeta::core::SchemaBundleOptions::callback

A callback to report where each schema got embedded, which is the only way to know what a later call has to frame when bundling into a container that the dialect does not otherwise traverse

◆ default_base

std::string_view sourcemeta::core::SchemaBundleOptions::default_base

The base URI that the document was retrieved from, which a relative reference within any of the given paths resolves against. As with sourcemeta::core::SchemaFrame, this does not claim that the document declares an identifier, so bundling never writes it into the document

◆ max_locations

std::uint64_t sourcemeta::core::SchemaBundleOptions::max_locations {std::numeric_limits<std::uint64_t>::max()}

The maximum number of frame locations that analysis may register. How many schemas bundling ends up embedding follows from what the resolver hands back rather than from the schema the caller passed in, and every frame that bundling constructs spends from this one limit, throwing sourcemeta::core::SchemaFrameLimitError once it runs out. Note that a remote is copied out of the resolver before anything charges for it, so this bounds how many oversized schemas get copied rather than whether one does

◆ sourcemeta::core::SchemaError

class sourcemeta::core::SchemaError

An error that represents a general schema error event

Inheritance diagram for sourcemeta::core::SchemaError:

Public Member Functions

 SchemaError (const char *message)
 Create a general schema error.
 SchemaError (std::string message)=delete
 SchemaError (std::string &&message)=delete
 SchemaError (std::string_view message)=delete

◆ sourcemeta::core::SchemaResolutionError

class sourcemeta::core::SchemaResolutionError

An error that represents a schema resolution failure event

Inheritance diagram for sourcemeta::core::SchemaResolutionError:
sourcemeta::core::SchemaRelativeMetaschemaResolutionError

Public Member Functions

 SchemaResolutionError (const std::string_view identifier, const char *message)
 Create a schema resolution error.
auto identifier () const noexcept -> std::string_view
 The identifier that could not be resolved.

◆ sourcemeta::core::SchemaRelativeMetaschemaResolutionError

class sourcemeta::core::SchemaRelativeMetaschemaResolutionError

An error that represents a relative meta-schema resolution failure event Relative references to meta-schemas are invalid as per the specification See https://json-schema.org/draft/2020-12/json-schema-core#section-8.1.1-2

Inheritance diagram for sourcemeta::core::SchemaRelativeMetaschemaResolutionError:
sourcemeta::core::SchemaResolutionError

Public Member Functions

 SchemaRelativeMetaschemaResolutionError (const std::string_view identifier)
 Create a relative meta-schema resolution error.
Public Member Functions inherited from sourcemeta::core::SchemaResolutionError
 SchemaResolutionError (const std::string_view identifier, const char *message)
 Create a schema resolution error.
auto identifier () const noexcept -> std::string_view
 The identifier that could not be resolved.

◆ sourcemeta::core::SchemaVocabularyError

class sourcemeta::core::SchemaVocabularyError

An error that represents a schema vocabulary error

Inheritance diagram for sourcemeta::core::SchemaVocabularyError:

Public Member Functions

 SchemaVocabularyError (const std::string_view uri, const char *message)
 Create a vocabulary error.
auto uri () const noexcept -> std::string_view
 The vocabulary that caused the error.

◆ sourcemeta::core::SchemaReferenceError

class sourcemeta::core::SchemaReferenceError

An error that represents a schema resolution failure event

Inheritance diagram for sourcemeta::core::SchemaReferenceError:

Public Member Functions

 SchemaReferenceError (const std::string_view identifier, sourcemeta::core::Pointer schema_location, const char *message)
 Create a reference error.
auto identifier () const noexcept -> std::string_view
 The reference that caused the error.
auto location () const noexcept -> const sourcemeta::core::Pointer &

Member Function Documentation

◆ location()

auto sourcemeta::core::SchemaReferenceError::location ( ) const -> const sourcemeta::core::Pointer &
inlinenodiscardnoexcept

Where the problem is, as a pointer from the root of the document that the schema came from

◆ sourcemeta::core::SchemaUnknownDialectError

class sourcemeta::core::SchemaUnknownDialectError

An error that represents that the dialect of the schema could not determined

Inheritance diagram for sourcemeta::core::SchemaUnknownDialectError:

◆ sourcemeta::core::SchemaUnknownBaseDialectError

class sourcemeta::core::SchemaUnknownBaseDialectError

An error that represents that the base dialect of the schema could not determined

Inheritance diagram for sourcemeta::core::SchemaUnknownBaseDialectError:

◆ sourcemeta::core::SchemaReferenceObjectResourceError

class sourcemeta::core::SchemaReferenceObjectResourceError

In JSON Schema Draft 7 and older, a schema that defines $ref is a reference object where every other keywords are ignored

Inheritance diagram for sourcemeta::core::SchemaReferenceObjectResourceError:

Public Member Functions

 SchemaReferenceObjectResourceError (const std::string_view identifier)
 Create a reference object resource error.
auto identifier () const noexcept -> std::string_view
 The reference that caused the error.

◆ sourcemeta::core::SchemaKeywordError

class sourcemeta::core::SchemaKeywordError

An error that represents a schema keyword error

Inheritance diagram for sourcemeta::core::SchemaKeywordError:

Public Member Functions

 SchemaKeywordError (const std::string_view keyword, const std::string_view value, const char *message)
 Create a keyword error.
auto value () const noexcept -> std::string_view
 The value that caused the error.
auto keyword () const noexcept -> std::string_view
 The keyword that caused the error.

◆ sourcemeta::core::SchemaFrameError

class sourcemeta::core::SchemaFrameError

An error that represents a schema frame error

Inheritance diagram for sourcemeta::core::SchemaFrameError:

Public Member Functions

 SchemaFrameError (const std::string_view identifier, const char *message)
 Create a framing error.
auto identifier () const noexcept -> std::string_view
 The identifier of the schema that caused the error.

◆ sourcemeta::core::SchemaFrameLimitError

class sourcemeta::core::SchemaFrameLimitError

An error that represents a schema that needs more frame locations than the caller was willing to spend on analysing it

Inheritance diagram for sourcemeta::core::SchemaFrameLimitError:

Public Member Functions

 SchemaFrameLimitError (const std::uint64_t limit)
 Create a framing limit error.
auto limit () const noexcept -> std::uint64_t
 The maximum number of locations that framing was allowed to register.

◆ sourcemeta::core::SchemaAnchorCollisionError

class sourcemeta::core::SchemaAnchorCollisionError

An error that represents a schema anchor collision error

Inheritance diagram for sourcemeta::core::SchemaAnchorCollisionError:

Public Member Functions

 SchemaAnchorCollisionError (const std::string_view identifier, sourcemeta::core::Pointer location, sourcemeta::core::Pointer other)
 Create an anchor collision error.
auto identifier () const noexcept -> std::string_view
 The anchor that collided.
auto location () const noexcept -> const sourcemeta::core::Pointer &
auto other () const noexcept -> const sourcemeta::core::Pointer &
 The other place that declares the same anchor.

Member Function Documentation

◆ location()

auto sourcemeta::core::SchemaAnchorCollisionError::location ( ) const -> const sourcemeta::core::Pointer &
inlinenodiscardnoexcept

Where the problem is, as a pointer from the root of the document that the schema came from

◆ sourcemeta::core::SchemaFrame

class sourcemeta::core::SchemaFrame

This class performs a static analysis pass on the input schema, computing things such as the static identifiers and references of a schema.

For example:

#include <sourcemeta/core/json.h>
#include <sourcemeta/core/jsonschema.h>
#include <cassert>
const sourcemeta::core::JSON document =
"$id": "https://www.example.com/schema",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"items": { "$id": "foo", "type": "string" },
"properties": {
"foo": { "$anchor": "test", "type": "number" },
"bar": { "$ref": "#/properties/foo" }
}
})JSON");
Definition json_value.h:39
SOURCEMETA_CORE_JSON_EXPORT auto parse_json(std::basic_istream< JSON::Char, JSON::CharTraits > &stream) -> JSON
@ References
Also report on whatever place every reference names.
Definition jsonschema_frame.h:77
Definition jsonschema_frame.h:57
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto schema_resolver(const std::string_view identifier) -> SchemaResolverResult
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto schema_walker(const std::string_view keyword, const SchemaVocabularies &vocabularies) -> const SchemaWalkerResult &

Public Types

enum class  Mode : std::uint8_t { Root , Locations , References , Pointers }
enum class  IdentifierMode : std::uint8_t { Additional , Fallback }
enum class  LocationType : std::uint8_t { LocationType::Resource , LocationType::Anchor , LocationType::Pointer , LocationType::Subschema }
using Paths = std::vector<sourcemeta::core::WeakPointer>
 A list of paths to frame within a schema wrapper.

Public Member Functions

 SchemaFrame (const SchemaFrame &)=delete
 SchemaFrame (SchemaFrame &&)=delete
auto mode () const noexcept -> Mode
 Query the current mode that the schema frame was configured with.
auto to_json (const SchemaResolver &resolver, const std::optional< sourcemeta::core::PointerPositionTracker > &tracker=std::nullopt) const -> sourcemeta::core::JSON
 Export the frame as JSON.
 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 location (const SchemaReferenceType type, const std::string_view uri) const -> std::optional< std::reference_wrapper< const Location > >
 Get a specific location entry by reference type and URI.
auto location_count () const noexcept -> std::size_t
 The number of locations in the frame.
auto reference_count () const noexcept -> std::size_t
 The number of references in the frame.
auto has_dynamic_references () const noexcept -> bool
 Check whether the analysed schema makes use of dynamic referencing at all.
auto standalone () const noexcept -> bool
 Check whether the analysed schema has no external references.
auto root () const noexcept -> const sourcemeta::core::JSON::String &
 Get the root schema identifier (empty if none).
auto root_location () const -> std::optional< std::reference_wrapper< const Location > >
auto metaschema (const SchemaResolver &resolver) const -> const sourcemeta::core::JSON &
auto vocabularies (const Location &location, const SchemaResolver &resolver) const -> const SchemaVocabularies &
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 traverse (const Location &location, const sourcemeta::core::WeakPointer &relative_schema_location) const -> const Location &
 Get the location associated by traversing a pointer from another location.
auto traverse (const std::string_view uri) const -> std::optional< std::reference_wrapper< const Location > >
 Get the location associated with a given URI.
auto traverse (const sourcemeta::core::WeakPointer &pointer) const -> std::optional< std::reference_wrapper< const Location > >
 Get the location associated with a given pointer.
auto traverse (const sourcemeta::core::WeakPointer &pointer, const LocationType type) const -> std::optional< std::reference_wrapper< const Location > >
 Get the location of a specific type associated with a given pointer.
auto uri (const sourcemeta::core::WeakPointer &pointer) const -> std::optional< std::reference_wrapper< const sourcemeta::core::JSON::String > >
 Turn an absolute pointer into a location URI.
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.
template<std::invocable< const Location & > F>
auto for_each_subschema (const F &callback) const -> void
template<std::invocable< const Location & > F>
auto for_each_subschema_under (const sourcemeta::core::WeakPointer &pointer, const F &callback) const -> void
template<std::predicate< const Location & > F>
auto any_subschema (const F &predicate) const -> bool
 Check whether any schema resource or subschema satisfies the predicate.
template<std::predicate< const Location & > F>
auto any_subschema_under (const sourcemeta::core::WeakPointer &pointer, const F &predicate) const -> bool
template<std::invocable< std::string_view, const Location & > F>
auto for_each_anchor (const SchemaReferenceType type, const F &callback) const -> void
 Iterate over every anchor of the given kind, along with its URI.
template<std::predicate< std::string_view, const Location & > F>
auto any_anchor (const SchemaReferenceType type, const F &predicate) const -> bool
 Check whether any anchor of the given kind satisfies the predicate.
template<std::invocable< std::string_view, const Location & > F>
auto for_each_resource (const F &callback) const -> void
 Iterate over every schema resource, along with its URI.
template<std::invocable< SchemaReferenceType, std::string_view, const Location & > F>
auto for_each_location (const F &callback) const -> void
 Iterate over every location, whatever kind it is.
template<std::predicate< SchemaReferenceType, std::string_view, const Location & > F>
auto any_location (const F &predicate) const -> bool
 Check whether any location satisfies the predicate.
template<std::invocable< SchemaReferenceType, const sourcemeta::core::WeakPointer &, const Reference & > F>
auto for_each_reference (const F &callback) const -> void
 Iterate over every reference, along with the pointer it originates from.
template<std::predicate< SchemaReferenceType, const sourcemeta::core::WeakPointer &, const Reference & > F>
auto any_reference (const F &predicate) const -> bool
 Check whether any reference satisfies the predicate.
template<std::invocable< SchemaReferenceType, const sourcemeta::core::WeakPointer &, const Reference & > F>
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.
template<std::predicate< SchemaReferenceType, const sourcemeta::core::WeakPointer &, const Reference & > F>
auto any_reference_from (const sourcemeta::core::WeakPointer &pointer, const F &predicate) const -> bool
template<std::invocable< SchemaReferenceType, const sourcemeta::core::WeakPointer &, const Reference & > F>
auto for_each_reference_into (const sourcemeta::core::WeakPointer &pointer, const F &callback) const -> void
template<std::predicate< SchemaReferenceType, const sourcemeta::core::WeakPointer &, const Reference & > F>
auto any_reference_into (const sourcemeta::core::WeakPointer &pointer, const F &predicate) const -> bool
template<std::invocable< std::string_view > F>
auto for_each_resource_uri (const F &callback) const -> void
 Iterate over all resource URIs in the frame.
template<std::invocable< const sourcemeta::core::WeakPointer &, const Reference & > F>
auto for_each_unresolved_reference (const F &callback) const -> void
auto has_references_to (const sourcemeta::core::WeakPointer &pointer) const -> bool
 Check if there are any references to a given location pointer.
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 has_references_through (const sourcemeta::core::WeakPointer &pointer, const sourcemeta::core::WeakPointer::Token &tail) const -> bool
auto relative_instance_location (const Location &location) const -> sourcemeta::core::WeakPointer
 Get the relative instance location pointer for a given location entry.
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.

Member Enumeration Documentation

◆ IdentifierMode

enum class sourcemeta::core::SchemaFrame::IdentifierMode : std::uint8_t
strong

How a caller-provided default identifier relates to the one that the schema declares, if any

Enumerator
Additional 

Register the default identifier in addition to the schema's own.

Fallback 

Register the default identifier only if the schema declares none.

◆ Mode

enum class sourcemeta::core::SchemaFrame::Mode : std::uint8_t
strong

The mode of framing. More extensive analysis can be compute and memory intensive. Each mode is a superset of the previous one. Note that sourcemeta::core::SchemaFrame::Mode::Root reports on a single schema, so framing a wrapper that holds more than one yields no locations

sourcemeta::core::SchemaFrame::Mode::Locations and sourcemeta::core::SchemaFrame::Mode::References locate the schemas of the document rather than each of its JSON Pointers, and the latter also locates whatever place a reference names. Reach for sourcemeta::core::SchemaFrame::Mode::Pointers only to address a keyword or a value of the document by URI, as computing those locations tends to dominate the cost of framing

Enumerator
Root 

Report on the single schema that the document declares.

Locations 

Report on every schema resource and anchor of the document.

References 

Also report on whatever place every reference names.

Pointers 

Also report on every JSON Pointer of the document.

Constructor & Destructor Documentation

◆ SchemaFrame()

sourcemeta::core::SchemaFrame::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() )

Frame a schema or set of schemas from a given root. Passing multiple paths that have any overlap is undefined behaviour

A frame is analysed once, on construction, and is immutable afterwards

The resulting locations point into the schema rather than copying from it, so the schema must outlive the frame. The same goes for default_dialect, as a location that has no dialect of its own reports the default back as a view into what the caller passed. In contrast, default_id and default_base are copied, so they do not need to outlive this call

Pass default_base to state the base URI that the document was retrieved from, which anything that declares no identifier of its own inherits, and which a relative identifier or reference resolves against. Unlike default_id, this does not claim that the document declares an identifier, so a pointer into it keeps being addressed from the top of the document rather than from wherever a path begins, and a document that declares no identifier still reports none

Framing a schema that declares nested identifiers registers a location per enclosing base, so what an untrusted schema costs to analyse grows faster than the schema itself does. Pass max_locations to bound that, throwing sourcemeta::core::SchemaFrameLimitError rather than analysing past it. What the limit buys depends on the mode, as sourcemeta::core::SchemaFrame::Mode::Pointers locates every JSON Pointer of the document rather than only the schemas of it. Note this bounds what framing registers rather than every last thing it does, as walking the document costs something even where nothing comes of it. Bounding the size of the document remains the caller's to do

Member Function Documentation

◆ any_reference_from()

template<std::predicate< SchemaReferenceType, const sourcemeta::core::WeakPointer &, const Reference & > F>
auto sourcemeta::core::SchemaFrame::any_reference_from ( const sourcemeta::core::WeakPointer & pointer,
const F & predicate ) const -> bool
inlinenodiscard

Check whether any reference originating at or below the given pointer satisfies the predicate

◆ any_reference_into()

template<std::predicate< SchemaReferenceType, const sourcemeta::core::WeakPointer &, const Reference & > F>
auto sourcemeta::core::SchemaFrame::any_reference_into ( const sourcemeta::core::WeakPointer & pointer,
const F & predicate ) const -> bool
inlinenodiscard

Check whether any reference whose destination resolves at or below the given pointer satisfies the predicate

◆ any_subschema_under()

template<std::predicate< const Location & > F>
auto sourcemeta::core::SchemaFrame::any_subschema_under ( const sourcemeta::core::WeakPointer & pointer,
const F & predicate ) const -> bool
inlinenodiscard

Check whether any schema resource or subschema strictly below the given pointer satisfies the predicate

◆ for_each_reference_into()

template<std::invocable< SchemaReferenceType, const sourcemeta::core::WeakPointer &, const Reference & > F>
auto sourcemeta::core::SchemaFrame::for_each_reference_into ( const sourcemeta::core::WeakPointer & pointer,
const F & callback ) const -> void
inline

Iterate over every reference whose destination resolves at or below the given pointer. Note that unlike sourcemeta::core::SchemaFrame::has_references_to, which matches a destination exactly, this covers the entire subtree

◆ for_each_subschema()

template<std::invocable< const Location & > F>
auto sourcemeta::core::SchemaFrame::for_each_subschema ( const F & callback) const -> void
inline

Iterate over every schema resource and subschema, skipping the pointer and anchor entries that do not stand for a schema of their own

◆ for_each_subschema_under()

template<std::invocable< const Location & > F>
auto sourcemeta::core::SchemaFrame::for_each_subschema_under ( const sourcemeta::core::WeakPointer & pointer,
const F & callback ) const -> void
inline

Iterate over every schema resource and subschema strictly below the given pointer

◆ for_each_unresolved_reference()

template<std::invocable< const sourcemeta::core::WeakPointer &, const Reference & > F>
auto sourcemeta::core::SchemaFrame::for_each_unresolved_reference ( const F & callback) const -> void
inline

Iterate over all unresolved references (where destination cannot be traversed)

◆ has_references_through()

auto sourcemeta::core::SchemaFrame::has_references_through ( const sourcemeta::core::WeakPointer & pointer,
const sourcemeta::core::WeakPointer::Token & tail ) const -> bool
nodiscard

Check if there are any references that go through a given location pointer with a tail token

◆ metaschema()

auto sourcemeta::core::SchemaFrame::metaschema ( const SchemaResolver & resolver) const -> const sourcemeta::core::JSON &
nodiscard

Get the meta-schema of the analysed schema, preferring one embedded in the document itself over what the resolver knows about

◆ root_location()

auto sourcemeta::core::SchemaFrame::root_location ( ) const -> std::optional< std::reference_wrapper< const Location > >
nodiscard

Get the location entry of the schema that was analysed. Unlike sourcemeta::core::SchemaFrame::root, this works for anonymous schemas

◆ vocabularies()

auto sourcemeta::core::SchemaFrame::vocabularies ( const Location & location,
const SchemaResolver & resolver ) const -> const SchemaVocabularies &
nodiscard

Get the vocabularies associated with a location entry. The frame owns the result, computing it at most once per dialect that it came across. Note that as with the meta-schemas that framing found embedded in the document, what the first resolver reported for a given dialect is what every later call reports, whichever resolver they pass

◆ sourcemeta::core::SchemaWalkerResult

struct sourcemeta::core::SchemaWalkerResult

A structure that encapsulates the result of walker over a specific keyword

Public Member Functions

 SchemaWalkerResult (const SchemaWalkerResult &)=delete
 SchemaWalkerResult (SchemaWalkerResult &&)=default
 Move a walker result.
constexpr SchemaWalkerResult (SchemaKeywordType keyword_type, std::optional< SchemaVocabularies::URIView > keyword_vocabulary, std::span< const std::string_view > keyword_dependencies, std::span< const std::string_view > keyword_order_dependencies, sourcemeta::core::JSON::TypeSet keyword_instances)

Public Attributes

SchemaKeywordType type
 The walker strategy to continue traversing across the schema.
std::optional< SchemaVocabularies::URIView > vocabulary
 The vocabulary associated with the keyword, if any.
std::span< const std::string_view > dependencies
std::span< const std::string_view > order_dependencies
sourcemeta::core::JSON::TypeSet instances
 The JSON instance types that this keyword applies to (empty means all).

Constructor & Destructor Documentation

◆ SchemaWalkerResult()

sourcemeta::core::SchemaWalkerResult::SchemaWalkerResult ( SchemaKeywordType keyword_type,
std::optional< SchemaVocabularies::URIView > keyword_vocabulary,
std::span< const std::string_view > keyword_dependencies,
std::span< const std::string_view > keyword_order_dependencies,
sourcemeta::core::JSON::TypeSet keyword_instances )
inlineconstexpr

Describe a keyword from its type, vocabulary, dependencies, and the instance types it applies to

Member Data Documentation

◆ dependencies

std::span<const std::string_view> sourcemeta::core::SchemaWalkerResult::dependencies

The keywords a given keyword depends on (if any) during the evaluation process

◆ order_dependencies

std::span<const std::string_view> sourcemeta::core::SchemaWalkerResult::order_dependencies

The keywords a given keyword depends on for evaluation ordering purposes only (not semantic dependencies)

◆ sourcemeta::core::SchemaVocabularies

struct sourcemeta::core::SchemaVocabularies

Optimized vocabulary set using bitflags for known vocabularies and a fallback std::unordered_map for custom vocabularies.

Public Types

enum class  Known : std::uint8_t {
  JSON_SCHEMA_DRAFT_0 = 0 , JSON_SCHEMA_DRAFT_0_HYPER = 1 , JSON_SCHEMA_DRAFT_1 = 2 , JSON_SCHEMA_DRAFT_1_HYPER = 3 ,
  JSON_SCHEMA_DRAFT_2 = 4 , JSON_SCHEMA_DRAFT_2_HYPER = 5 , JSON_SCHEMA_DRAFT_3 = 6 , JSON_SCHEMA_DRAFT_3_HYPER = 7 ,
  JSON_SCHEMA_DRAFT_4 = 8 , JSON_SCHEMA_DRAFT_4_HYPER = 9 , JSON_SCHEMA_DRAFT_6 = 10 , JSON_SCHEMA_DRAFT_6_HYPER = 11 ,
  JSON_SCHEMA_DRAFT_7 = 12 , JSON_SCHEMA_DRAFT_7_HYPER = 13 , JSON_SCHEMA_2019_09_CORE = 14 , JSON_SCHEMA_2019_09_APPLICATOR = 15 ,
  JSON_SCHEMA_2019_09_VALIDATION = 16 , JSON_SCHEMA_2019_09_META_DATA = 17 , JSON_SCHEMA_2019_09_FORMAT = 18 , JSON_SCHEMA_2019_09_CONTENT = 19 ,
  JSON_SCHEMA_2019_09_HYPER_SCHEMA = 20 , JSON_SCHEMA_2020_12_CORE = 21 , JSON_SCHEMA_2020_12_APPLICATOR = 22 , JSON_SCHEMA_2020_12_UNEVALUATED = 23 ,
  JSON_SCHEMA_2020_12_VALIDATION = 24 , JSON_SCHEMA_2020_12_META_DATA = 25 , JSON_SCHEMA_2020_12_FORMAT_ANNOTATION = 26 , JSON_SCHEMA_2020_12_FORMAT_ASSERTION = 27 ,
  JSON_SCHEMA_2020_12_CONTENT = 28 , OPENAPI_3_1_BASE = 29 , OPENAPI_3_2_BASE = 30 , SOURCEMETA_EXTENSION_V1 = 31
}
 Every vocabulary that this implementation recognises out of the box. More...
using URI = std::variant<Known, sourcemeta::core::JSON::String>
using URIView = std::variant<Known, std::string_view>

Public Member Functions

 SchemaVocabularies (const SchemaVocabularies &)=default
 Copy a vocabulary set.
 SchemaVocabularies (SchemaVocabularies &&) noexcept=default
 Move a vocabulary set.
 SchemaVocabularies (std::initializer_list< std::pair< sourcemeta::core::JSON::String, bool > > init)
 Construct from initializer list.
 SchemaVocabularies (std::initializer_list< std::pair< Known, bool > > init)
 Construct from initializer list using known vocabulary enums.
auto contains (const sourcemeta::core::JSON::String &uri) const noexcept -> bool
 Check if a vocabulary is enabled.
auto contains (Known vocabulary) const noexcept -> bool
 Check if a known vocabulary is enabled.
auto contains_any (std::initializer_list< Known > vocabularies) const noexcept -> bool
 Check if any of the given known vocabularies are enabled.
auto insert (const sourcemeta::core::JSON::String &uri, bool required) noexcept -> void
 Insert a vocabulary with its required/optional status.
auto insert (Known vocabulary, bool required) noexcept -> void
 Insert a known vocabulary with its required/optional status.
auto get (const sourcemeta::core::JSON::String &uri) const noexcept -> std::optional< bool >
 Get vocabulary status by URI.
auto get (Known vocabulary) const noexcept -> std::optional< bool >
 Get known vocabulary status.
auto size () const noexcept -> std::size_t
 Get the number of vocabularies (required + optional + custom).
auto empty () const noexcept -> bool
 Check if there are no vocabularies.
auto has_unknown () const noexcept -> bool
 Check if there are any unknown vocabularies.
template<typename Callback>
auto for_each (const Callback &callback) const -> void
 Iterate over every vocabulary, along with whether it is required.
auto throw_if_any_unsupported (const std::unordered_set< URI > &supported, const char *message) const -> void

Static Public Attributes

static constexpr std::size_t KNOWN_VOCABULARY_COUNT = 32
 How many vocabularies this implementation recognises out of the box.

Member Typedef Documentation

◆ URI

A vocabulary URI type that can be either a known vocabulary enum or a custom string URI

◆ URIView

using sourcemeta::core::SchemaVocabularies::URIView = std::variant<Known, std::string_view>

A vocabulary URI that does not own its custom string, for tables that point at storage that outlives them

Member Enumeration Documentation

◆ Known

enum class sourcemeta::core::SchemaVocabularies::Known : std::uint8_t
strong

Every vocabulary that this implementation recognises out of the box.

Enumerator
JSON_SCHEMA_DRAFT_0 

The Draft 0 dialect, which predates vocabularies.

JSON_SCHEMA_DRAFT_0_HYPER 

The Draft 0 hyper-schema dialect, which predates vocabularies.

JSON_SCHEMA_DRAFT_1 

The Draft 1 dialect, which predates vocabularies.

JSON_SCHEMA_DRAFT_1_HYPER 

The Draft 1 hyper-schema dialect, which predates vocabularies.

JSON_SCHEMA_DRAFT_2 

The Draft 2 dialect, which predates vocabularies.

JSON_SCHEMA_DRAFT_2_HYPER 

The Draft 2 hyper-schema dialect, which predates vocabularies.

JSON_SCHEMA_DRAFT_3 

The Draft 3 dialect, which predates vocabularies.

JSON_SCHEMA_DRAFT_3_HYPER 

The Draft 3 hyper-schema dialect, which predates vocabularies.

JSON_SCHEMA_DRAFT_4 

The Draft 4 dialect, which predates vocabularies.

JSON_SCHEMA_DRAFT_4_HYPER 

The Draft 4 hyper-schema dialect, which predates vocabularies.

JSON_SCHEMA_DRAFT_6 

The Draft 6 dialect, which predates vocabularies.

JSON_SCHEMA_DRAFT_6_HYPER 

The Draft 6 hyper-schema dialect, which predates vocabularies.

JSON_SCHEMA_DRAFT_7 

The Draft 7 dialect, which predates vocabularies.

JSON_SCHEMA_DRAFT_7_HYPER 

The Draft 7 hyper-schema dialect, which predates vocabularies.

JSON_SCHEMA_2019_09_CORE 

The 2019-09 core vocabulary.

JSON_SCHEMA_2019_09_APPLICATOR 

The 2019-09 applicator vocabulary.

JSON_SCHEMA_2019_09_VALIDATION 

The 2019-09 validation vocabulary.

JSON_SCHEMA_2019_09_META_DATA 

The 2019-09 meta-data vocabulary.

JSON_SCHEMA_2019_09_FORMAT 

The 2019-09 format vocabulary.

JSON_SCHEMA_2019_09_CONTENT 

The 2019-09 content vocabulary.

JSON_SCHEMA_2019_09_HYPER_SCHEMA 

The 2019-09 hyper-schema vocabulary.

JSON_SCHEMA_2020_12_CORE 

The 2020-12 core vocabulary.

JSON_SCHEMA_2020_12_APPLICATOR 

The 2020-12 applicator vocabulary.

JSON_SCHEMA_2020_12_UNEVALUATED 

The 2020-12 unevaluated vocabulary.

JSON_SCHEMA_2020_12_VALIDATION 

The 2020-12 validation vocabulary.

JSON_SCHEMA_2020_12_META_DATA 

The 2020-12 meta-data vocabulary.

JSON_SCHEMA_2020_12_FORMAT_ANNOTATION 

The 2020-12 format vocabulary, in its annotation form.

JSON_SCHEMA_2020_12_FORMAT_ASSERTION 

The 2020-12 format vocabulary, in its assertion form.

JSON_SCHEMA_2020_12_CONTENT 

The 2020-12 content vocabulary.

OPENAPI_3_1_BASE 

The OpenAPI 3.1 base vocabulary.

OPENAPI_3_2_BASE 

The OpenAPI 3.2 base vocabulary.

SOURCEMETA_EXTENSION_V1 

The first version of the Sourcemeta extension vocabulary.

Member Function Documentation

◆ throw_if_any_unsupported()

auto sourcemeta::core::SchemaVocabularies::throw_if_any_unsupported ( const std::unordered_set< URI > & supported,
const char * message ) const -> void

Throw if the current vocabularies have required ones outside the given supported set

Typedef Documentation

◆ SchemaResolver

using sourcemeta::core::SchemaResolver = std::function<SchemaResolverResult(std::string_view)>

Some functions need to reference other schemas by their URIs. To accomplish this in a generic and flexible way, these functions take resolver functions as arguments, of the type sourcemeta::core::SchemaResolver.

For convenience, we provide the following default resolvers:

You can implement resolvers to read from a local storage, to send HTTP requests, or anything your application might require. Unless your resolver is trivial, it is recommended to create a callable object that implements the function interface.

◆ SchemaResolverResult

Initial value:

What a sourcemeta::core::SchemaResolver hands back: either a schema it owns, or a reference to one that outlives the call

◆ SchemaWalker

Initial value:
std::function<const SchemaWalkerResult &(
std::string_view, const SchemaVocabularies &)>
Definition jsonschema_vocabularies.h:30
Definition jsonschema_types.h:211

For walking purposes, some functions need to understand which JSON Schema keywords declare other JSON Schema definitions. To accomplish this in a generic and flexible way that does not assume the use any vocabulary other than core, these functions take a walker function as argument.

Enumeration Type Documentation

◆ LocationType

enum class sourcemeta::core::SchemaFrame::LocationType : std::uint8_t
strong

The type of a location frame

Enumerator
Resource 

A schema resource, as introduced by an identifier.

Anchor 

An anchor declared within a schema resource.

Pointer 

A JSON Pointer into the document that is not a schema of its own.

Subschema 

A subschema that declares no identifier of its own.

◆ SchemaBaseDialect

enum class sourcemeta::core::SchemaBaseDialect : std::uint8_t
strong

All the known JSON Schema base dialects

Enumerator
JSON_SCHEMA_2020_12 

The 2020-12 validation base dialect.

JSON_SCHEMA_2020_12_HYPER 

The 2020-12 hyper-schema base dialect.

JSON_SCHEMA_2019_09 

The 2019-09 validation base dialect.

JSON_SCHEMA_2019_09_HYPER 

The 2019-09 hyper-schema base dialect.

JSON_SCHEMA_DRAFT_7 

The Draft 7 validation base dialect.

JSON_SCHEMA_DRAFT_7_HYPER 

The Draft 7 hyper-schema base dialect.

JSON_SCHEMA_DRAFT_6 

The Draft 6 validation base dialect.

JSON_SCHEMA_DRAFT_6_HYPER 

The Draft 6 hyper-schema base dialect.

JSON_SCHEMA_DRAFT_4 

The Draft 4 validation base dialect.

JSON_SCHEMA_DRAFT_4_HYPER 

The Draft 4 hyper-schema base dialect.

JSON_SCHEMA_DRAFT_3 

The Draft 3 validation base dialect.

JSON_SCHEMA_DRAFT_3_HYPER 

The Draft 3 hyper-schema base dialect.

JSON_SCHEMA_DRAFT_2_HYPER 

The Draft 2 hyper-schema base dialect.

JSON_SCHEMA_DRAFT_1_HYPER 

The Draft 1 hyper-schema base dialect.

JSON_SCHEMA_DRAFT_0_HYPER 

The Draft 0 hyper-schema base dialect.

◆ SchemaKeywordType

enum class sourcemeta::core::SchemaKeywordType : std::uint8_t
strong

Determines the type of a JSON Schema keyword

Enumerator
Unknown 

The JSON Schema keyword is unknown.

Assertion 

The JSON Schema keyword is a non-applicator assertion.

Annotation 

The JSON Schema keyword is a non-applicator annotation.

Reference 

The JSON Schema keyword is a reference.

Other 

The JSON Schema keyword is known but doesn't match any other type.

Comment 

The JSON Schema keyword is considered to be a comment without any additional meaning

LocationMembers 

The JSON Schema keyword is a reserved location that potentially takes an object as argument, whose values are potentially JSON Schema definitions

ApplicatorMembersTraversePropertyStatic 

The JSON Schema keyword is an applicator that potentially takes an object as argument, whose values are potentially JSON Schema definitions. The instance traverses based on the members as property names

ApplicatorMembersTraversePropertyRegex 

The JSON Schema keyword is an applicator that potentially takes an object as argument, whose values are potentially JSON Schema definitions. The instance traverses based on the members as property regular expressions

ApplicatorValueTraverseSomeProperty 

The JSON Schema keyword is an applicator that potentially takes a JSON Schema definition as an argument The instance traverses to some of the properties

ApplicatorValueTraverseAnyPropertyKey 

The JSON Schema keyword is an applicator that potentially takes a JSON Schema definition as an argument The instance traverses to any property key

ApplicatorValueTraverseAnyItem 

The JSON Schema keyword is an applicator that potentially takes a JSON Schema definition as an argument The instance traverses to any item

ApplicatorValueTraverseSomeItem 

The JSON Schema keyword is an applicator that potentially takes a JSON Schema definition as an argument The instance traverses to some of the items

ApplicatorValueTraverseParent 

The JSON Schema keyword is an applicator that potentially takes a JSON Schema definition as an argument The instance traverses back to the parent

ApplicatorElementsTraverseItem 

The JSON Schema keyword is an applicator that potentially takes an array of potentially JSON Schema definitions as an argument The instance traverses based on the element indexes

ApplicatorValueOrElementsTraverseAnyItemOrItem 

The JSON Schema keyword is an applicator that may take a JSON Schema definition or an array of potentially JSON Schema definitions as an argument The instance traverses to any item or based on the element indexes

ApplicatorValueOrElementsInPlace 

The JSON Schema keyword is an applicator that may take a JSON Schema definition or an array of potentially JSON Schema definitions as an argument without affecting the instance location. The instance does not traverse

ApplicatorMembersInPlaceSome 

The JSON Schema keyword is an applicator that potentially takes an object as argument, whose values are potentially JSON Schema definitions without affecting the instance location. The instance does not traverse

ApplicatorElementsInPlace 

The JSON Schema keyword is an applicator that potentially takes an array of potentially JSON Schema definitions as an argument without affecting the instance location. The instance does not traverse

ApplicatorElementsInPlaceSome 

The JSON Schema keyword is an applicator that potentially takes an array of potentially JSON Schema definitions as an argument without affecting the instance location The instance does not traverse, and only some of the elements apply.

ApplicatorElementsInPlaceSomeNegate 

The JSON Schema keyword is an applicator that potentially takes an array of potentially JSON Schema definitions as an argument without affecting the instance location The instance does not traverse, and only some of the elements apply in negated form.

ApplicatorValueInPlaceMaybe 

The JSON Schema keyword is an applicator that potentially takes a JSON Schema definition as an argument without affecting the instance location. The instance does not traverse, and only applies some of the times.

ApplicatorValueInPlaceOther 

The JSON Schema keyword is an applicator that potentially takes a JSON Schema definition as an argument but its evaluation follows special rules. The instance does not traverse

ApplicatorValueInPlaceNegate 

The JSON Schema keyword is an applicator that potentially takes a JSON Schema definition as an argument but the instance is expected to not validate against it. The instance does not traverse

◆ SchemaReferenceType

enum class sourcemeta::core::SchemaReferenceType : std::uint8_t
strong

The reference type

Enumerator
Static 

A reference that resolves at framing time.

Dynamic 

A reference that resolves at evaluation time.

Function Documentation

◆ schema_bundle() [1/2]

SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_bundle ( const sourcemeta::core::JSON & schema,
const SchemaWalker & walker,
const SchemaResolver & resolver,
std::string_view default_dialect = "",
std::string_view default_id = "",
const SchemaBundleOptions & options = {} ) -> sourcemeta::core::JSON

This function bundles a JSON Schema (starting from Draft 4) by embedding every remote reference into the top level schema resource, handling circular dependencies and more. This overload returns a new schema, without mutating the input schema. For example:

#include <sourcemeta/core/json.h>
#include <sourcemeta/core/jsonschema.h>
#include <cassert>
// A custom resolver that knows about an additional schema
static auto test_resolver(std::string_view identifier)
if (identifier == "https://www.example.com/test") {
"$id": "https://www.example.com/test",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string"
})JSON");
} else {
}
}
const sourcemeta::core::JSON document =
"$schema": "https://json-schema.org/draft/2020-12/schema",
"items": { "$ref": "https://www.example.com/test" }
})JSON");
const sourcemeta::core::JSON result =
const sourcemeta::core::JSON expected =
"$schema": "https://json-schema.org/draft/2020-12/schema",
"items": { "$ref": "https://www.example.com/test" },
"$defs": {
"https://www.example.com/test": {
"$id": "https://www.example.com/test",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string"
}
}
})JSON");
assert(result == expected);
sourcemeta::core::OwnedOrReference< sourcemeta::core::JSON > SchemaResolverResult
Definition jsonschema_types.h:24
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto schema_bundle(sourcemeta::core::JSON &schema, const SchemaWalker &walker, const SchemaResolver &resolver, std::string_view default_dialect="", std::string_view default_id="", const SchemaBundleOptions &options={}) -> void

◆ schema_bundle() [2/2]

SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_bundle ( sourcemeta::core::JSON & schema,
const SchemaWalker & walker,
const SchemaResolver & resolver,
std::string_view default_dialect = "",
std::string_view default_id = "",
const SchemaBundleOptions & options = {} ) -> void

This function bundles a JSON Schema (starting from Draft 4) by embedding every remote reference into the top level schema resource, handling circular dependencies and more. This overload mutates the input schema. For example:

#include <sourcemeta/core/json.h>
#include <sourcemeta/core/jsonschema.h>
#include <cassert>
// A custom resolver that knows about an additional schema
static auto test_resolver(std::string_view identifier)
if (identifier == "https://www.example.com/test") {
"$id": "https://www.example.com/test",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string"
})JSON");
} else {
}
}
sourcemeta::core::JSON document =
"$schema": "https://json-schema.org/draft/2020-12/schema",
"items": { "$ref": "https://www.example.com/test" }
})JSON");
const sourcemeta::core::JSON expected =
"$schema": "https://json-schema.org/draft/2020-12/schema",
"items": { "$ref": "https://www.example.com/test" },
"$defs": {
"https://www.example.com/test": {
"$id": "https://www.example.com/test",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "string"
}
}
})JSON");
assert(document == expected);

◆ schema_format()

SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_format ( sourcemeta::core::JSON & schema,
const SchemaFrame & frame ) -> void

This function reorders the properties of every subschema that the given frame reports, following an opinionated JSON Schema aware order, modifying the schema in place. Note that doing so invalidates the given frame, as the locations it holds point into the schema. For example:

#include <sourcemeta/core/json.h>
#include <sourcemeta/core/jsonschema.h>
#include <iostream>
#include <sstream>
"type": "string",
"minLength": 3,
"$schema": "https://json-schema.org/draft/2020-12/schema"
})JSON");
std::ostringstream stream;
sourcemeta::core::prettify(document, stream);
std::cout << stream.str() << std::endl;
SOURCEMETA_CORE_JSON_EXPORT auto prettify(const JSON &document, std::basic_ostream< JSON::Char, JSON::CharTraits > &stream, const std::size_t spaces=2) -> void
@ Locations
Report on every schema resource and anchor of the document.
Definition jsonschema_frame.h:75
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto schema_format(sourcemeta::core::JSON &schema, const SchemaFrame &frame) -> void

◆ schema_identifier_keyword()

SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_identifier_keyword ( const SchemaBaseDialect base_dialect) -> std::string_view

The keyword that carries a schema identifier in the given base dialect. For example:

#include <sourcemeta/core/jsonschema.h>
#include <cassert>
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto schema_identifier_keyword(const SchemaBaseDialect base_dialect) -> std::string_view
@ JSON_SCHEMA_DRAFT_4
The Draft 4 validation base dialect.
Definition jsonschema_types.h:72
@ JSON_SCHEMA_2020_12
The 2020-12 validation base dialect.
Definition jsonschema_types.h:56

◆ schema_is_known()

SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_is_known ( const std::string_view identifier) -> bool
noexcept

Check if a given identifier corresponds to a known built-in schema

◆ schema_is_official()

SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_is_official ( const std::string_view identifier) -> bool
noexcept

Check if a given URI corresponds to an official schema released by the JSON Schema organisation

◆ schema_reidentify() [1/2]

SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_reidentify ( sourcemeta::core::JSON & schema,
std::string_view new_identifier,
const SchemaBaseDialect base_dialect ) -> void

A shortcut to sourcemeta::core::schema_reidentify if you know the base dialect of the schema.

◆ schema_reidentify() [2/2]

SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_reidentify ( sourcemeta::core::JSON & schema,
std::string_view new_identifier,
const SchemaResolver & resolver,
std::string_view default_dialect = "" ) -> void

This function sets the identifier of a schema, replacing the existing one, if any. For example:

#include <sourcemeta/core/json.h>
#include <sourcemeta/core/jsonschema.h>
#include <cassert>
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://sourcemeta.com/example-schema"
})JSON");
"https://example.com/my-new-id",
assert(document.at("$id").to_string() ==
"https://example.com/my-new-id");
SOURCEMETA_FORCEINLINE auto at(const Array::size_type index) const -> const JSON &
Definition json_value.h:893
SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto schema_reidentify(sourcemeta::core::JSON &schema, std::string_view new_identifier, const SchemaResolver &resolver, std::string_view default_dialect="") -> void

◆ schema_resolver()

SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_resolver ( const std::string_view identifier) -> SchemaResolverResult

A default resolver that relies on built-in official schemas. The schemas are parsed once and handed back by reference, so they must not outlive the program.

◆ schema_walker()

SOURCEMETA_CORE_JSONSCHEMA_EXPORT auto sourcemeta::core::schema_walker ( const std::string_view keyword,
const SchemaVocabularies & vocabularies ) -> const SchemaWalkerResult &

A default schema walker with support for a wide range of drafts