mirror of
https://github.com/nodejs/node.git
synced 2026-11-24 21:50:36 -05:00
src: make node.config.json throw at unknown fields
Signed-off-by: Marco Ippolito <marcoippolito54@gmail.com> PR-URL: https://github.com/nodejs/node/pull/62992 Reviewed-By: Pietro Marchini <pietro.marchini94@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
This commit is contained in:
1 parent
49198d2313
commit
ba36ab71fd
3 files changed
+27
-4
No files matched your search
@@ -8,6 +8,7 @@ constexpr std::string_view kConfigFileFlag = "--experimental-config-file";
|
||||
constexpr std::string_view kDefaultConfigFileFlag =
|
||||
"--experimental-default-config-file";
|
||||
constexpr std::string_view kDefaultConfigFileName = "node.config.json";
|
||||
constexpr std::string_view kSchemaField = "$schema";
|
||||
|
||||
inline bool HasEqualsPrefix(std::string_view arg, std::string_view flag) {
|
||||
return arg.size() > flag.size() && arg.starts_with(flag) &&
|
||||
@@ -287,10 +288,14 @@ ParseResult ConfigReader::ParseConfig(const std::string_view& config_path) {
|
||||
return ParseResult::InvalidContent;
|
||||
}
|
||||
|
||||
if (namespace_name == kSchemaField) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if this field is a valid namespace
|
||||
if (!valid_namespaces.contains(namespace_name)) {
|
||||
// If not, skip it
|
||||
continue;
|
||||
FPrintF(stderr, "Unknown namespace %s\n", namespace_name);
|
||||
return ParseResult::InvalidContent;
|
||||
}
|
||||
|
||||
// Get the namespace object
|
||||
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"$schema": "https://nodejs.org/dist/vX.Y.Z/docs/node-config-schema.json",
|
||||
"nodeOptions": {
|
||||
"max-http-header-size": 10
|
||||
}
|
||||
}
|
||||
@@ -516,14 +516,26 @@ describe('namespace-scoped options', () => {
|
||||
assert.strictEqual(result.code, 9);
|
||||
});
|
||||
|
||||
it('should not throw an error when a namespace is not recognised', async () => {
|
||||
it('should throw an error when a namespace is not recognised', async () => {
|
||||
const result = await spawnPromisified(process.execPath, [
|
||||
'--no-warnings',
|
||||
`--experimental-config-file=${fixtures.path('rc/unknown-namespace.json')}`,
|
||||
'-p', '"Hello, World!"',
|
||||
]);
|
||||
assert.match(result.stderr, /Unknown namespace an-invalid-namespace/);
|
||||
assert.match(result.stderr, /unknown-namespace\.json: invalid content/);
|
||||
assert.strictEqual(result.stdout, '');
|
||||
assert.strictEqual(result.code, 9);
|
||||
});
|
||||
|
||||
it('should allow the $schema field', async () => {
|
||||
const result = await spawnPromisified(process.execPath, [
|
||||
'--no-warnings',
|
||||
`--experimental-config-file=${fixtures.path('rc/schema.json')}`,
|
||||
'-p', 'http.maxHeaderSize',
|
||||
]);
|
||||
assert.strictEqual(result.stderr, '');
|
||||
assert.strictEqual(result.stdout, 'Hello, World!\n');
|
||||
assert.strictEqual(result.stdout, '10\n');
|
||||
assert.strictEqual(result.code, 0);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user