Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom format array? #322

Open
basickarl opened this issue Sep 9, 2019 · 1 comment
Open

Custom format array? #322

basickarl opened this issue Sep 9, 2019 · 1 comment

Comments

@basickarl
Copy link

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)

@A-312 A-312 mentioned this issue Oct 27, 2019
@A-312
Copy link
Contributor

A-312 commented Dec 22, 2019

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');
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants