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

undefined in the array with label returns wrong missing value message #1392

Closed
rohitshetty opened this issue Jan 5, 2018 · 8 comments
Closed
Assignees
Labels
bug Bug or defect
Milestone

Comments

@rohitshetty
Copy link

Context

  • node version: 6.10
  • joi version: 10.6.0
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone
  • any other relevant information:

What are you trying to achieve or the steps to reproduce ?

I am trying to validate a variable (array of length two) which is a location data, with lat long.
I want to validate the array such that -90 < lat < 90 and -180 < long<180 also make sure that both the values are present.
Location data is in GeoJSON hence data at 0 position should be longitude and latitude at pos 1. And subsequent rules should be applied for testing.

Describe your issue here, include schemas and inputs you are validating if needed.

var schema = Joi.object().keys({location: Joi.array().ordered(Joi.number().min(-180).max(180).label('longitude').required(), Joi.number().min(-90).max(90).label('latitude').required()).label('location').required()});

var input = {location: [undefined, 10]};
Joi.validate(input,schema, {abortEarly: false});

Which result you had ?

{ error: 
   { ValidationError: child "location" fails because ["location" must not be a sparse array, "location" does not contain [latitude]]
       at Object.exports.process (/home/rohit/dev/upwork/streetography/joi/joi/node_modules/joi/lib/errors.js:196:19)
       at _validateWithOptions (/home/rohit/dev/upwork/streetography/joi/joi/node_modules/joi/lib/types/any/index.js:668:31)
       at root.validate (/home/rohit/dev/upwork/streetography/joi/joi/node_modules/joi/lib/index.js:139:23)
       at repl:1:5
       at sigintHandlersWrap (vm.js:22:35)
       at sigintHandlersWrap (vm.js:73:12)
       at ContextifyScript.Script.runInThisContext (vm.js:21:12)
       at REPLServer.defaultEval (repl.js:346:29)
       at bound (domain.js:280:14)
       at REPLServer.runBound [as eval] (domain.js:293:12)
     isJoi: true,
     name: 'ValidationError',
     details: [ [Object], [Object] ],
     _object: { location: [Object] },
     annotate: [Function] },
  value: { location: [ undefined, 5 ] },
  then: [Function: then],
  catch: [Function: catch] }

What did you expect ?

observe the error message: "location does not contain [latitude]", should it rather not be "longitude" ?
because the first parameter is undefined, should not the output specify which of the input index has the error? (here in this case longitude because we are using label)?

Let me know if you think this is a bug (or if this is intended and I am using this feature wrongly) And if there is something i can do to fix it (would be happy to make a PR if needed)
Thank you for the time.

@Marsup Marsup added the bug Bug or defect label Jan 5, 2018
@Marsup
Copy link
Collaborator

Marsup commented Jan 5, 2018

Confirmed the bug, there is probably some wrong logic happening on the manipulation of the ordereds array or the error built out of it. You can have a look at the code but it might not be an easy one.

@rohitshetty
Copy link
Author

Thanks for confirming this bug, I will try to have a look the source code and get back with queries if any.

@mrveera
Copy link

mrveera commented Jan 18, 2018

I think it should be ValidationError: child "location" fails because ["location" must not be a sparse array, "location" at position 0 fails because ["longitude" is required]]

@mrveera
Copy link

mrveera commented Jan 18, 2018

@Marsup firstly thanks for your excellent work.

Would you please consider merging a PR if I fix this issue?

@Marsup
Copy link
Collaborator

Marsup commented Feb 2, 2018

@veera83372 I had a look at your PR and it's not the correct fix, the validation was the real problem, not the error building process. Thanks for trying though !

@Marsup Marsup closed this as completed in 45192c5 Feb 2, 2018
@Marsup Marsup added this to the 13.1.2 milestone Feb 2, 2018
@Marsup Marsup self-assigned this Feb 2, 2018
@Marsup
Copy link
Collaborator

Marsup commented Feb 2, 2018

The new error you'll get might not strictly be what you'd expect in terms of wording but it's how it should always have been.

@rohitshetty
Copy link
Author

@Marsup Thank you for your time and help. Will check the new fix. Is this version out at npm ?

@Marsup
Copy link
Collaborator

Marsup commented Feb 2, 2018

It should be yes.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bug or defect
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants