# Using boolean-like predicates as constraints in dry-types

**URL:** <https://discourse.dry-rb.org/t/using-boolean-like-predicates-as-constraints-in-dry-types/1807>\
**Category:** dry-types\
**Created:** [June 30, 2024, 12:07am UTC](https://discourse.dry-rb.org/t/using-boolean-like-predicates-as-constraints-in-dry-types/1807 "2024-06-30T00:07:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Damian](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/damian/32/963_2.png) [@Damian](https://discourse.dry-rb.org/u/Damian)\
**Post date:** [June 30, 2024, 12:07am UTC](https://discourse.dry-rb.org/t/using-boolean-like-predicates-as-constraints-in-dry-types/1807/1 "2024-06-30T00:07:41Z")

</div>

When specifying a schema using `dry-validations`, I can do something like this to check for a particular type of UUID:

```ruby
required(:uuid).value(:string, :uuid_v4?)

```

This is invoking the `uuid_v4?` predicate provided by `dry-logic`. I would like to simplify this by creating a custom type to use instead. Something like:

```ruby
required(:uuid).filled(My::Types::ContactUuid)

```

However, I am having trouble creating this custom type. The `dry-types` constraints docs say that “Under the hood it uses `dry-logic` and all of its predicates are supported.” However, the examples given only show predicates that take an argument:

```ruby
string = Types::String.constrained(min_size: 3)

```

If the predicate does not take an argument, like `uuid_v4` (and many other boolean-like predicates), then how can it be used in the `constrained` method?

I tried everything I can think of and the only thing that works is to pass a dummy argument. I use `nil` here, but literally anything should work.

```ruby
module My
  module Types
    include Dry.Types()

    ContactUuid = Types::String.constrained(uuid_v4: nil)
  end
end

```

Is this expected behavior? It appears to be so from the implementation of `self.Rule`:

```ruby
# frozen_string_literal: true

module Dry
  # Helper methods for constraint types
  #
  # @api public
  module Types
    # @param [Hash] options
    #
    # @return [Dry::Logic::Rule]
    #
    # @api public
    def self.Rule(options)
      rule_compiler.(
        options.map { |key, val|
          ::Dry::Logic::Rule::Predicate.build(
            ::Dry::Logic::Predicates[:"#{key}?"]
          ).curry(val).to_ast
        }
      ).reduce(:and)
    end

    # @return [Dry::Logic::RuleCompiler]
    #
    # @api private
    def self.rule_compiler
      @rule_compiler ||= ::Dry::Logic::RuleCompiler.new(::Dry::Logic::Predicates)
    end
  end
end

```

Am I missing something? Is there a better way?

Thank you!

---

<div class="post-metadata">

**Author:** ![flash-gordon](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/flash-gordon/32/494_2.png) [@flash-gordon](https://discourse.dry-rb.org/u/flash-gordon)\
**Post date:** [July 1, 2024, 10:45am UTC](https://discourse.dry-rb.org/t/using-boolean-like-predicates-as-constraints-in-dry-types/1807/2 "2024-07-01T10:45:26Z")

</div>

It seems you can also use an array of symbols `Types::String.constrained([:uuid_v4])`. I think we should allow accepting a single symbol, `.constrained(:uuid_v4)` for cases when there’s just one predicate. If you agree, can you submit a request on github?

---

<div class="post-metadata">

**Author:** ![Damian](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/damian/32/963_2.png) [@Damian](https://discourse.dry-rb.org/u/Damian)\
**Post date:** [July 3, 2024, 6:19pm UTC](https://discourse.dry-rb.org/t/using-boolean-like-predicates-as-constraints-in-dry-types/1807/3 "2024-07-03T18:19:40Z")

</div>

Thank you! That’s a good trick that I didn’t think of. 🤔

This would only work if the array only contained boolean-like predicates. (By “boolean-like,” I mean predicates that don’t take any arguments. I just learned from Wikipedia that you can call these “nullary predicates”). I don’t think it would work if you had to mix the boolean-like predicates with other predicates that require an argument (`gt: 3` or similar, which may also be called “unary predicates”–thanks Wikipedia!).

Thank you for your help! I will open an issue on Github.
