Registry of JSON Schemas with validation helpers.
More...
#include <dfx-utilities/JsonValidator.hpp>
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:
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.
◆ 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.
◆ 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
-
If id is not registered, this is typically a no-op.
◆ DISABLE_COPY()
◆ isRegistered()
| bool dfx::Utils::JsonValidator::isRegistered |
( |
std::string_view | id | ) |
const |
|
noexcept |
Check whether a schema is registered.
- Parameters
-
- Returns
true if the schema is registered, otherwise false.
◆ operator=()
◆ registerSchema()
| void dfx::Utils::JsonValidator::registerSchema |
( |
std::string_view | id, |
|
|
nlohmann::json | schema, |
|
|
bool | allowOverride = false ) |
Register a JSON Schema under an identifier.
- Parameters
-
| id | Identifier used later in validate. |
| schema | JSON Schema document. |
| allowOverride | If 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
-
- Returns
- The stored schema JSON.
- Exceptions
-
◆ 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
-
| id | Schema id to validate against. |
| json | JSON document to validate. |
| basePath | Base 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
-
| schema | JSON Schema document (passed by value). |
| json | JSON 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: