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

default value for array items #367

Open
Darkein opened this issue Apr 24, 2020 · 8 comments
Open

default value for array items #367

Darkein opened this issue Apr 24, 2020 · 8 comments

Comments

@Darkein
Copy link

Darkein commented Apr 24, 2020

Hello,

i'm using the array validator in my schema.

Unfortunately, because it use a new convict instance to validate the schema, the default values of the elements in my array are not set.

I tried to get my active instance, but because I don't know where i am in the schema during the validation, I can't set the values on my own.

If someone as any idea ?


this is the validator:

convict.addFormat({
  name: 'source-array',
  validate: function(sources, schema) {
   /* ... */
    for (source of sources) {
      convict(schema.children).load(source).validate();
    }
  }
});

my schema:

defaultValue:
  format: String
  default: 'default'
contexts:
  format: array
  default: []
  children:
    defaultValue:
      format: String
      default: 'default'
    name:
      format: String
      default: null

my config file:

contexts:
  - name: TEST1

what I want:

{
   "defaultValue": "default",
   "contexts":[
      {
         "defaultValue": "default",
         "name": "TEST1"
      }
   ]
}

what I have:

{
   "defaultValue": "default",
   "contexts":[
      {
         "name": "TEST1"
      }
   ]
}
@Darkein
Copy link
Author

Darkein commented Apr 24, 2020

I can make a PR if needed, but I don't have any clue how to do it properly

@A-312
Copy link
Contributor

A-312 commented Apr 24, 2020

In my fork, I did: A-312@c9d2410#diff-9a1c05ec2f523aa189f25a817c2b605dR368-R381 (It is maybe usefull to have a different messages displays when you want return several error).

Like this:

Validate failed because wrong value(s):
  - root: Custom format "children" tried to validate something and failed:
    1) germany:
      - name: must be of type String: value was 1
    2) italy:
      - subregion: must be of type String: value was 2 

I can make a PR if needed, but I don't have any clue how to do it properly

You have to had the fullName variable like a third arg:

newFormat(x, this)

newFormat(x, this, fullName) 

@Darkein
Copy link
Author

Darkein commented Apr 27, 2020

Thanks for answer.

I already tried with fullname but it's not enough if I have sub array.
But if I can pass the current instance of convict it could do the job :)

I will give a try

@A-312
Copy link
Contributor

A-312 commented Apr 27, 2020

I already tried with fullname but it's not enough if I have sub array.

I think my solution handle this

@Darkein
Copy link
Author

Darkein commented Apr 27, 2020

it works by adding fullname and instance, thanks :)

export default {
  name: 'source-array',
  validate: (
    sources: any,
    schema: { children: string | convict.Schema<any> },
    fullname: string,
    instance: convict.Config<any>,
  ) => {
    if (!Array.isArray(sources)) {
      throw new Error('must be of type Array');
    }

    const data = sources.map(source => 
        convict(schema.children)
          .load(source)
          .validate({ allowed: 'strict' })
          .getProperties();
    );

    instance.set(fullname, data);
  },
};

I forked node-convict to do it, but I can create a PR on your project if you want ?

@A-312
Copy link
Contributor

A-312 commented Apr 27, 2020

or: newFormat.call(instanceConvict, x, this, fullName)

and after: this.set(fullname, data)

@Darkein
Copy link
Author

Darkein commented Apr 27, 2020

yes, good idea too !

@ashishmahawal
Copy link

Is there any fix available for this issue yet ,i am facing similar issue ,default values of children array objects are not being loading after upgrade to 6.0.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

3 participants