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

Does array include allow nested object properties #553

Closed
michelob opened this issue Feb 17, 2015 · 11 comments
Closed

Does array include allow nested object properties #553

michelob opened this issue Feb 17, 2015 · 11 comments
Assignees
Labels
breaking changes Change that can breaking existing code bug Bug or defect
Milestone

Comments

@michelob
Copy link

Hello...

I'm having a difficult time trying to validate an array which includes an object of nested properties. Should this work?

I've looked through the Joi tests for this case where Jo.array().includes(nested object), but can not see an example there.

https://github.com/hapijs/joi/blob/v5.1.0/test/array.js#L93
https://github.com/hapijs/joi/blob/v5.1.0/test/array.js#L109
https://github.com/hapijs/joi/blob/v5.1.0/test/array.js#L301

None of the tests seem to test a nested object included in an array ... so perhaps this should not work?


var objectSchema = {
  prop1: Joi.string().required(),
  prop2: Joi.string().required(),
  prop3: Joi.object({
    nestprop1: Joi.string().required(),
    nestprop2: Joi.string().required()
  }).required()
}

var arraySchema = Joi.array().includes(Joi.object(objectSchema))

var value = [{
  prop1: 'test1',
  prop2: 'test2',
  prop3: {
    nestprop1: 'nesttest1',
    nestprop2: 'nesttest2'
  }
}]

Joi.validate(value, arraySchema, function (err, value) { 
  *results in ... error* = array at position 0 fails because prop3 is not allowed
})

Strangely Joi is reporting 'prop3' is not allowed, when in the schema it's defined as required.

Thank you
-M

@Marsup
Copy link
Collaborator

Marsup commented Feb 17, 2015

I've copy/pasted your sample without any modification and I can't reproduce the bug.

@Marsup Marsup self-assigned this Feb 17, 2015
@Marsup Marsup added the support Questions, discussions, and general support label Feb 17, 2015
@michelob
Copy link
Author

Hmm...I'm flummoxed.

I edited the sample a couple minutes after posting as I forgot the important bit:

Joi.array().includes(Joi.object(objectSchema))

This works for you? If so...feel free to close.

I'll re-open if I can't get this working after digging in a bit further.

@Marsup
Copy link
Collaborator

Marsup commented Feb 17, 2015

Taking that exact code you run into this error ? Maybe you fixed it while writing the issue.

@michelob
Copy link
Author

Ah ha....I'm good now...

I may I've run into this: #495

My schema was slightly more complicated than my example. Joi wasn't telling which deeply nested property was not validating...it reported at the parent object level.

See above example...I had an issue with prop3.nestprop2, for example, but the error was: 'value at position 0 fails because prop3 is not allowed'.

prop3 was in the error, not prop3.nestprop2.

Feel free to close.

@Marsup
Copy link
Collaborator

Marsup commented Feb 17, 2015

Can you test your case against the master branch since I did not have any confirmation that my fix worked ?

@michelob
Copy link
Author

Is this correct?

"joi": "git://github.com/hapijs/joi.git#8c66bbbaecce20fb103fbdd251f49b6342238e22",

or

"joi": "git://github.com/hapijs/joi.git#master",

sorry for ignorance.

I tried both above.

Unfortunately I did not get an error specified at the nested object property.

I can see that the error has changed slightly however, so I believe I'm on master...

value at position 0 fails because ["prop3" is not allowed]

Note the brackets around the error.

@Marsup
Copy link
Collaborator

Marsup commented Feb 17, 2015

This is probably the master yes, can you provide the code you run to get that error ?

@michelob
Copy link
Author

Done...

Seems still issue in master...simply make a typo in the Schema...

see in the objectSchema I have a typo...prop3.nestprop3 but in my array[0].prop3.nestpro2. (nestprop3 vs nestprop2 is the typo)

var objectSchema = {
  prop1: T.string().required(),
  prop2: T.string().required(),
  prop3: T.object({
    nestprop1: T.string().required(),
    nestprop3: T.string().required() <--- TYPO HERE
  }).required()
};

var arraySchema = T.array().includes(T.object(objectSchema));

var value = [{
  prop1: 'test1',
  prop2: 'test2',
  prop3: {
    nestprop1: 'nesttest1',
    nestprop2: 'nesttest2' <-- INCORRECT PROP HERE
  }
}];

T.validate(value, arraySchema, function (err, value) {
  console.log(err);
  console.log(value);
});

You get:

[ValidationError: "value" at position 0 fails because ["nestprop3" is required]]

@Marsup Marsup added bug Bug or defect and removed support Questions, discussions, and general support labels Feb 17, 2015
@Marsup Marsup added this to the 6.0 milestone Feb 17, 2015
@Marsup
Copy link
Collaborator

Marsup commented Feb 17, 2015

OK, I've changed that part so it now gives "value" at position 0 fails because [child "prop3" fails because [child "nestprop3" fails because ["nestprop3" is required]]]]. A bit verbose but I think we have everything, thoughts ?

@michelob
Copy link
Author

I think that covers my case, where I couldn't see the prop failing, as well as 495 too.

I'm good with that!

@Marsup Marsup closed this as completed in 91568a1 Feb 19, 2015
@michelob
Copy link
Author

Thank you!

@Marsup Marsup added the breaking changes Change that can breaking existing code label Feb 22, 2015
@lock lock bot locked as resolved and limited conversation to collaborators Jan 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
breaking changes Change that can breaking existing code bug Bug or defect
Projects
None yet
Development

No branches or pull requests

2 participants