Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

Commit 34e52aa

Browse files
authored
Refactor schema loading and validation (#230)
* Refactor schema loading and validation Now when a schema is loaded a validator for it is instantiated as well Then validators can then be re-used instead of instantiating new ones for the same schema multiple times * Remove redundant unique_ptrs in Validators struct * Turn load_schema and load_schemas into free functions * load_schema and load_schemas: clang-format Signed-off-by: Kai-Uwe Hermann <kai-uwe.hermann@pionix.de>
1 parent f0d3731 commit 34e52aa

File tree

3 files changed

+149
-128
lines changed

3 files changed

+149
-128
lines changed

include/utils/config.hpp

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,27 @@ struct RuntimeSettings;
3030
///
3131
/// \brief A structure that contains all available schemas
3232
///
33-
struct schemas {
33+
struct Schemas {
3434
nlohmann::json config; ///< The config schema
3535
nlohmann::json manifest; ///< The manifest scheme
3636
nlohmann::json interface; ///< The interface schema
3737
nlohmann::json type; ///< The type schema
3838
nlohmann::json error_declaration_list; ///< The error-declaration-list schema
3939
};
4040

41+
struct Validators {
42+
nlohmann::json_schema::json_validator config;
43+
nlohmann::json_schema::json_validator manifest;
44+
nlohmann::json_schema::json_validator type;
45+
nlohmann::json_schema::json_validator interface;
46+
nlohmann::json_schema::json_validator error_declaration_list;
47+
};
48+
49+
struct SchemaValidation {
50+
Schemas schemas;
51+
Validators validators;
52+
};
53+
4154
///
4255
/// \brief Allowed format of a type URI, which are of a format like this /type_file_name#/TypeName
4356
///
@@ -50,6 +63,30 @@ struct ImplementationInfo {
5063
std::string impl_intf;
5164
};
5265

66+
///
67+
/// \brief A simple json schema loader that uses the builtin draft7 schema of
68+
/// the json schema validator when it encounters it, throws an exception
69+
/// otherwise
70+
void loader(const nlohmann::json_uri& uri, nlohmann::json& schema);
71+
72+
///
73+
/// \brief An extension to the default format checker of the json schema
74+
/// validator supporting uris
75+
void format_checker(const std::string& format, const std::string& value);
76+
77+
///
78+
/// \brief loads and validates a json schema at the provided \p path
79+
///
80+
/// \returns the loaded json schema as a json object as well as a related schema validator
81+
std::tuple<nlohmann::json, nlohmann::json_schema::json_validator> load_schema(const fs::path& path);
82+
83+
///
84+
/// \brief loads the config.json and manifest.json in the schemes subfolder of
85+
/// the provided \p schemas_dir
86+
///
87+
/// \returns the loaded configs and related validators
88+
SchemaValidation load_schemas(const fs::path& schemas_dir);
89+
5390
///
5491
/// \brief Base class for configs
5592
///
@@ -62,7 +99,7 @@ class ConfigBase {
6299
nlohmann::json interfaces;
63100
nlohmann::json interface_definitions;
64101
nlohmann::json types;
65-
schemas _schemas;
102+
Schemas schemas;
66103

67104
std::unordered_map<std::string, ModuleTierMappings> tier_mappings;
68105
// experimental caches
@@ -190,6 +227,8 @@ class ManagerConfig : public ConfigBase {
190227
private:
191228
const ManagerSettings& ms;
192229
std::unordered_map<std::string, std::optional<TelemetryConfig>> telemetry_configs;
230+
Validators validators;
231+
std::unique_ptr<nlohmann::json_schema::json_validator> draft7_validator;
193232

194233
///
195234
/// \brief loads and validates the manifest of the module \p module_id using the provided \p module config
@@ -328,19 +367,6 @@ class Config : public ConfigBase {
328367
/// otherwise
329368
void ref_loader(const nlohmann::json_uri& uri, nlohmann::json& schema);
330369

331-
///
332-
/// \brief loads the config.json and manifest.json in the schemes subfolder of
333-
/// the provided \p schemas_dir
334-
///
335-
/// \returns the config and manifest schemas
336-
static schemas load_schemas(const fs::path& schemas_dir);
337-
338-
///
339-
/// \brief loads and validates a json schema at the provided \p path
340-
///
341-
/// \returns the loaded json schema as a json object
342-
static nlohmann::json load_schema(const fs::path& path);
343-
344370
///
345371
/// \brief loads all module manifests relative to the \p main_dir
346372
///
@@ -352,25 +378,14 @@ class Config : public ConfigBase {
352378
///
353379
/// \returns a set of object keys
354380
static std::set<std::string> keys(const nlohmann::json& object);
355-
356-
///
357-
/// \brief A simple json schema loader that uses the builtin draft7 schema of
358-
/// the json schema validator when it encounters it, throws an exception
359-
/// otherwise
360-
static void loader(const nlohmann::json_uri& uri, nlohmann::json& schema);
361-
362-
///
363-
/// \brief An extension to the default format checker of the json schema
364-
/// validator supporting uris
365-
static void format_checker(const std::string& format, const std::string& value);
366381
};
367382
} // namespace Everest
368383

369384
NLOHMANN_JSON_NAMESPACE_BEGIN
370-
template <> struct adl_serializer<Everest::schemas> {
371-
static void to_json(nlohmann::json& j, const Everest::schemas& s);
385+
template <> struct adl_serializer<Everest::Schemas> {
386+
static void to_json(nlohmann::json& j, const Everest::Schemas& s);
372387

373-
static void from_json(const nlohmann::json& j, Everest::schemas& s);
388+
static void from_json(const nlohmann::json& j, Everest::Schemas& s);
374389
};
375390
NLOHMANN_JSON_NAMESPACE_END
376391

0 commit comments

Comments
 (0)