Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement: allow partial validation to check values used before config is ready #162

Open
Xerkus opened this issue Sep 20, 2016 · 1 comment

Comments

@Xerkus
Copy link

Xerkus commented Sep 20, 2016

A way to do partial validation is needed for the cases where value from incomplete config is used to finish setting it up.
With sample code below node index.js --config config.js will fail (hopefully) on loadFile()

const convict = require('convict');
const path = require('path');

convict.addFormat({
    name: 'configfile',
    validate: (val) => {
        // Some sample checks 
        if (typeof val !== 'string' || ! val.endsWith('.json')) {
            throw new TypeError('Path must be a string pointing to json file');
        }
    },
    coerce: (val) => {
        return path.resolve(val);
    },
});

const conf = convict({
    config: {
        doc: 'Configuration file to use',
        format: 'configfile',
        default: path.resolve(__dirname, 'config.json'),
        arg: 'config',
    },
});

// fs, json5 or some other error is thrown here
conf.loadFile(conf.get('config'));

And with partial validation we can be sure values are good:

// validate 'config' only, validation error will be thrown here and loadFile will never be reached
conf.validate({}, ['config']);
conf.loadFile(conf.get('config'));

This case is fairly safe, but it gets more important if, for example, that value is used to fetch config from external storage

@A-312
Copy link
Contributor

A-312 commented Dec 7, 2019

Solution : Should be to run 2 instances of convict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants