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

Joi valid fails, when ref is used with a stripped key. #831

Closed
kevbook opened this issue Feb 20, 2016 · 4 comments
Closed

Joi valid fails, when ref is used with a stripped key. #831

kevbook opened this issue Feb 20, 2016 · 4 comments
Assignees
Labels
feature New functionality or improvement
Milestone

Comments

@kevbook
Copy link

kevbook commented Feb 20, 2016

Joi valid fails, when ref is used with a stripped key. Maybe this is the expected behaviour. I'll let you decide.

  • node version: v4.2.1
  • joi version: ^0.8.5
  • environment (node, browser): node
  • used with (hapi, standalone, ...): standalone

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

var Joi = require('joi');

var schema = Joi.object({
  a: Joi.any().required().strip(),
  b: Joi.any().valid(Joi.ref('a'))
});
console.log(  schema.validate({ a: 1, b: 1 }) );

Which result you had ?

Error: child "b" fails because ["b" must be one of [ref:a]]

What did you expect ?

No error, and "a" should have been stripped. Value: { b: 1 }

@Marsup
Copy link
Collaborator

Marsup commented Feb 21, 2016

This is the expected behavior, I can't remember anyone needing this use case, I'll wait see if more people need this.

@Marsup Marsup added the request label Feb 21, 2016
@Marsup Marsup self-assigned this Feb 21, 2016
@Marsup Marsup added this to the 10.3.0 milestone Mar 18, 2017
@Marsup Marsup closed this as completed in 6f4377b Mar 18, 2017
@emoriarty
Copy link

emoriarty commented Mar 24, 2017

Hello there,

I just run into this strange "issue" in a similar context when using strip() with Strings instead of Numbers.

Scenario

A port range numbers needs to be validated, so let's call them port and portend. The restrictions applied are as follows:

  • port is required,
  • port must be between 1 and 65535,
  • portend is not required,
  • portend cannot be smaller than port.

In order to check portend is equal or greater than port is mandatory the use of a third property: portmin. This last property will store the value of port - 1. Then portmin will be used as reference to validate portend.

So the validation object will look as follows.

var Joi = require('joi');

function portValidation () {
  return Joi.number().integer().min(1).max(65535);
}

var schema = Joi.object().keys({
  port: portValidation().required(),
  portmin: portValidation().strip(),
  portend: portValidation().greater(Joi.ref('portmin'))
});

console.log(schema.validate(
  { port: "1234", portmin: "1233", portend: "1234" },
  { convert: true }
));

In this scenario portmin is only used to help for the portend validation in case is inserted. There is no much use for portmin, so it is marked to be removed from the final result.

Obviously, this scenario can be achieved using the min() function instead of the portmin reference but for this matter the example is as it is.

Issue

When using Number values works OK.

{ error: null, value: { port: 1234, portend: 1234 } }

The issue comes up when the port's properties are String based values. By default, validate() converts String to Numbers but looks this convertion is not being carried out when the referenced property is stripped before the greater() validation is executed.

{ error:
   { ValidationError: child "portend" fails because ["portend" references "portmin" which is not a number]
       at Object.exports.process (/Users/enric/Workspace/testing/joi/node_modules/joi/lib/errors.js:154:19)
       at Object._validateWithOptions (/Users/enric/Workspace/testing/joi/node_modules/joi/lib/any.js:629:31)
       at Object.validate (/Users/enric/Workspace/testing/joi/node_modules/joi/lib/any.js:644:21)
       at Object.<anonymous> (/Users/enric/Workspace/testing/joi/joi-validations.js:13:20)
       at Module._compile (module.js:571:32)
       at Object.Module._extensions..js (module.js:580:10)
       at Module.load (module.js:488:32)
       at tryModuleLoad (module.js:447:12)
       at Function.Module._load (module.js:439:3)
       at Module.runMain (module.js:605:10)
     isJoi: true,
     name: 'ValidationError',
     details: [ [Object] ],
     _object: { port: '1234', portmin: '1233', portend: '1234' },
     annotate: [Function] },
  value: { port: 1234, portmin: '1233', portend: '1234' } }

But if the strip() function is removed, it works as expected.

var schema = Joi.object().keys({
  port: portValidation().required(),
  portmin: portValidation(),
  portend: portValidation().greater(Joi.ref('portmin'))
});

Another "issue" involves when portmin is moved after portend, no matter id strip() is applied or not, just does not validate the object. The error returned is the same as previous.

var schema = Joi.object().keys({
  port: portValidation().required(),
  portend: portValidation().greater(Joi.ref('portmin')),
  portmin: portValidation()
});

Expected Result

No matter Number or String values are used, the expected behavior should be the same.

@Marsup
Copy link
Collaborator

Marsup commented Apr 2, 2017

@emoriarty Thanks for the report, but in the future please create your own issue instead of reviving closed ones.

@hueniverse hueniverse added feature New functionality or improvement and removed request labels Sep 19, 2019
@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@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
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

4 participants