node-convict
node-convict copied to clipboard
Custom format array?
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)
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');
});