@@ -58,16 +58,17 @@ YamlValidator::YamlValidator()
5858}
5959
6060YamlValidator::YamlValidator (
61- InputType input_type,
62- const std::string& schema_string)
61+ InputType input_type,
62+ const std::string& schema_string)
6363 : validator(std::make_unique<nlohmann::json_schema::json_validator>(nullptr , format_checker))
6464{
6565 this ->set_schema (input_type, schema_string);
6666}
6767
6868YamlValidator::~YamlValidator () = default ;
6969
70- YamlValidator& YamlValidator::operator =(YamlValidator&&) = default ;
70+ YamlValidator& YamlValidator::operator =(
71+ YamlValidator&&) = default ;
7172
7273void YamlValidator::set_schema (
7374 InputType input_type,
@@ -77,34 +78,34 @@ void YamlValidator::set_schema(
7778
7879 switch (input_type)
7980 {
80- case InputType::FROM_FILE : // Treat input string as the path to the file with the schema
81- try
82- {
83- std::ifstream schema_file;
84- schema_file.exceptions (std::ifstream::failbit | std::ifstream::badbit);
85- schema_file.open (schema_string);
86- schema = nlohmann::json::parse (schema_file);
87- }
88- catch (const std::exception& e)
89- {
90- throw eprosima::utils::ConfigurationException (
91- utils::Formatter () << " Error occured while loading JSON schema from file: "
92- << schema_string << " :\n " << e.what ());
93- }
94- break ;
95-
96- case InputType::FROM_STRING : // Treat input string as the raw schema
97- try
98- {
99- schema = nlohmann::json::parse (schema_string);
100- }
101- catch (const std::exception& e)
102- {
103- throw eprosima::utils::ConfigurationException (
104- utils::Formatter () << " Error occured while loading JSON schema from input string:\n "
105- << e.what ());
106- }
107- break ;
81+ case InputType::FROM_FILE : // Treat input string as the path to the file with the schema
82+ try
83+ {
84+ std::ifstream schema_file;
85+ schema_file.exceptions (std::ifstream::failbit | std::ifstream::badbit);
86+ schema_file.open (schema_string);
87+ schema = nlohmann::json::parse (schema_file);
88+ }
89+ catch (const std::exception& e)
90+ {
91+ throw eprosima::utils::ConfigurationException (
92+ utils::Formatter () << " Error occured while loading JSON schema from file: "
93+ << schema_string << " :\n " << e.what ());
94+ }
95+ break ;
96+
97+ case InputType::FROM_STRING : // Treat input string as the raw schema
98+ try
99+ {
100+ schema = nlohmann::json::parse (schema_string);
101+ }
102+ catch (const std::exception& e)
103+ {
104+ throw eprosima::utils::ConfigurationException (
105+ utils::Formatter () << " Error occured while loading JSON schema from input string:\n "
106+ << e.what ());
107+ }
108+ break ;
108109 }
109110
110111 // Set the validator schema
@@ -149,77 +150,78 @@ bool YamlValidator::validate_YAML(
149150
150151 // Create lambda function to convert from YAML to JSON
151152 auto yaml_to_json = [](auto && self, const Yaml& yml) -> nlohmann::json
152- {
153- if (yml.IsNull ())
154- {
155- return nullptr ;
156- }
157-
158- if (yml.IsScalar ())
159- {
160- // The only allowed scalar types are: boolean, integer, number (double) and string
161- const std::string value = yml.as <std::string>();
162-
163- if (value == " true" )
164153 {
165- return true ;
166- }
167- if (value == " false" )
168- {
169- return false ;
170- }
171-
172- try
173- {
174- return yml.as <int >();
175- } catch (...)
176- {
177- }
178- try
179- {
180- return yml.as <double >();
181- } catch (...)
182- {
183- }
184-
185- return value;
186- }
187-
188- if (yml.IsSequence ())
189- {
190- nlohmann::json array = nlohmann::json::array ();
191- for (const auto & item : yml)
192- {
193- array.push_back (self (self, item));
194- }
195- return array;
196- }
197-
198- if (yml.IsMap ())
199- {
200- nlohmann::json object = nlohmann::json::object ();
201- for (const auto & item : yml)
202- {
203- std::string key = item.first .as <std::string>();
204- object[key] = self (self, item.second );
205- }
206- return object;
207- }
208-
209- std::string yml_as_string;
210- try
211- {
212- yml_as_string = yml.as <std::string>();
213- }
214- catch (...)
215- {
216- throw eprosima::utils::ConfigurationException (" Unsupported YAML file, cannot be converted to JSON." );
217- }
218-
219- throw eprosima::utils::ConfigurationException (
220- utils::Formatter () << " Unsupported YAML file, cannot be converted to JSON.\n "
221- << " Error in node: " << yml_as_string);
222- };
154+ if (yml.IsNull ())
155+ {
156+ return nullptr ;
157+ }
158+
159+ if (yml.IsScalar ())
160+ {
161+ // The only allowed scalar types are: boolean, integer, number (double) and string
162+ const std::string value = yml.as <std::string>();
163+
164+ if (value == " true" )
165+ {
166+ return true ;
167+ }
168+ if (value == " false" )
169+ {
170+ return false ;
171+ }
172+
173+ try
174+ {
175+ return yml.as <int >();
176+ } catch (...)
177+ {
178+ }
179+ try
180+ {
181+ return yml.as <double >();
182+ } catch (...)
183+ {
184+ }
185+
186+ return value;
187+ }
188+
189+ if (yml.IsSequence ())
190+ {
191+ nlohmann::json array = nlohmann::json::array ();
192+ for (const auto & item : yml)
193+ {
194+ array.push_back (self (self, item));
195+ }
196+ return array;
197+ }
198+
199+ if (yml.IsMap ())
200+ {
201+ nlohmann::json object = nlohmann::json::object ();
202+ for (const auto & item : yml)
203+ {
204+ std::string key = item.first .as <std::string>();
205+ object[key] = self (self, item.second );
206+ }
207+ return object;
208+ }
209+
210+ std::string yml_as_string;
211+ try
212+ {
213+ yml_as_string = yml.as <std::string>();
214+ }
215+ catch (...)
216+ {
217+ throw eprosima::utils::ConfigurationException (
218+ " Unsupported YAML file, cannot be converted to JSON." );
219+ }
220+
221+ throw eprosima::utils::ConfigurationException (
222+ utils::Formatter () << " Unsupported YAML file, cannot be converted to JSON.\n "
223+ << " Error in node: " << yml_as_string);
224+ };
223225
224226 // Covert YAML to JSON
225227 nlohmann::json instance = yaml_to_json (yaml_to_json, yml);
0 commit comments