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

Object format cannot use null-default

Open ronkorving opened this issue 5 years ago • 3 comments

From the ReadMe:

However, you can set a default value to null and if your format doesn't accept null it will throw an error.

This works for String for example, but not for the Object format. This is unfortunate (in my case anyway :)), and I was wondering if there is a design-choice underlying this, or if it could be allowed? Thanks!

ronkorving avatar May 08 '19 09:05 ronkorving

Do you have an example of what you want to do ?

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

Heh, I wish you had asked in May :) I should've been clearer the first time around, apologies.

iirc, I was hoping to configure a dictionary-style property like:

{
  format: Object,
  default: null
}

I think I wanted that to throw when the configuration was omitted?

ronkorving avatar Aug 05 '19 01:08 ronkorving

Similar to : https://github.com/mozilla/node-convict/issues/7#issuecomment-553856857

You can make your own format and accept null or object ? Or with #312 :

convict.addFormat({
  name: /^(.*)-null$/, //accept : String-null, Object-null, etc...
  validate: function(value, schema) {
   if (value === null)
     return true;

   const subSchema = {};

   subSchema["value-or-null"] = {
      format: schema.format.match(/^Array\[(.*)]$/)[1],
      default: null
    };
    convict(subSchema).load(value).validate();
  })
})

Do you have any proper solution which need a PR ?

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