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

Dry-schema integration #18

Merged
merged 1 commit into from
Jul 26, 2019
Merged

Dry-schema integration #18

merged 1 commit into from
Jul 26, 2019

Conversation

waiting-for-dev
Copy link
Owner

Integration with dry-schema validation library.

This extension provides a simple integration with dry-schema
library to streamline param sanitization.

On its own, the library just provides with a
Conn#sanitized_params method, which will return what is set into
bag's :sanitized_params key.

This key in the bag is what will be populated by SanitizeParams
plug, which accepts a dry-validation schema that will be applied
to Conn#params:

require 'web_pipe'

WebPipe.load_extensions(:dry_schema)

class App
  include WebPipe

  Schema = Dry::Schema.Params do
    required(:name).filled(:string)
  end

  plug :sanitize_params, WebPipe::Plugs::SanitizeParams[Schema]
  plug(:do_something_with_params) do |conn|
    DB.persist(:entity, conn.sanitized_params)
  end
end

By default, when the result of applying the schema is a failure,
Conn is tainted with a 500 as status code. However, you can
specify your own handler for the unhappy path. It will take the
Conn and Dry::Schema::Result instances as arguments:

plug :sanitize_params, WebPipe::Plugs::SanitizeParams[
                           Schema,
                           ->(conn, result) { ... }
                         ]

A common workflow is applying the same handler for all param
sanitization across your application. This can be achieved setting
a :param_sanitization_handler bag key in a upstream operation
which can be composed downstream for any number of
pipes. SanitizeParams will used configured handler if none is
injected as argument. Another plug ParamSanitizationHandler
exists to help with this process:

class App
  plug :sanitization_handler, WebPipe::Plugs::ParamSanitizationHandler[
                                ->(conn, result) { ... }
                              ]
end

class Subapp
  Schema = Dry::Schema.Params { ... }

  plug :app, App.new
  plug :sanitize_params, WebPipe::Plugs::SanitizeParams[Schema]
end

Integration with `dry-schema` validation library.

This extension provides a simple integration with `dry-schema`
library to streamline param sanitization.

On its own, the library just provides with a
`Conn#sanitized_params` method, which will return what is set into
bag's `:sanitized_params` key.

This key in the bag is what will be populated by `SanitizeParams`
plug, which accepts a `dry-validation` schema that will be applied
to `Conn#params`:

```ruby
require 'web_pipe'

WebPipe.load_extensions(:dry_schema)

class App
  include WebPipe

  Schema = Dry::Schema.Params do
    required(:name).filled(:string)
  end

  plug :sanitize_params, WebPipe::Plugs::SanitizeParams[Schema]
  plug(:do_something_with_params) do |conn|
    DB.persist(:entity, conn.sanitized_params)
  end
end
```

By default, when the result of applying the schema is a failure,
`Conn` is tainted with a 500 as status code. However, you can
specify your own handler for the unhappy path. It will take the
`Conn` and `Dry::Schema::Result` instances as arguments:

```ruby
plug :sanitize_params, WebPipe::Plugs::SanitizeParams[
                           Schema,
                           ->(conn, result) { ... }
                         ]
```

A common workflow is applying the same handler for all param
sanitization across your application. This can be achieved setting
a `:param_sanitization_handler` bag key in a upstream operation
which can be composed downstream for any number of
pipes. `SanitizeParams` will used configured handler if none is
injected as argument. Another plug `ParamSanitizationHandler`
exists to help with this process:

```ruby
class App
  plug :sanitization_handler, WebPipe::Plugs::ParamSanitizationHandler[
                                ->(conn, result) { ... }
                              ]
end

class Subapp
  Schema = Dry::Schema.Params { ... }

  plug :app, App.new
  plug :sanitize_params, WebPipe::Plugs::SanitizeParams[Schema]
end
```
@waiting-for-dev waiting-for-dev added the enhancement New feature or request label Jul 26, 2019
@waiting-for-dev waiting-for-dev merged commit 0bd7ad5 into master Jul 26, 2019
@waiting-for-dev waiting-for-dev deleted the dry_schema branch July 29, 2019 05:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant