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

Get default value of a property of an object when the object is an array item #403

Open
vaidkaran opened this issue Apr 20, 2022 · 5 comments

Comments

@vaidkaran
Copy link

I am trying to get the default value of pathFilter via convict.default()
Below is my configSchema

  masks: {
    format: 'Object',
    body: {
      doc: 'masks for body',
      format: 'Array',
      default: [],
      children: {
        pathFilter: {
          format: RegExp,
          doc: 'Filter for applying mask to certain paths',
          default: /.*/,
        },
      },
    },
  },

And the config looks like:

    masks: {
      body: [
        {
          pathFilter: /xyz/,
        },
      ],
    }

I tried doing config.default('masks.body.children.pathFilter') but that doesn't work.
One problem that I see here is that we add and look for _cvtProperties under arrays too. Arrays seem to have children and not _cvtProperties. So the default function would resolve the path and look for body._cvtProperties but it won't exist.

Maybe we need to cater for arrays here

Please advise.

@koerbcm
Copy link

koerbcm commented Apr 20, 2022

+1

@madarche
Copy link
Collaborator

@vaidkaran or anyone interested, could you create a failing test demonstrating the problem please? That would help!

@vaidkaran
Copy link
Author

const convict = require('convict');

const schema = {
  masks: {
    format: 'Object',
    body: {
      doc: 'masks for body',
      format: 'Array',
      default: [],
      children: {
        pathFilter: {
          format: RegExp,
          doc: 'Filter for applying mask to certain paths',
          default: /.*/,
        },
      },
    },
  },
}


const config = convict(schema).load({
  masks: {
    body: [
      {
        pathFilter: /xyz/,
      },
    ],
  },
}).validate();

const defaultBody = config.default('masks.body') // returns [] which is fine
console.log('Default value of body: ', defaultBody);

const defaultPathFilter = config.default('masks.body.children.pathFilter') // doesn't work
// Error: cannot find configuration param 'masks._cvtProperties.body._cvtProperties.children._cvtProperties.pathFilter.default'

Does this help?

@koerbcm
Copy link

koerbcm commented May 9, 2022

Any update on this?

@TzachiSh
Copy link

TzachiSh commented Jan 8, 2023

+1

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

4 participants