Skip to content

Commit 8923655

Browse files
src: make node.config.json throw at unknown fields
Signed-off-by: Marco Ippolito <[email protected]>
1 parent 9c4ca0a commit 8923655

3 files changed

Lines changed: 27 additions & 4 deletions

File tree

src/node_config_file.cc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ constexpr std::string_view kConfigFileFlag = "--experimental-config-file";
88
constexpr std::string_view kDefaultConfigFileFlag =
99
"--experimental-default-config-file";
1010
constexpr std::string_view kDefaultConfigFileName = "node.config.json";
11+
constexpr std::string_view kSchemaField = "$schema";
1112

1213
inline bool HasEqualsPrefix(std::string_view arg, std::string_view flag) {
1314
return arg.size() > flag.size() && arg.starts_with(flag) &&
@@ -290,10 +291,14 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
290291
return ParseResult::InvalidContent;
291292
}
292293

294+
if (namespace_name == kSchemaField) {
295+
continue;
296+
}
297+
293298
// Check if this field is a valid namespace
294299
if (!valid_namespaces.contains(namespace_name)) {
295-
// If not, skip it
296-
continue;
300+
FPrintF(stderr, "Unknown namespace %s\n", namespace_name);
301+
return ParseResult::InvalidContent;
297302
}
298303

299304
// List of implicit namespace flags

test/fixtures/rc/schema.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"$schema": "https://nodejs.org/dist/vX.Y.Z/docs/node-config-schema.json",
3+
"nodeOptions": {
4+
"max-http-header-size": 10
5+
}
6+
}

test/parallel/test-config-file.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,26 @@ describe('namespace-scoped options', () => {
517517
assert.strictEqual(result.code, 9);
518518
});
519519

520-
it('should not throw an error when a namespace is not recognised', async () => {
520+
it('should throw an error when a namespace is not recognised', async () => {
521521
const result = await spawnPromisified(process.execPath, [
522522
'--no-warnings',
523523
`--experimental-config-file=${fixtures.path('rc/unknown-namespace.json')}`,
524524
'-p', '"Hello, World!"',
525525
]);
526+
assert.match(result.stderr, /Unknown namespace an-invalid-namespace/);
527+
assert.match(result.stderr, /unknown-namespace\.json: invalid content/);
528+
assert.strictEqual(result.stdout, '');
529+
assert.strictEqual(result.code, 9);
530+
});
531+
532+
it('should allow the $schema field', async () => {
533+
const result = await spawnPromisified(process.execPath, [
534+
'--no-warnings',
535+
`--experimental-config-file=${fixtures.path('rc/schema.json')}`,
536+
'-p', 'http.maxHeaderSize',
537+
]);
526538
assert.strictEqual(result.stderr, '');
527-
assert.strictEqual(result.stdout, 'Hello, World!\n');
539+
assert.strictEqual(result.stdout, '10\n');
528540
assert.strictEqual(result.code, 0);
529541
});
530542

0 commit comments

Comments
 (0)