GeezerVison is showing github.com — src: make node.config.json throw at unknown fields · nodejs/node@ba36ab7
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/node_config_file.cc +7-2Lines changed: 7 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,6 +8,7 @@ constexpr std::string_view kConfigFileFlag = "--experimental-config-file"; | 8 | 8 | constexpr std::string_view kDefaultConfigFileFlag = | 9 | 9 | "--experimental-default-config-file"; | 10 | 10 | constexpr std::string_view kDefaultConfigFileName = "node.config.json"; | | 11 | +constexpr std::string_view kSchemaField = "$schema"; | 11 | 12 |
| 12 | 13 | inline bool HasEqualsPrefix(std::string_view arg, std::string_view flag) { | 13 | 14 | return arg.size() > flag.size() && arg.starts_with(flag) && | @@ -287,10 +288,14 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) { | 287 | 288 | return ParseResult::InvalidContent; | 288 | 289 | } | 289 | 290 |
| | 291 | + if (namespace_name == kSchemaField) { | | 292 | + continue; | | 293 | + } | | 294 | + | 290 | 295 | // Check if this field is a valid namespace | 291 | 296 | if (!valid_namespaces.contains(namespace_name)) { | 292 | | - // If not, skip it | 293 | | - continue; | | 297 | + FPrintF(stderr, "Unknown namespace %s\n", namespace_name); | | 298 | + return ParseResult::InvalidContent; | 294 | 299 | } | 295 | 300 |
| 296 | 301 | // Get the namespace object | |
Collapse file Copy file name to clipboard +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-config-file.js +14-2Lines changed: 14 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -516,14 +516,26 @@ describe('namespace-scoped options', () => { | 516 | 516 | assert.strictEqual(result.code, 9); | 517 | 517 | }); | 518 | 518 |
| 519 | | - it('should not throw an error when a namespace is not recognised', async () => { | | 519 | + it('should throw an error when a namespace is not recognised', async () => { | 520 | 520 | const result = await spawnPromisified(process.execPath, [ | 521 | 521 | '--no-warnings', | 522 | 522 | `--experimental-config-file=${fixtures.path('rc/unknown-namespace.json')}`, | 523 | 523 | '-p', '"Hello, World!"', | 524 | 524 | ]); | | 525 | + assert.match(result.stderr, /Unknown namespace an-invalid-namespace/); | | 526 | + assert.match(result.stderr, /unknown-namespace\.json: invalid content/); | | 527 | + assert.strictEqual(result.stdout, ''); | | 528 | + assert.strictEqual(result.code, 9); | | 529 | + }); | | 530 | + | | 531 | + it('should allow the $schema field', async () => { | | 532 | + const result = await spawnPromisified(process.execPath, [ | | 533 | + '--no-warnings', | | 534 | + `--experimental-config-file=${fixtures.path('rc/schema.json')}`, | | 535 | + '-p', 'http.maxHeaderSize', | | 536 | + ]); | 525 | 537 | assert.strictEqual(result.stderr, ''); | 526 | | - assert.strictEqual(result.stdout, 'Hello, World!\n'); | | 538 | + assert.strictEqual(result.stdout, '10\n'); | 527 | 539 | assert.strictEqual(result.code, 0); | 528 | 540 | }); | 529 | 541 |
| |
|