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

Implementation of a new feature: support loading of map like objects

Open avxhi93 opened this issue 3 years ago • 5 comments

Implementation of a new feature: support loading of map like objects

For example: 
  var config = {
    myMap: {
      someArbitraryKey:    { schemaBoundProp: 'asdf', anotherSchemaBoundProp: 'qwer'},
      anotherArbitraryKey: { schemaBoundProp: 'zxcv', anotherSchemaBoundProp: 'vbnm'}
    }
  }

  convict.addFormat({
    name: `map`,
    validate: function (theMap, schema) {
      if (typeof theMap !== 'object' || theMap == null) {
        throw new Error('must be an non-empty (map like) object')
      }
      // iterate each key in the map 
      for (const key of Object.keys(theMap)) {
        // perform validation on the key's value against the child schema
        convict(schema.children).load(theMap[key]).validate()
      }
    }
  })

  const schema = {
    myMap: {
      doc: 'this is a key value map, where the value object can be validated by a child schema',
      default: {},
      format: 'map',
      children: {
        schemaBoundProp: {
          doc: 'a property of the value object',
          default: null,
          format: String
        }, 
        anotherSchemaBoundProp: {
          doc: 'a property of the value object',
          default: null,
          format: String
        }
      }
    }
  }

  convict(schema).load(config).validate()

CHANGES made -> one line change to the overlay function

avxhi93 avatar Jun 10 '21 20:06 avxhi93

Coverage Status

Coverage remained the same at 93.151% when pulling 59eb1e5bed5ed27a60752a11bcc95d5a8e243d11 on avxhi93:master into bdd8a4e55a236c2ccb2a2d6062c9ac3ddec34af6 on mozilla:master.

coveralls avatar Jun 10 '21 20:06 coveralls

@A-312, @madarche can I have the attention of one of you guys to accept this pull request, please.

avxhi93 avatar Jun 11 '21 22:06 avxhi93

This would be a great feature, please do accept it 👍

keithnlarsen avatar Jun 14 '21 18:06 keithnlarsen

Is there any reason this is not merged ?

Xyaren avatar May 17 '22 10:05 Xyaren

@A-312, @madarche still not merged :(

fritzmatiasbosonic avatar Sep 16 '22 20:09 fritzmatiasbosonic