dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
dfx::Utils::JsonValidator Class Reference

Registry of JSON Schemas with validation helpers. More...

#include <dfx-utilities/JsonValidator.hpp>

Classes

struct  ValidationResult
 Result returned by validation functions. More...

Public Member Functions

 JsonValidator ()
 Construct an empty validator.
 JsonValidator (JsonValidator &&other)
 Move-construct a validator.
 ~JsonValidator ()
 Destroy the validator and release all registered schemas.
 DISABLE_COPY (JsonValidator)
 Copying is disabled.
JsonValidatoroperator= (JsonValidator &&other)
 Move-assign a validator.
void registerSchema (std::string_view id, nlohmann::json schema, bool allowOverride=false)
 Register a JSON Schema under an identifier.
bool isRegistered (std::string_view id) const noexcept
 Check whether a schema is registered.
void deregisterSchema (std::string_view id)
 Deregister a schema.
nlohmann::json schema (std::string_view id) const
 Retrieve the JSON Schema associated with an id.
std::vector< std::string > allSchemaIds () const
 List all registered schema identifiers.
ValidationResult validate (std::string_view id, nlohmann::json const &json, nlohmann::json::json_pointer basePath=nlohmann::json::json_pointer{}) const
 Validate a JSON document against a registered schema.

Static Public Member Functions

static ValidationResult validateOnceWithSchema (nlohmann::json schema, nlohmann::json const &json)
 Validate a JSON document against a schema without registering it.

Detailed Description

Registry of JSON Schemas with validation helpers.

A JsonValidator instance maintains an in-memory set of schemas indexed by an application-defined string identifier.

Typical usage:

validator.registerSchema("dfx.node", mySchemaJson);
auto result = validator.validate("dfx.node", inputJson);
if (!result.valid)
{
for (auto const & err : result.errors)
std::cerr << err << "\n";
}
Registry of JSON Schemas with validation helpers.
Definition JsonValidator.hpp:46
void registerSchema(std::string_view id, nlohmann::json schema, bool allowOverride=false)
Register a JSON Schema under an identifier.
ValidationResult validate(std::string_view id, nlohmann::json const &json, nlohmann::json::json_pointer basePath=nlohmann::json::json_pointer{}) const
Validate a JSON document against a registered schema.
Note
This class is movable but not copyable.

Constructor & Destructor Documentation

◆ JsonValidator() [1/2]

dfx::Utils::JsonValidator::JsonValidator ( )

Construct an empty validator.

◆ JsonValidator() [2/2]

dfx::Utils::JsonValidator::JsonValidator ( JsonValidator && other)

Move-construct a validator.

◆ ~JsonValidator()

dfx::Utils::JsonValidator::~JsonValidator ( )

Destroy the validator and release all registered schemas.

Member Function Documentation

◆ allSchemaIds()

std::vector< std::string > dfx::Utils::JsonValidator::allSchemaIds ( ) const

List all registered schema identifiers.

Returns
Vector of schema ids.

◆ deregisterSchema()

void dfx::Utils::JsonValidator::deregisterSchema ( std::string_view id)

Deregister a schema.

Parameters
idSchema id.

If id is not registered, this is typically a no-op.

◆ DISABLE_COPY()

dfx::Utils::JsonValidator::DISABLE_COPY ( JsonValidator )

Copying is disabled.

◆ isRegistered()

bool dfx::Utils::JsonValidator::isRegistered ( std::string_view id) const
noexcept

Check whether a schema is registered.

Parameters
idSchema id.
Returns
true if the schema is registered, otherwise false.

◆ operator=()

JsonValidator & dfx::Utils::JsonValidator::operator= ( JsonValidator && other)

Move-assign a validator.

◆ registerSchema()

void dfx::Utils::JsonValidator::registerSchema ( std::string_view id,
nlohmann::json schema,
bool allowOverride = false )

Register a JSON Schema under an identifier.

Parameters
idIdentifier used later in validate.
schemaJSON Schema document.
allowOverrideIf false, registering an already-registered id is an error and will throw an exception. If true, an existing schema for id will be replaced.

The schema is stored and may be compiled/cached internally to make subsequent validations faster.

◆ schema()

nlohmann::json dfx::Utils::JsonValidator::schema ( std::string_view id) const

Retrieve the JSON Schema associated with an id.

Parameters
idSchema id.
Returns
The stored schema JSON.
Exceptions
Ifid is not registered

◆ validate()

ValidationResult dfx::Utils::JsonValidator::validate ( std::string_view id,
nlohmann::json const & json,
nlohmann::json::json_pointer basePath = nlohmann::json::json_pointer{} ) const
nodiscard

Validate a JSON document against a registered schema.

Parameters
idSchema id to validate against.
jsonJSON document to validate.
basePathBase JSON pointer used as the root path for error reporting.
Returns
A ValidationResult containing success/failure and error messages.

The returned error messages are intended for diagnostics and user-facing logs.

Note
This function is marked [[nodiscard]] to encourage callers to check results.

◆ validateOnceWithSchema()

ValidationResult dfx::Utils::JsonValidator::validateOnceWithSchema ( nlohmann::json schema,
nlohmann::json const & json )
staticnodiscard

Validate a JSON document against a schema without registering it.

Parameters
schemaJSON Schema document (passed by value).
jsonJSON document to validate.
Returns
A ValidationResult containing success/failure and error messages.

This is a convenience helper for one-off validation, typically used when:

  • the schema is ephemeral,
  • registering it is unnecessary,
  • or the caller wants to validate with a schema provided at runtime.
Note
This function does not persist or cache the schema in the validator instance.
This function is marked [[nodiscard]] to encourage callers to check results.

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