# Custom predicates in dry-schema

**URL:** <https://discourse.dry-rb.org/t/custom-predicates-in-dry-schema/705>\
**Category:** Support\
**Created:** [February 6, 2019, 12:49pm UTC](https://discourse.dry-rb.org/t/custom-predicates-in-dry-schema/705 "2019-02-06T12:49:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![klaustopher](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/klaustopher/32/336_2.png) [@klaustopher](https://discourse.dry-rb.org/u/klaustopher)\
**Post date:** [February 6, 2019, 12:49pm UTC](https://discourse.dry-rb.org/t/custom-predicates-in-dry-schema/705/1 "2019-02-06T12:49:52Z")

</div>

Hi there,

I am currently in the process of evalating using `dry-schema` for some input validation before hitting our ActiveRecord classes in a Rails application. Everything is working nice and smoothely. But now I wanted to add some custom predicates to match some of our application inputs. For example, we use prefixed-uuids for all of our objects and I want to validate with a Regex that only ids with a certain prefix can be passed in:

```ruby
module CustomPredicates
  include Dry::Logic::Predicates

  def uuid_with_prefix?(prefix, input)
    input =~ /\A#{prefix}_[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\Z/
  end
end

```

```ruby
class ApplicationSchema < Dry::Schema::JSON
  # here I would like to specify where the predicates are loaded from
end

```

The documentation says at one point that inheriting like this is possible, but that seems not to be correct. I have opened a [GH issue](https://github.com/dry-rb/dry-rb.org/issues/284) about this. I am not too fixated on this, but it would be the nicest way to reuse logic.

```ruby
class PostSchema < ApplicationSchema
  required(:text).filled(:string?)
  required(:author).filled(:string?, uuid_with_prefix?: 'user')
end

```

I have tried all different method of how to specify predicates that are documented in the other gems like `confgure { config.predicates = CustomPredicates }` but I suspect that in `dry-schema` I somehow need access to the `PredicateRegistry` but it is not exposed anywhere.

Is there maybe something still missing, that this cannot be done yet? Should I better stick with the old version of `dry-validation` until the extraction process is done? Or am I completely missing something?

If somebody points me into the right direction, I would be totally happy to contribute a section of the readme that describes how to do this.

---

<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:** [February 7, 2019, 2:38pm UTC](https://discourse.dry-rb.org/t/custom-predicates-in-dry-schema/705/2 "2019-02-07T14:38:52Z")

</div>

Hey,

Custom predicates will not be supported in dry-schema. The alternative is as simple as defining methods on validation contracts, which will be available in dry-validation 1.0.0. I already started working on it and we could have an early alpha/beta ready later this month, if there’s interest in testing it early.

Cheers!

---

<div class="post-metadata">

**Author:** ![klaustopher](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/klaustopher/32/336_2.png) [@klaustopher](https://discourse.dry-rb.org/u/klaustopher)\
**Post date:** [February 7, 2019, 2:50pm UTC](https://discourse.dry-rb.org/t/custom-predicates-in-dry-schema/705/3 "2019-02-07T14:50:02Z")

</div>

Ok, so for now, I will work with the current version of `dry-validation`

---

<div class="post-metadata">

**Author:** ![dhempy](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/dhempy/32/765_2.png) [@dhempy](https://discourse.dry-rb.org/u/dhempy)\
**Post date:** [May 9, 2022, 3:43pm UTC](https://discourse.dry-rb.org/t/custom-predicates-in-dry-schema/705/4 "2022-05-09T15:43:27Z")

</div>

Hi, @solnic . I’m in a similar situation. I’ve inherited a legacy app that has custom predicates, similar to above. I have no prior little dry-validation experience, but have been learning a lot in the past few days as we address vulnerability concerns by updating gems, including dry-validation from 0.13 to 1.x.

You suggest, “defining methods on validation contracts.” I’ve not been able to find references on what that looks like, based on that name. I’ve been all up and down the dry-validation 1.7 docs and have not found any examples of writing a custom validation method when simple type/value checks are insufficient.

Can you share an example of what that looks like, or link to current docs that describe this?

---

<div class="post-metadata">

**Author:** ![alassek](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alassek/32/314_2.png) [@alassek](https://discourse.dry-rb.org/u/alassek)\
**Post date:** [May 11, 2022, 12:39am UTC](https://discourse.dry-rb.org/t/custom-predicates-in-dry-schema/705/5 "2022-05-11T00:39:05Z")

</div>

Take a look at [contract macros](https://dry-rb.org/gems/dry-validation/1.5/macros/). Either that, or just rewriting as [rules](https://dry-rb.org/gems/dry-validation/1.5/rules/)
