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

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

avxhi93
Copy link

@avxhi93 avxhi93 commented Jun 10, 2021

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

@coveralls
Copy link

Coverage Status

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

@avxhi93
Copy link
Author

avxhi93 commented Jun 11, 2021

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

@keithnlarsen
Copy link

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

@Xyaren
Copy link

Xyaren commented May 17, 2022

Is there any reason this is not merged ?

@fritzmatiasbosonic
Copy link

fritzmatiasbosonic commented Sep 16, 2022

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

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

Successfully merging this pull request may close these issues.

5 participants