pjson 2.0.0
A small, owning JSON value for C++11
Loading...
Searching...
No Matches
ByteDance::pJsonSchemaValidator Class Reference

Validates pjson values against a schema (itself a pjson value). More...

#include <pjson_schema.h>

Classes

struct  Error
 One schema-compilation or instance-validation failure. More...
 
struct  Options
 Bounds schema work and controls dialect, format, and resolver policy. More...
 

Public Types

typedef bool(* Resolver) (const std::string &aDocumentUri, pjson &aSchema, void *aContext)
 Resolves one absolute schema-document URI during construction. Implementations populate aSchema and return true, or return false when unavailable. The resolved document is copied into a cache owned by the validator; pjson performs no implicit network I/O. The callback and aContext need remain valid only until construction returns.
 

Public Member Functions

 pJsonSchemaValidator (const pjson &aSchema, const Options &aOptions=Options())
 Compiles aSchema (deep-copied with the default allocator) and all resolved resources. Resolver failures, malformed references, and duplicate IDs/anchors are reported through isSchemaValid() and schemaErrors(); allocation failure may still throw std::bad_alloc.
 
 ~pJsonSchemaValidator ()
 Destroys the compiled schema.
 
bool validate (const pjson &aInstance) const noexcept
 Returns whether aInstance conforms to the compiled schema. Never throws.
 
bool validate (const pjson &aInstance, std::vector< Error > &aErrors) const noexcept
 Validates and appends discovered failures to aErrors. If schema compilation failed, appends schemaErrors() instead. Never throws.
 
bool isSchemaValid () const noexcept
 Returns whether dialect and required-vocabulary compilation succeeded.
 
const std::vector< Error > & schemaErrors () const noexcept
 Returns immutable schema-compilation diagnostics (paths address the schema).
 
const std::string & dialect () const noexcept
 Returns the effective dialect URI selected by $schema or the option default.
 
const pjson & schema () const noexcept
 Returns the compiled schema value (read-only).
 
const Options & options () const noexcept
 Returns the options in effect for this validator. Construction-only resolver pointers are cleared in this snapshot.
 

Static Public Member Functions

static const char * documentedSubsetDialectUri () noexcept
 URI naming pjson's explicitly documented JSON Schema subset dialect.
 
static const char * documentedSubsetVocabularyUri () noexcept
 URI naming the vocabulary implemented by the subset dialect.
 
static const char * draft2020DialectUri () noexcept
 URI of the supported official JSON Schema Draft 2020-12 dialect.
 

Detailed Description

Validates pjson values against a schema (itself a pjson value).

Validates pjson values using the subset or Draft 2020-12 contract.

Construct once from a schema; validate many instances. The schema is deep-copied on construction, so the caller's schema value may change or be destroyed afterward. Validation never throws and never mutates its inputs. Construction resolves and owns external resources; validate() is read-only and may be called concurrently when each caller uses its own error vector.

Dialect contract: default construction implements the named subset dialect. Options::draft2020() selects official Draft 2020-12, bundled meta-schema validation, and per-resource vocabulary activation. Custom meta-schemas require explicit resolver-based loading. Unknown optional vocabularies are annotations and unknown required vocabularies fail compilation.

Supported keywords (subset defaults and required Draft 2020-12 vocabularies): type, enum, const, $ref, $dynamicRef, $id, $anchor, $dynamicAnchor; properties, patternProperties, propertyNames, required, dependentRequired, dependencies, dependentSchemas, additionalProperties, unevaluatedProperties, minProperties, maxProperties; items, prefixItems, contains, minContains, maxContains, minItems, maxItems, uniqueItems, unevaluatedItems; minimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf; minLength, maxLength, pattern, format; allOf, anyOf, oneOf, not, if, then, else. A boolean schema (true/false) accepts/rejects everything. By default unknown or unsupported keywords are ignored; strict() rejects unsupported standard keywords. External references require an explicit Resolver; pjson never performs network I/O. Regular expressions use a private Unicode-aware ECMAScript engine, including property escapes.

