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

Applying raw after successful validation #1972

Closed
kasperekt opened this issue Jul 19, 2019 · 1 comment
Closed

Applying raw after successful validation #1972

kasperekt opened this issue Jul 19, 2019 · 1 comment
Assignees
Labels
breaking changes Change that can breaking existing code feature New functionality or improvement
Milestone

Comments

@kasperekt
Copy link

Describe the problem you are trying to fix (provide as much context as possible)

I have an issue with compare dates of certain format using .min() and .ref() methods.
Basically, what I need is comparing two dates and checking whether one is less or equal to another. There's a catch, I need to use raw() to ensure that dates are still in string format.

Let's consider this example:

const schema = Joi.object({
  from: Joi.date().format('YYYY-MM-DD').raw().required(),
  to: Joi.date().format('YYYY-MM-DD').min(Joi.ref('from')).raw().required()
})

const payload = {from: '2019-10-19', to: '2019-10-19'}

I would expect that payload would pass validation. But it doesn't. I get an error where .min() condition is not satisfied. When I remove raw() it works great, but I don't have dates as a string format.

Is there any way to achieve my goal with current API?

Which API (or modification of the current API) do you suggest to solve that problem ?

I'm using version 15.0.1 of @hapi/joi package together with 1.3.0 version @hapi/joi-date package, so it's not base package only.

Are you ready to work on a pull request if your suggestion is accepted ?

Yes, I would be glad to help.

@hueniverse
Copy link
Contributor

Since raw is applied on the value immediately after it is validated, the reference resolves into the raw value which of course fails validation. It might be easy to support in your simple case, but not in cases where the reference goes deeper than a sibling.

However, in this specific example, you can just remove raw() and ignore the converted value. If the value passes validation, use the original value because it will be in raw string format that was validated.

@hueniverse hueniverse self-assigned this Jul 19, 2019
@hueniverse hueniverse added request breaking changes Change that can breaking existing code feature New functionality or improvement and removed request labels Jul 19, 2019
@hueniverse hueniverse added this to the 16.0.0 milestone Jul 26, 2019
@hueniverse hueniverse added the v16 label Aug 11, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Feb 8, 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 feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

2 participants