mozilla / node-convict

Featureful configuration management library for Node.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

custom formats override set order

gjbaxter opened this issue · comments

I have a custom format that loads values from a set of secrets.

testit: {
    format: 'secret-value',
    default: 'DATABASE_PASSWORD',
},

This will use the default value and retrieve the value from the secrets. All well and good.

I also load a json5 file to provide for local overriding. This works fine if testit were a 'String' value.

E.g.
testit: {
format: 'String',
default: 'not overridden',
},

json5 file:
{
testit: "override!"
}

In this case, configuration.getProperties().testit == 'override!'.

But.. again, if the format is 'secret-value', the testit value is NOT 'override!', but whatever value is coming from the secrets.

Here's the configuration code (roughly):

const config = convict(schema);
config.loadFile(filePath);
const configuration = config.getProperties();

console.log(configuration.testit);

undefined (secret didn't have the value)

On further digging.. it turns out that loading the json5 file actually removes whatever was populated previously. Thus if config has a value before loadFile(), that value is removed. I can only guess that it's because the value from the file is a string and validate is... nullifying that as it doesn't match the custom format.

And.. that indeed is the problem. I'm using 'coerce' to take the default value as a key to the secrets. If, however, the json5 file loads a value in, coerce tries to use that value as a key... and fails to find a match returning undefined.

So.. nevermind.