pJsonSchemaValidator is a standalone helper declared in <pjson_schema.h> and built from focused private schema translation units. It is a pure consumer of pjson's public API and never touches the DOM's internal storage, so the core pjson class carries no schema or regex state. The implementation is isolated in its own translation unit for future optional-library packaging. Construct one validator from a schema (deep-copied on construction) and reuse it to validate many instances; validation never throws and never mutates its inputs. Default options implement pjson's explicitly named subset dialect; Options::draft2020() selects the required Draft 2020-12 vocabularies, bundled meta-schema validation, and per-resource vocabulary activation. A root $schema or Options::defaultDialectUri selects the effective dialect; unsupported dialects and required vocabularies fail compilation. Inspect isSchemaValid(), schemaErrors(), and dialect() before trusting validation results.

See also
ByteDance::pJsonSchemaValidator::Options
ByteDance::pJsonSchemaValidator::Error

Definition at line 74 of file pjson_schema.h.

Member Typedef Documentation

◆ Resolver

typedef bool(* ByteDance::pJsonSchemaValidator::Resolver) (const std::string &aDocumentUri, pjson &aSchema, void *aContext)

Resolves one absolute schema-document URI during construction. Implementations populate aSchema and return true, or return false when unavailable. The resolved document is copied into a cache owned by the validator; pjson performs no implicit network I/O. The callback and aContext need remain valid only until construction returns.

Definition at line 81 of file pjson_schema.h.

Constructor & Destructor Documentation

◆ pJsonSchemaValidator()

ByteDance::pJsonSchemaValidator::pJsonSchemaValidator ( const pjson &  aSchema,
const Options &  aOptions = Options() 
)
explicit

Compiles aSchema (deep-copied with the default allocator) and all resolved resources. Resolver failures, malformed references, and duplicate IDs/anchors are reported through isSchemaValid() and schemaErrors(); allocation failure may still throw std::bad_alloc.

◆ ~pJsonSchemaValidator()

ByteDance::pJsonSchemaValidator::~pJsonSchemaValidator ( )

Destroys the compiled schema.

Member Function Documentation

◆ dialect()

const std::string & ByteDance::pJsonSchemaValidator::dialect ( ) const
noexcept

Returns the effective dialect URI selected by $schema or the option default.

◆ documentedSubsetDialectUri()

static const char * ByteDance::pJsonSchemaValidator::documentedSubsetDialectUri ( )
staticnoexcept

URI naming pjson's explicitly documented JSON Schema subset dialect.

◆ documentedSubsetVocabularyUri()

static const char * ByteDance::pJsonSchemaValidator::documentedSubsetVocabularyUri ( )
staticnoexcept

URI naming the vocabulary implemented by the subset dialect.

◆ draft2020DialectUri()

static const char * ByteDance::pJsonSchemaValidator::draft2020DialectUri ( )
staticnoexcept

URI of the supported official JSON Schema Draft 2020-12 dialect.

◆ isSchemaValid()

bool ByteDance::pJsonSchemaValidator::isSchemaValid ( ) const
noexcept

Returns whether dialect and required-vocabulary compilation succeeded.

◆ options()

const Options & ByteDance::pJsonSchemaValidator::options ( ) const
noexcept

Returns the options in effect for this validator. Construction-only resolver pointers are cleared in this snapshot.

◆ schema()

const pjson & ByteDance::pJsonSchemaValidator::schema ( ) const
noexcept

Returns the compiled schema value (read-only).

◆ schemaErrors()

const std::vector< Error > & ByteDance::pJsonSchemaValidator::schemaErrors ( ) const
noexcept

Returns immutable schema-compilation diagnostics (paths address the schema).

◆ validate() [1/2]

bool ByteDance::pJsonSchemaValidator::validate ( const pjson &  aInstance) const
noexcept

Returns whether aInstance conforms to the compiled schema. Never throws.

◆ validate() [2/2]

bool ByteDance::pJsonSchemaValidator::validate ( const pjson &  aInstance,
std::vector< Error > &  aErrors 
) const
noexcept

Validates and appends discovered failures to aErrors. If schema compilation failed, appends schemaErrors() instead. Never throws.


The documentation for this class was generated from the following file: