Phosphor
Qt6 / Wayland library suite for window-management tools
 
Loading...
Searching...
No Matches
PhosphorFsLoader::SchemaValidator Class Reference

Validates parsed JSON documents against a JSON Schema (Draft 7). More...

#include <phosphor-fsloader/include/PhosphorFsLoader/SchemaValidator.h>

Classes

struct  Error
 A single schema violation. More...
 

Public Member Functions

 SchemaValidator (const QByteArray &schemaJson)
 Compile a JSON Schema from its serialized bytes.
 
 ~SchemaValidator ()
 
 SchemaValidator (SchemaValidator &&) noexcept
 
SchemaValidatoroperator= (SchemaValidator &&) noexcept
 
 SchemaValidator (const SchemaValidator &)=delete
 
SchemaValidatoroperator= (const SchemaValidator &)=delete
 
bool isValid () const
 True when the schema compiled successfully and the validator is usable.
 
std::optional< QList< Error > > validate (const QJsonValue &document) const
 Validate a parsed JSON document against the compiled schema.
 

Static Public Member Functions

static SchemaValidator fromResource (const QString &resourcePath, const QLoggingCategory &category)
 Build a validator from a Qt resource (or file) path, failing closed.
 

Detailed Description

Validates parsed JSON documents against a JSON Schema (Draft 7).

Thin, dependency-free wrapper over the vendored valijson engine. The schema-validation library (valijson + its Qt adapter) is an implementation detail confined to the .cpp; this header exposes only Qt/std types so the engine never leaks into the public ABI or into consumers' include closure.

Construct one validator per document type from the serialized schema bytes (typically a schema embedded as a Qt resource), compile it once, and reuse it across every file of that type — schema compilation is the expensive step and validate() is cheap to call repeatedly.

Failure model is fail-closed. If the schema bytes are themselves malformed, isValid() returns false and every validate() call reports a single error rather than silently passing documents through unvalidated.

Constructor & Destructor Documentation

◆ SchemaValidator() [1/3]

PhosphorFsLoader::SchemaValidator::SchemaValidator ( const QByteArray &  schemaJson)
explicit

Compile a JSON Schema from its serialized bytes.

Parameters
schemaJsonUTF-8 JSON text of a Draft 7 schema. A malformed or non-object schema leaves the validator in an invalid state (isValid() == false) instead of throwing.

◆ ~SchemaValidator()

PhosphorFsLoader::SchemaValidator::~SchemaValidator ( )

◆ SchemaValidator() [2/3]

PhosphorFsLoader::SchemaValidator::SchemaValidator ( SchemaValidator &&  )
noexcept

◆ SchemaValidator() [3/3]

PhosphorFsLoader::SchemaValidator::SchemaValidator ( const SchemaValidator )
delete

Member Function Documentation

◆ fromResource()

static SchemaValidator PhosphorFsLoader::SchemaValidator::fromResource ( const QString &  resourcePath,
const QLoggingCategory &  category 
)
static

Build a validator from a Qt resource (or file) path, failing closed.

Opens resourcePath and compiles the schema from its bytes. If the resource cannot be opened — for an RCC-embedded schema this means a build regression — it logs a warning at category and returns a validator whose isValid() is false, so every validate() call rejects rather than silently passing documents through unvalidated. The single place the embed-and-fail-closed contract lives, shared by every loader.

◆ isValid()

bool PhosphorFsLoader::SchemaValidator::isValid ( ) const

True when the schema compiled successfully and the validator is usable.

◆ operator=() [1/2]

SchemaValidator & PhosphorFsLoader::SchemaValidator::operator= ( const SchemaValidator )
delete

◆ operator=() [2/2]

SchemaValidator & PhosphorFsLoader::SchemaValidator::operator= ( SchemaValidator &&  )
noexcept

◆ validate()

std::optional< QList< Error > > PhosphorFsLoader::SchemaValidator::validate ( const QJsonValue &  document) const

Validate a parsed JSON document against the compiled schema.

Parameters
documentAny parsed JSON value (typically a QJsonObject).
Returns
std::nullopt when the document satisfies the schema; otherwise the non-empty list of violations. A validator whose schema failed to compile always returns a one-element list.

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