-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Feature - Robust Validation #1461
Conversation
cc @jshier and @kcharwood for review. |
6350757
to
f02a579
Compare
c46bc01
to
652536c
Compare
@@ -34,9 +39,117 @@ extension Request { | |||
case failure(Error) | |||
} | |||
|
|||
/// A closure used to validate a request that takes a URL request and URL response, and returns whether the | |||
fileprivate struct MIMEType { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've always wondered whether we want to expose this publicly, as it seems useful for users to be able to parse MIME types themselves, similar to URL encoding methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd vote for keeping it hidden until someone actually requests it. I haven't thought this myself yet.
@cnoon Review complete. Great changes! Just some minor comments. |
f02a579
to
66712de
Compare
652536c
to
f8b1c4b
Compare
…ionFailureReason.
…ubclasses. Previously, the Validation closure only exposed the optional request and response parameters making it difficult to customize validations in inline closures that were not custom extensions in the `Request` class. You didn’t have access to the actual response data. This was limiting in certain cases where you want to create a custom validation error that includes the error message buried in the server data. The only way you could do this previously was to write an extension on `Request`. Now you can do it in an inline closure right at the callsight. Another limitation was that the `Validation` closure did not expose the `destinationURL` for a `DownloadRequest` incase you needed to inspect the data in some special scenario. There is now a custom `Validation` closure on `DownloadRequest` that exposes the `destinationURL` directly. This allows you to inspect the file right in an inline closure without having to extension `DownloadRequest` directly.
f8b1c4b
to
cb44f1f
Compare
a942a7a
to
a01540b
Compare
Thanks for the review @jshier! All the comments have been addressed, I'm going to go ahead and move this one through. |
Thanks so much, we need this so bad in swift 2.2 otherwise we'll be forced to switch to swift 3 and da heck we're gonna get by doing this for big project |
This PR splits up validation between
Request
subclasses and addsData?
orURL?
parameters to theValidation
closure.Motivation
Previously, the Validation closure only exposed the optional request and response parameters making it difficult to customize validations in inline closures that were not custom extensions in the
Request
class. You didn’t have access to the actual response data. This was limiting in certain cases where you want to create a custom validation error that includes the error message buried in the server data. The only way you could do this previously was to write an extension onRequest
.Another limitation was that the
Validation
closure did not expose thedestinationURL
for aDownloadRequest
incase you needed to inspect the data in some special scenario.One final reason to change up the design was that all
validate
methods could be chained on anyRequest
. This doesn't make sense forStreamRequest
types.Solution
Modifying the validation system to accomplish all the goals stated above meant rethinking the majority of the design and splitting it up between subclasses.
Data Request
The
Validation
closure exposed on theDataRequest
class is now as follows:By exposing the
Data?
property directly in the closure, you no longer have to write an extension onRequest
to access it. Now you can do something like this:Download Request
The
Validation
closure exposed on theDownloadRequest
class is now as follows:The
URL?
parameter allows you to access the response data downloaded from the server and stored in thedestinationURL
. This allows you to inspect the data inside the file if you've determined you need to in order to create a custom error.DRY by Design
Since the default validation methods supported in AF are the same between data and download requests, I decided to DRY things up by pulling the validation logic out into a
fileprivate
set of extensions onRequest
. This is very similar to how the response serialization logic was DRY'd up as well.Session Manager
I also snuck in commit c46bc01 into this PR that updates the
SessionManager
to leverage the newTaskConvertible
conformance to generate theURLSessionTask
instances. This cleans up a bunch of duplicated logic that I should have caught in the earlier PRs. This commit doesn't change any functionality, just DRYs things up a bit.Summary
These changes allow inline validation closures to be much more powerful, eliminates chaining validation APIs on
StreamRequest
instances, and exposes thedestinationURL
directly to the inline closure. We'll want to update the Validation section of the README to include examples of best practices here.