Skip to content

Conversation

@pmcnr-hx
Copy link
Contributor

@pmcnr-hx pmcnr-hx commented May 22, 2016

Parses and Joi validate, setting values to correct types, during Joi validates.

This was a bit more invasive than initially anticipated, but it should improve the validation of filters while providing a fully parsed filter to both post-processing and handlers, while avoiding duplicated functionality for this in each handler.

  • 👍 1️⃣
  • 👍 2️⃣
  • 👍 3️⃣

@pmcnr-hx pmcnr-hx changed the title [RFC] Parse and Joi validate search filter [RFC] Parse and Joi validate filter May 22, 2016
@pmcnr-hx pmcnr-hx force-pushed the parse-and-joi-validate-search-filter branch from 6588ec5 to 3d1e565 Compare May 22, 2016 23:20
@theninj4
Copy link
Contributor

Thanks for the contribution 👍 I'm sat here thinking if mutating the original filter object is the right thing to do. I think it'd be better for everyone else if we simply build a new object called something like processedFilter, validatedFilter, safeFilter or somerthing, which exists alongside the original set of filters? That way we won't be breaking any existing integrations and we still provide the nicer interface moving forwards.

@pmcnr-hx
Copy link
Contributor Author

pmcnr-hx commented May 26, 2016

You're sat there thinking very wise thoughts. I concur with you 100% and will adjust accordingly. That will actually the changes less intrusive and I will be able to simplify the patch. Thanks!

if (request.route.query) {
uri += "&" + request.route.query;
}
return uri;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this change designed to try and remove the leading & from the query string? It's quite hard to read by comparison, I'd rather we either live with the leading ampersand or we simply substring it off with a comment saying // trim the leading ampersand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wasn't just that, it was because we weren't passing the filter to the included resources rerouted request (see 54-56). With the new strategy of not altering the original filter and adding the parsed filter as a new property we won't need to touch lib/postProcess.js at all so I am going to remove these changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahhh right, cool 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed it back to how it was before but we still need to pass the query string to the related resources. I am not sure how the test was working before... 😖

pmcnr-hx added 4 commits May 27, 2016 00:37
We only meant to skip validation for nested filters, but we were
actually skipping array values (multiple values for a property) which
should not be skipped.
Filters should always be valid when we get to the post-processing stage
so remove the redundant check.
Parse and validate using Joi the filter elements, so this work is only
done in one place and the parsed filters can be provided to the
handlers, avoid the need to redo that work there as is currently needed.
The test was passing previously by accident. Once the filter is
corrected to include a valid email address, it breaks because the filter
isn't being passed to the related resource rerouted request. Fix the
test and pass the query string (including the filter) to related
resources.
@pmcnr-hx pmcnr-hx force-pushed the parse-and-joi-validate-search-filter branch from 3d1e565 to 94efbb6 Compare May 27, 2016 00:11
@pmcnr-hx
Copy link
Contributor Author

pmcnr-hx commented May 27, 2016

@theninj4: implemented the proposed changes. Adding the processed filter as a new property of the request, instead of mutating the existing filter is indeed a cleaner, less invasive and backward compatible approach 😍. Post-processing filtering is now nicely simplified, and the parsed filter is made available to the handlers simplifying their job and avoiding code duplication there.

lib/filter.js Outdated
return { result: helperResult.result };
};

filter.process = function(request, callback) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is filter.process the right name for this? Reading further down and seeing it alongside other high level features left me a bit confused as to what processing it was doing - maybe something like filter.validateAndSanitise could be more appropriate?

Copy link
Contributor Author

@pmcnr-hx pmcnr-hx May 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, I though I had renamed it to parseAndValidate... Oops, forgot to push those changes.

@theninj4
Copy link
Contributor

This is looking good 👍

@pmcnr-hx
Copy link
Contributor Author

Feedback actioned @theninj4. Thanks for the +1. 😀

@reecemillsom
Copy link

Taking 1️⃣

@reecemillsom
Copy link

Looking epic 👍

pmcnr-hx added 2 commits May 27, 2016 11:04
The more generic nature of its functionality warrants the rename.
@pmcnr-hx pmcnr-hx force-pushed the parse-and-joi-validate-search-filter branch from c0bc94c to 4357871 Compare May 27, 2016 10:05
@elliottcrush
Copy link

🔎

@elliottcrush
Copy link

Happy with the changes and actioned updates. Looks like some nice work here 👍

@pmcnr-hx pmcnr-hx merged commit 9ac9f4f into swagger-fixes May 27, 2016
@pmcnr-hx pmcnr-hx deleted the parse-and-joi-validate-search-filter branch May 27, 2016 12:15
@theninj4
Copy link
Contributor

Hmm... this merged into the wrong branch :'(

@pmcnr-hx
Copy link
Contributor Author

It was! Just fixing it now.

@pmcnr-hx pmcnr-hx restored the parse-and-joi-validate-search-filter branch May 27, 2016 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants