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

Support for env variable arrays

Open cupcakearmy opened this issue 2 years ago • 1 comments

Sry if there is already a ticket, I could not find it.

Is there any way to use arrays in a ENV variable? something like FOO=1,2,3? I understand that comma might be a valid char, so maybe have it opt in?

cupcakearmy avatar Dec 08 '21 19:12 cupcakearmy

This should work

convict.addFormat({
  name: "comma-separated-string",
  validate: function (val) {
    const emptyStringRegex = /^$/;
    const commaSeparatedStringRegex = /^[\w\d]+(,[\w\d]+)*$/;

    if (!emptyStringRegex.test(val) && !commaSeparatedStringRegex.test(val)) {
      throw new Error("must be a comma separated string");
    }
  },
  coerce: function (val) {
    if (!val) return [];

    return val.split(",");
  },
});

petertriho avatar Dec 08 '22 06:12 petertriho