dfx 0.1.0
Linux-based dynamic dataflow executor
Loading...
Searching...
No Matches
JsonValidator.hpp
1// SPDX-FileCopyrightText: 2025 Vincent Leroy
2// SPDX-License-Identifier: MIT
3//
4// This file is part of dfx.
5//
6// Licensed under the MIT License. See the LICENSE file in the project root
7// for full license information.
8
9#pragma once
10
11// Standard includes
12#include <memory>
13
14// Third-party includes
15#include <nlohmann/json.hpp>
16
17// Project includes
18#include "CopyMoveControl.hpp"
19#include "StringMap.hpp"
20
21namespace dfx::Utils
22{
46{
47 struct SchemaData;
48 using SchemaDataPtr = std::unique_ptr<SchemaData>;
49
50public:
53 {
55 bool valid = false;
57 std::vector<std::string> errors;
58 };
59
60public:
71
82 void registerSchema(std::string_view id, nlohmann::json schema, bool allowOverride = false);
88 bool isRegistered(std::string_view id) const noexcept;
94 void deregisterSchema(std::string_view id);
102 nlohmann::json schema(std::string_view id) const;
106 std::vector<std::string> allSchemaIds() const;
107
119 [[nodiscard]] ValidationResult validate(std::string_view id, nlohmann::json const & json,
120 nlohmann::json::json_pointer basePath = nlohmann::json::json_pointer{}) const;
121
122public:
137 [[nodiscard]] static ValidationResult validateOnceWithSchema(nlohmann::json schema, nlohmann::json const & json);
138
139private:
141};
142} // !namespace dfx::Utils
Convenience macros to explicitly control copy and move semantics.
DISABLE_COPY(JsonValidator)
Copying is disabled.
nlohmann::json schema(std::string_view id) const
Retrieve the JSON Schema associated with an id.
static ValidationResult validateOnceWithSchema(nlohmann::json schema, nlohmann::json const &json)
Validate a JSON document against a schema without registering it.
std::vector< std::string > allSchemaIds() const
List all registered schema identifiers.
JsonValidator & operator=(JsonValidator &&other)
Move-assign a validator.
bool isRegistered(std::string_view id) const noexcept
Check whether a schema is registered.
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.
JsonValidator(JsonValidator &&other)
Move-construct a validator.
void deregisterSchema(std::string_view id)
Deregister a schema.
~JsonValidator()
Destroy the validator and release all registered schemas.
JsonValidator()
Construct an empty validator.
Definition SystemConfigCommandHandler.hpp:15
std::unordered_map< std::string, T, TransparentHash, TransparentEqual, Allocator > UnorderedStringMap
Convenience alias for an unordered map keyed by std::string with transparent lookup.
Definition StringMap.hpp:76
Result returned by validation functions.
Definition JsonValidator.hpp:53
bool valid
Whether validation succeeded.
Definition JsonValidator.hpp:55
std::vector< std::string > errors
List of validation error messages (empty when valid is true).
Definition JsonValidator.hpp:57