-
Notifications
You must be signed in to change notification settings - Fork 184
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
Expose conn
validation for reuse in custom plugs
#129
Expose conn
validation for reuse in custom plugs
#129
Conversation
""" | ||
def init(opts), do: opts | ||
|
||
def call(conn, opts) do | ||
validation_failed_status = Keyword.get(opts, :validation_failed_status, 400) | ||
regex_result = opts | ||
|> Keyword.get(:exclude_paths, [~r/$^/]) # the default regex matches nothing |
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.
List.wrap()
will convert a nil
into an empty list, so no need for the default regex.
@doc """ | ||
Performs unconditional validation of a request. Will halt and send error on failed validation. | ||
""" | ||
def handle_validation(conn, opts) do |
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.
This feels like a private function
* `{:error, message, path}` if the request was mapped but failed validation | ||
""" | ||
def validate(conn) do | ||
result = |
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.
this result
variable is unused
Otherwise looks good to me |
I believe this already works, eg see the example project: pipeline :api do
plug :accepts, ["json"]
plug Validate, validation_failed_status: 422
end
scope "/api", SimpleWeb do
pipe_through :api
resources "/users", UserController, except: [:new, :edit]
end
scope "/api/swagger" do
forward "/", PhoenixSwagger.Plug.SwaggerUI, otp_app: :simple, swagger_file: "swagger.json"
end |
I'm wondering if there is another way to opt-out of validation that doesn't involve matching multiple regexes against each request. Eg, if we set a flag like |
It seems to me that this plug has two interesting parts: validation of the request parameters against a Swagger Spec from ETS, and acting upon the results of the validation. Maybe it is a better idea to just extract |
conn
validation for reuse in custom plugs
@0xAX @mbuhot thank you for your feedback! I have extracted request validation from the plug, so now it can be reused in custom plugs or called directly from controllers. Also, there is now a flag |
The validation plug is quite handy, but only works best at the top-level router. This commit allows piping the whole API scope (e.g. /api) throught the validator plug, but allowing Swagger UI to be hosted somewhere under the scope's path (e.g. /api/v1/docs) without validation errors.
43b054f
to
84dd479
Compare
@soundmonster sorry this has been unmerged for so long. I'll take a look and try to merge this week. 👍 |
Great! BTW, we're running off of my fork in production since late Oct 2017 and no problems so far 😉 |
👍 Moving the query/path/header param validation out of the It might be a bit confusing for new users now that there are 3 ways to perform validation with |
The validation plug is quite handy, but only works best at the top-level
router. This commit allows piping the whole API scope (e.g. /api)
throught the validator plug, but allowing Swagger UI to be hosted
somewhere under the scope's path (e.g. /api/v1/docs) without validation
errors.