# Decoupling input pre-processing from validation rules

**URL:** https://discourse.dry-rb.org/t/decoupling-input-pre-processing-from-validation-rules/669
**Category:** dry-validation
**Created:** [December 11, 2018, 8:08am UTC](https://discourse.dry-rb.org/t/decoupling-input-pre-processing-from-validation-rules/669 "2018-12-11T08:08:08Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![waiting-for-dev](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/waiting-for-dev/32/71_2.png) [@waiting-for-dev](https://discourse.dry-rb.org/u/waiting-for-dev)
#### Post date: [December 11, 2018, 8:08am UTC](https://discourse.dry-rb.org/t/decoupling-input-pre-processing-from-validation-rules/669/1 "2018-12-11T08:08:08Z")

</div>

Currently, it is possible to define, for example, the validation of a form through `Dry::Validation.Params`. It will pre-process given input (symbolizing keys and coercing values) and later it will apply the “expected schema”.

However, I’d like to be able to define just a “core business” schema, and then be able to call it from different interfaces applying an input pre-processor which only makes sense for that interface. For example, for the web interface, something like that:

```ruby
result = MySchema.(
  Dry::Validation::Processors::Params.(my_input)
)

```

I think it makes more sense because it is decoupling validation rules from input pre-processing. However, if I’m not wrong, it is possible to do that easily. [Pre-processors expect the AST](https://github.com/dry-rb/dry-validation/blob/master/spec/unit/input_processor_compiler/params_spec.rb), and doing it manually through [`type_specs`](https://dry-rb.org/gems/dry-validation/input-preprocessing/) would be very cumbersome.

Is there another way to do that, or is it planned for 1.0?

Thanks in advance.

---

<div class="post-metadata">

### Author: ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)
#### Post date: [December 11, 2018, 10:31am UTC](https://discourse.dry-rb.org/t/decoupling-input-pre-processing-from-validation-rules/669/2 "2018-12-11T10:31:21Z")

</div>

This will be separated in dry-schema that dry-validation will use. I recently started working on it in [this PR](https://github.com/dry-rb/dry-schema/pull/11). The idea is that pre-processing (aka coercion) will be an even more distinct step, furthermore, you’ll be able to provide “filter” rules that will reject any input that cannot be coerced.

---

<div class="post-metadata">

### Author: ![waiting-for-dev](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/waiting-for-dev/32/71_2.png) [@waiting-for-dev](https://discourse.dry-rb.org/u/waiting-for-dev)
#### Post date: [December 11, 2018, 10:58am UTC](https://discourse.dry-rb.org/t/decoupling-input-pre-processing-from-validation-rules/669/3 "2018-12-11T10:58:15Z")

</div>

Happy to hear that, that looks even better. Thanks for your reply!

---

<div class="post-metadata">

### Author: ![shepmaster](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/shepmaster/32/396_2.png) [@shepmaster](https://discourse.dry-rb.org/u/shepmaster)
#### Post date: [July 22, 2019, 8:50pm UTC](https://discourse.dry-rb.org/t/decoupling-input-pre-processing-from-validation-rules/669/4 "2019-07-22T20:50:50Z")

</div>

Since dry-schema and dry-validation both exist now, are there any clear examples of this idea being applied?

---

<div class="post-metadata">

### Author: ![waiting-for-dev](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/waiting-for-dev/32/71_2.png) [@waiting-for-dev](https://discourse.dry-rb.org/u/waiting-for-dev)
#### Post date: [July 23, 2019, 5:13am UTC](https://discourse.dry-rb.org/t/decoupling-input-pre-processing-from-validation-rules/669/5 "2019-07-23T05:13:47Z")

</div>

@shepmaster dry-schema is responsible for the input pre-processing (coercion, like to `Params` or `JSON`) while dry-validation handles validation rules. It’s its core behaviour, so you just have to look at their documentation to see examples 🙂
