Skip to content

Providing a default of undefined prevents format from being checked #278

Description

@molomby

The docs are clear that defaults must be supplied and, although the specifics could be improved (#254), not supplying a default value usually errors out.

However, if undefined is given for a default value and the environment variable is also undefined, the format function is never called. This results in bugs like this...

var convict = require("convict");

var config = convict({
  something: {
    format: (val) => {
        if (typeof val === 'undefined' || val === null || val === '') {
            throw new Error('must be a non-empty string');
        }
    },
    default: undefined,
    env: "SOMETHING",
  },
});

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

// If the `SOMETHING` env var isn't set, this will return `undefined` rather than erroring
// The format() function is never called
config.get('something');

Tested on 4.4.1, see RunKit

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions