Skip to content

Commit 0f1017d

Browse files
committed
src: run schema validation before option parsing
1 parent ddf8af9 commit 0f1017d

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

src/node_config_file.cc

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,33 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
222222
return ParseResult::FileError;
223223
}
224224

225+
// Validate against JSON Schema before parsing options.
226+
// ata works on the raw string independently of simdjson,
227+
// so no re-parse is needed.
228+
{
229+
auto schema = ata::compile(kNodeConfigSchema);
230+
if (schema) {
231+
auto result = ata::validate(schema, file_content);
232+
if (!result.valid) {
233+
for (const auto& err : result.errors) {
234+
if (err.code != ata::error_code::additional_property_not_allowed) {
235+
FPrintF(
236+
stderr, "Invalid configuration in %s:\n", config_path.data());
237+
for (const auto& e : result.errors) {
238+
if (e.code != ata::error_code::additional_property_not_allowed) {
239+
FPrintF(stderr,
240+
" %s: %s\n",
241+
e.path.empty() ? "/" : e.path,
242+
e.message);
243+
}
244+
}
245+
return ParseResult::InvalidContent;
246+
}
247+
}
248+
}
249+
}
250+
}
251+
225252
// Parse the configuration file
226253
simdjson::ondemand::parser json_parser;
227254
simdjson::ondemand::document document;
@@ -325,32 +352,6 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
325352
}
326353
}
327354

328-
// Validate against JSON Schema. Skip additionalProperties errors
329-
// because unknown namespaces are intentionally ignored.
330-
{
331-
auto schema = ata::compile(kNodeConfigSchema);
332-
if (schema) {
333-
auto result = ata::validate(schema, file_content);
334-
if (!result.valid) {
335-
for (const auto& err : result.errors) {
336-
if (err.code != ata::error_code::additional_property_not_allowed) {
337-
FPrintF(
338-
stderr, "Invalid configuration in %s:\n", config_path.data());
339-
for (const auto& e : result.errors) {
340-
if (e.code != ata::error_code::additional_property_not_allowed) {
341-
FPrintF(stderr,
342-
" %s: %s\n",
343-
e.path.empty() ? "/" : e.path,
344-
e.message);
345-
}
346-
}
347-
return ParseResult::InvalidContent;
348-
}
349-
}
350-
}
351-
}
352-
}
353-
354355
return ParseResult::Valid;
355356
}
356357

0 commit comments

Comments
 (0)