arb / celebrate

A joi validation middleware for Express.
MIT License
1.33k stars 66 forks source link

Subsequent calls no longer validating on upgrade to v11 #147

Closed rnphilp closed 4 years ago

rnphilp commented 4 years ago

My joi schema looks like this:

exports.getAllProjectParametersSchema = {
  query: Joi.object({
    limit: Joi.number()
      .integer()
      .default(50),
    start: Joi.number()
      .integer()
      .default(0),
    sort: Joi.string()
      .valid(
        'asset_name',
        'asset_name:asc',
        'asset_name:desc',
        'updated_by',
        'updated_by:asc',
        'updated_by:desc',
      )
      .default('updated_at:desc')
  }),
  params: Joi.object({
    project_id: Joi.number()
      .integer()
      .required()
  })
};

Here is an example value that is not working as expected: Route being checked:

projectParameterRouter
  .route('/api/projects/:project_id/parameters')
  .get(celebrate(getAllProjectParametersSchema), getAllProjectParameters)
  .all(methodNotAllowed);

Example api call for which celebrate should set the default queries:

'/api/projects/1/parameters'

The issue I am having with celebrate is:

The code was working ok in v10.1.0. I upgraded it to allow use of joi-v16 and the failover() method to set the queries to predefined values if they don't meet the schema. However, I'm running into validation issues before changing any of the code. I'm using Jest to test the api and for the first value in the test the default values are updating req.query. However, all subsequent tests in the same run do not have their default values updated and do not appear to be getting validated. Equally all tests when run in isolation work as expected. The same happens when running the api locally - the first call to the endpoint works, but subsequent calls do not (as per the tests). I don't think there is an issue with the schema vs the upgraded joi version as its validating the first request ok. Any ideas what would be causing this? Thanks.

arb commented 4 years ago

Are there any exceptions of errors logged anywhere? Is the schema you posted up there exactly as you have it in your unit tests?

arb commented 4 years ago

Figured it out, will open a PR and push rc3 soon. Thank you @rnphilp for taking the release candidate for a spin!!! You caught this early 🙇

arb commented 4 years ago

@rnphilp - Try version 11.0.0-rc3

rnphilp commented 4 years ago

@arb - all working as expected. thanks!