Skip to content

Commit 6d22ebb

Browse files
committed
src: add JSON Schema validation for node.config.json
Validate the configuration file against its JSON Schema using ata-validator as a supplementary check after the existing parser. This provides a safety net for type errors the parser might miss. Refs: #62598
1 parent 5573d73 commit 6d22ebb

2 files changed

Lines changed: 1061 additions & 0 deletions

File tree

src/node_config_file.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "node_config_file.h"
2+
#include "ata.h"
23
#include "debug_utils-inl.h"
4+
#include "node_config_schema.h"
35
#include "simdjson.h"
46

57
#include <string>
@@ -323,6 +325,34 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
323325
}
324326
}
325327

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(stderr,
338+
"Invalid configuration in %s:\n",
339+
config_path.data());
340+
for (const auto& e : result.errors) {
341+
if (e.code !=
342+
ata::error_code::additional_property_not_allowed) {
343+
FPrintF(stderr,
344+
" %s: %s\n",
345+
e.path.empty() ? "/" : e.path,
346+
e.message);
347+
}
348+
}
349+
return ParseResult::InvalidContent;
350+
}
351+
}
352+
}
353+
}
354+
}
355+
326356
return ParseResult::Valid;
327357
}
328358

0 commit comments

Comments
 (0)