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.ref doesn't work properly with max() function for dependant dates #1350

Closed
jeurrutia opened this issue Nov 7, 2017 · 4 comments
Closed
Assignees
Labels
bug Bug or defect
Milestone

Comments

@jeurrutia
Copy link

jeurrutia commented Nov 7, 2017

Context

  • node version: 8.9.0
  • joi version: 10.6.0
  • environment (node, browser): node
  • used with (hapi, standalone, ...): hapi

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

When trying to do the validation from a date dependant for another one and using joi.ref(...) an error related to a bad example occurs.

Here the code snippet to reproduce the problem:

server.route({
     method: 'GET',
     path: 'foo',
     handler: (request, reply) => reply('OK'),
      config: {
        validate: {
          query: joi.object({           
            fromDate: joi.date()
              .iso()
              .required()
              .max(joi.ref('toDate'))             
              .example(new Date('2017-10-25T00:00:00.000Z')),
            toDate: joi.date()
              .iso()
              .default(() => new Date(), 'Now')
              .max('now')
              .example(new Date('2017-10-28T00:00:00.000Z')),
          }).unknown(),        
        },
      },
    });

Which result you had ?

error: {
      "message": "Bad example: \"value\" references \"toDate\" which is not a date",
      "name": "Error",
...
}

What did you expect ?

Return json data expected for this API.

@WesTyler
Copy link
Contributor

WesTyler commented Nov 7, 2017

Confirmed this in Joi@13.0.1 as well.

@WesTyler WesTyler added the bug Bug or defect label Nov 7, 2017
@WesTyler
Copy link
Contributor

WesTyler commented Nov 7, 2017

Interesting, so it's not actually the ref in the .max(). It's .example().

If you remove .example() from the "fromDate" child the schema compiles fine. And actually, I think if you just rearrange the order so that .example is called before .max it will also work.

joi.object({           
            fromDate: joi.date()
              .iso()
              .required()             
              .example(new Date('2017-10-25T00:00:00.000Z'))
              .max(joi.ref('toDate')),
            toDate: joi.date()
              .iso()
              .default(() => new Date(), 'Now')
              .max('now')
              .example(new Date('2017-10-28T00:00:00.000Z')),
          }).unknown()

@WesTyler
Copy link
Contributor

WesTyler commented Nov 7, 2017

Oh yeah, we've seen this before but with .min. See the discussion here.

Since the "fix" would be rather involved, there is nothing really left that's actionable here. I am going to bump the discussion issue on the underlying issue over here though.

@WesTyler
Copy link
Contributor

WesTyler commented Nov 13, 2017

This actually should be resolved in v13.0.2. I'm going to close this out. Let me know if you see any additional issues and I can re-open if needed.

@Marsup Marsup added this to the 13.0.2 milestone Nov 13, 2017
@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

No branches or pull requests

3 participants