Joi: undefined in the array with label returns wrong missing value message

Created on 5 Jan 2018  路  8Comments  路  Source: sideway/joi

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.

bug

Most helpful comment

It should be yes.

All 8 comments

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.

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

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]]

@Marsup firstly thanks for your excellent work.

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

@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 !

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.

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

It should be yes.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

neroaugustus1 picture neroaugustus1  路  4Comments

farwayer picture farwayer  路  3Comments

leore picture leore  路  4Comments

n-sviridenko picture n-sviridenko  路  3Comments

Taxi4you picture Taxi4you  路  3Comments