# Reusable schema fragments

**URL:** <https://discourse.dry-rb.org/t/reusable-schema-fragments/988>\
**Category:** dry-validation\
**Created:** [January 24, 2020, 2:18am UTC](https://discourse.dry-rb.org/t/reusable-schema-fragments/988 "2020-01-24T02:18:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Minty-Fresh](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/minty-fresh/32/411_2.png) [@Minty-Fresh](https://discourse.dry-rb.org/u/Minty-Fresh)\
**Post date:** [January 24, 2020, 2:18am UTC](https://discourse.dry-rb.org/t/reusable-schema-fragments/988/1 "2020-01-24T02:18:13Z")

</div>

One feature I’ve notice dry-validation (or rather dry-schema) lacks is the ability to compose a schema from previously defined chunks.  
In plain-old Ruby, common functionality can be extracted into modules and added to other objects using `include`, `extend`, or `prepend`. In a Rails app, Concerns build upon that paradigm.

To give an example of the problem, one of the applications I’m working on has 30 or so instances of this:

```ruby
params do
  optional(:page).value(:integer, gteq?: 1)
  optional(:per_page).value(:integer, included_in?: [10, 25, 50])
  # Other parameters ...
end

```

Ideally, this would be pulled out into a `Pagination` fragment or similar, and then be reused across all of the contracts.  
ie.

```ruby
params do
  fragment(:pagination)
  # Other parameters ...
end

```

I’ve hacked out something that does work, the ergonomics are rather suboptimal:

```ruby
module Pagination
  def self.extended(object)
    super(object)
    object.instance_eval do
      optional(:page).value(:integer, gteq?: 1)
      optional(:per_page).value(:integer, included_in?: [10, 25, 50])
    end
  end
end

```

Then, in the contract:

```ruby
params do
  extend Pagination
  # Other parameters ...
end

```

It would be nice for to have a cleaner DSL available to do this since feature extraction plays a major part in DRYing up a codebase. 😉

---

<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:** [January 24, 2020, 12:12pm UTC](https://discourse.dry-rb.org/t/reusable-schema-fragments/988/2 "2020-01-24T12:12:48Z")

</div>

We are working on exactly this and sooner or later merging schemas will be available. Stay tuned 🙂
