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

Custom format array?

Open basickarl opened this issue 6 years ago • 1 comments

Hi quick question, I'm wondering if it's possible to have custom formats in the array for example:

format: ['my-custom-added-format-1', 'my-custom-added-format-2'],

It seems to expect a string when I try and do this! (I've added the custom formats via the addFormat method)

basickarl avatar Sep 09 '19 14:09 basickarl

Actually, a format array is for a whitelist of value, like : format: ['value1', 'value2'].

But you can make your own format to do :

format: 'several',
formats: ['my-custom-added-format-1', 'my-custom-added-format-2']
convict.addFormat('several', function (value, schema) {
  for (formatExpected in schema.formats) {
    const conf = convict({ val: { format: formatExpected } }).load({ val: value }).validate();
    try {
        conf.validate();
        return true;
    } catch { }
  });
  throw new Error('invalid');
});

A-312 avatar Dec 22 '19 12:12 A-312