node-convict icon indicating copy to clipboard operation
node-convict copied to clipboard

Validate the schema to require defaults

Open theRemix opened this issue 7 years ago • 4 comments

Default values are required for every config key. If a default value is not set, convict does not throw a validation error.

If there are any missing default keys, config.validate() should throw a validation error.

theRemix avatar May 17 '18 23:05 theRemix

This just bit us, when doing config.get('value'), the configuration object was returned { doc: 'Some documentation', format: '*', env: 'TEST_VALUE' }. This stopped happening when we set default.

jonpacker avatar Nov 22 '18 11:11 jonpacker

On 4.4.1, when I have no default configured for a value, eg..

var convict = require("convict")

var config = convict({
  something: {
    format: (val) => { /* noop */ },
    env: "SOMETHING"
  }
});

config.validate({ allowed: 'strict' });

The error I get is..

Error: something.format: should be of type Function: value was {}

Which is neither correct (format is a function..?) or at all helpful.

molomby avatar Feb 05 '19 04:02 molomby

var convict = require("convict")

var config = convict({
  something: {
    format: (val) => { /* noop */ },
    env: "SOMETHING"
  }
});

config.validate({ allowed: 'strict' });

Convict understands :

{
  "something": {
    "format": {
      "format": "function"
    },
    "env": {
      "default": "SOMETHING",
      "format": "string"
    }
  }
}

A-312 avatar Jan 06 '20 20:01 A-312

Default values are required for every config key. If a default value is not set, convict does not throw a validation error.

We can't do that because :

https://github.com/mozilla/node-convict/blob/d2637632ee2a8c05905e15f2ca9000b6ef2dde21/lib/convict.js#L241-L245

Or we should disable the magic normalizer when we use the strict mode.

A-312 avatar Jan 06 '20 21:01 A-312