|
14 | 14 |
|
15 | 15 | #pragma once |
16 | 16 |
|
| 17 | +#include <memory> |
| 18 | + |
17 | 19 | #include <ddspipe_yaml/library/library_dll.h> |
18 | 20 | #include <ddspipe_yaml/Yaml.hpp> |
19 | 21 |
|
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 |
22 | 27 |
|
23 | 28 | namespace eprosima { |
24 | 29 | namespace ddspipe { |
25 | 30 | namespace yaml { |
26 | 31 |
|
27 | 32 | /** |
28 | | - * @brief Yaml Validator |
| 33 | + * @brief Validates YAML objects against a JSON Schema (draft-07). |
29 | 34 | * |
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. |
31 | 38 | */ |
32 | 39 | class DDSPIPE_YAML_DllAPI YamlValidator |
33 | 40 | { |
34 | 41 | private: |
35 | 42 |
|
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; |
45 | 44 |
|
46 | 45 | protected: |
47 | 46 |
|
48 | 47 | /** |
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. |
50 | 49 | * |
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. |
53 | 57 | */ |
54 | 58 | static void format_checker( |
55 | 59 | const std::string& format, |
56 | 60 | const std::string& value); |
57 | 61 |
|
58 | 62 | public: |
| 63 | + |
| 64 | + enum class InputType |
| 65 | + { |
| 66 | + FROM_STRING, |
| 67 | + FROM_FILE |
| 68 | + }; |
59 | 69 |
|
60 | 70 | /** |
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. |
62 | 74 | */ |
63 | 75 | YamlValidator(); |
64 | 76 |
|
65 | 77 | /** |
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. |
67 | 83 | * |
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. |
69 | 86 | */ |
70 | 87 | explicit YamlValidator( |
71 | | - const nlohmann::json& schema); |
| 88 | + InputType input_type, |
| 89 | + const std::string& schema_string); |
72 | 90 |
|
73 | 91 | /** |
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. |
77 | 94 | */ |
78 | | - void set_schema( |
79 | | - const nlohmann::json& schema); |
| 95 | + ~YamlValidator(); |
80 | 96 |
|
81 | 97 | /** |
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). |
86 | 100 | */ |
87 | | - static nlohmann::json from_file( |
88 | | - const std::string& schema_path); |
| 101 | + YamlValidator& operator=(YamlValidator&&); |
89 | 102 |
|
90 | 103 | /** |
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. |
92 | 105 | * |
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. |
95 | 112 | */ |
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); |
98 | 116 |
|
99 | 117 | /** |
100 | 118 | * @brief Validate a YAML object against the loaded schema. |
101 | 119 | * |
| 120 | + * Returns \c false immediately if no schema has been loaded. |
| 121 | + * |
102 | 122 | * @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. |
104 | 124 | * @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. |
105 | 127 | */ |
106 | 128 | bool validate_YAML( |
107 | 129 | const Yaml& yml, |
108 | | - bool print_errors = true); |
| 130 | + bool display_errors = true); |
109 | 131 | }; |
110 | 132 |
|
111 | 133 | } /* namespace yaml */ |
|
0 commit comments