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

list formats

Open zaach opened this issue 12 years ago • 4 comments

There is currently an Array format, but you can't restrict the values in the array to a certain type.

I was thinking of just wrapping the format property in an array to signify that it should be a list with elements of only that type.

E.g.:

var config = convict({
  foo: {
    default: ["5 seconds", "10 seconds"],
    format: ["duration"]
  }
});

One concern is that it looks similar to the enum format, although enums shouldn't be allowed to have a single element. An alternative would be to add a list: true to the schema.

zaach avatar Mar 05 '13 18:03 zaach

You can make your own format :

  foo: {
    child: ["5 seconds", "10 seconds"],
    format: duration-array
  }



convict.addFormat({
  name: 'duration-array',
  validate: function(children, schema) {
    if (!Array.isArray(children)) {
      throw new Error('must be of type Array');
    }

    for (child of children) {
      convict({
        child: { default: '', format: 'duration' }
      }).load({ child: child }).validate();
    }
  }
});

A-312 avatar Aug 04 '19 21:08 A-312

Also refer to #238.

evisong avatar Nov 14 '19 10:11 evisong

And #312 ?

convict.addFormat({
  name: /^enum\[.*\]$/, //enum[duration]
  // ...
})

A-312 avatar Nov 14 '19 11:11 A-312

This issue should be closed because we have several way to do that.

A-312 avatar Dec 07 '19 08:12 A-312