Skip to content

Commit b06277a

Browse files
committed
Mode dependency from nlomann to CPP
Signed-off-by: David Laseca Perez <davidlaseca@eprosima.com>
1 parent c4d5cb8 commit b06277a

3 files changed

Lines changed: 190 additions & 174 deletions

File tree

ddspipe_yaml/include/ddspipe_yaml/YamlValidator.hpp

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,98 +14,120 @@
1414

1515
#pragma once
1616

17+
#include <memory>
18+
1719
#include <ddspipe_yaml/library/library_dll.h>
1820
#include <ddspipe_yaml/Yaml.hpp>
1921

20-
#include <nlohmann/json.hpp>
21-
#include <nlohmann/json-schema.hpp>
22+
namespace nlohmann {
23+
namespace json_schema {
24+
class json_validator;
25+
} // namespace json_schema
26+
} // namespace nlohmann
2227

2328
namespace eprosima {
2429
namespace ddspipe {
2530
namespace yaml {
2631

2732
/**
28-
* @brief Yaml Validator
33+
* @brief Validates YAML objects against a JSON Schema (draft-07).
2934
*
30-
* This class is used to validate Yaml objects.
35+
* Loads a schema from a file path or a raw string via \c InputType, then
36+
* validates \c Yaml nodes against it. Custom format checkers (e.g. IPv4/IPv6)
37+
* are applied automatically during validation.
3138
*/
3239
class DDSPIPE_YAML_DllAPI YamlValidator
3340
{
3441
private:
3542

36-
nlohmann::json_schema::json_validator validator;
37-
38-
/**
39-
* @brief Convert \c yml to a nlohmann::json type
40-
*
41-
* @param yml Yaml object to convert.
42-
*/
43-
nlohmann::json yaml_to_json(
44-
const Yaml& yml);
43+
std::unique_ptr<nlohmann::json_schema::json_validator> validator;
4544

4645
protected:
4746

4847
/**
49-
* @brief Used to allow checking formats, for example, a string with an IP address
48+
* @brief Custom format checker registered with the JSON Schema validator.
5049
*
51-
* @param format Name of the format option (i.e. IPv4 or IPv6).
52-
* @param value Value being checked against the \c format type.
50+
* Called automatically during validation when the schema uses the \c format keyword.
51+
* Supported format values are \c "v4" (IPv4) and \c "v6" (IPv6).
52+
*
53+
* @param format Name of the format to check (e.g. \c "v4" or \c "v6").
54+
* @param value Value to validate against the given format.
55+
*
56+
* @throws std::invalid_argument if \c value does not conform to \c format.
5357
*/
5458
static void format_checker(
5559
const std::string& format,
5660
const std::string& value);
5761

5862
public:
63+
64+
enum class InputType
65+
{
66+
FROM_STRING,
67+
FROM_FILE
68+
};
5969

6070
/**
61-
* @brief Default constructor. Creates a \c YamlValidator with an empty schema validator.
71+
* @brief Default constructor. Creates a \c YamlValidator with no schema loaded.
72+
*
73+
* \c validate_YAML will return \c false until a schema is set via \c set_schema.
6274
*/
6375
YamlValidator();
6476

6577
/**
66-
* @brief Construct a \c YamlValidator and load the given JSON schema.
78+
* @brief Construct a \c YamlValidator and load a JSON Schema (draft-07).
79+
*
80+
* @param input_type Whether \c schema_string is a file path (\c InputType::FROM_FILE)
81+
* or raw JSON content (\c InputType::FROM_STRING).
82+
* @param schema_string File path or raw JSON string of the schema to load.
6783
*
68-
* @param schema JSON schema to set as the root schema of the validator.
84+
* @throws utils::ConfigurationException if the schema cannot be read, parsed, or is not
85+
* a valid JSON Schema draft-07.
6986
*/
7087
explicit YamlValidator(
71-
const nlohmann::json& schema);
88+
InputType input_type,
89+
const std::string& schema_string);
7290

7391
/**
74-
* @brief Set or replace the root schema of the validator.
75-
*
76-
* @param schema JSON schema to set as the root schema of the validator.
92+
* @brief Default destructor. Needed because \c std::unique_ptr needs the full definition
93+
* of \c nlohmann::json_schema::json_validator when destroying it.
7794
*/
78-
void set_schema(
79-
const nlohmann::json& schema);
95+
~YamlValidator();
8096

8197
/**
82-
* @brief Load a JSON schema from a file path.
83-
*
84-
* @param schema_path Path to the JSON schema file.
85-
* @return Parsed \c nlohmann::json object representing the schema.
98+
* @brief Move assignment operator. Needed because \c std::unique_ptr needs the full definition
99+
* of \c nlohmann::json_schema::json_validator when move-assigning (the old value is destroyed).
86100
*/
87-
static nlohmann::json from_file(
88-
const std::string& schema_path);
101+
YamlValidator& operator=(YamlValidator&&);
89102

90103
/**
91-
* @brief Load a JSON schema from a string containing the schema content directly.
104+
* @brief Set or replace the root schema of the validator.
92105
*
93-
* @param schema_file_content String with the JSON schema content.
94-
* @return Parsed \c nlohmann::json object representing the schema.
106+
* @param input_type Whether \c schema_string is a file path (\c InputType::FROM_FILE)
107+
* or raw JSON content (\c InputType::FROM_STRING).
108+
* @param schema_string File path or raw JSON string of the schema to load.
109+
*
110+
* @throws utils::ConfigurationException if the schema cannot be read, parsed, or is not
111+
* a valid JSON Schema draft-07.
95112
*/
96-
static nlohmann::json from_string(
97-
const std::string& schema_file_content);
113+
void set_schema(
114+
InputType input_type,
115+
const std::string& schema_string);
98116

99117
/**
100118
* @brief Validate a YAML object against the loaded schema.
101119
*
120+
* Returns \c false immediately if no schema has been loaded.
121+
*
102122
* @param yml Yaml object to validate.
103-
* @param print_errors Flag to print the errors in the error output when \c yml doesn't fit the schema.
123+
* @param display_errors If \c true, prints validation errors to \c stderr.
104124
* @return \c true if \c yml conforms to the schema, \c false otherwise.
125+
*
126+
* @throws utils::ConfigurationException if \c yml cannot be converted to JSON.
105127
*/
106128
bool validate_YAML(
107129
const Yaml& yml,
108-
bool print_errors = true);
130+
bool display_errors = true);
109131
};
110132

111133
} /* namespace yaml */

0 commit comments

Comments
 (0)