# \[dry-validation\] Improve base rules with names

**URL:** https://discourse.dry-rb.org/t/dry-validation-improve-base-rules-with-names/1335
**Category:** Ideas
**Created:** [September 10, 2021, 5:01pm UTC](https://discourse.dry-rb.org/t/dry-validation-improve-base-rules-with-names/1335 "2021-09-10T17:01:15Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![wuarmin](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/wuarmin/32/641_2.png) [@wuarmin](https://discourse.dry-rb.org/u/wuarmin)
#### Post date: [September 10, 2021, 5:01pm UTC](https://discourse.dry-rb.org/t/dry-validation-improve-base-rules-with-names/1335/1 "2021-09-10T17:01:15Z")

</div>

Hey,  
in my current project I have lots of use-cases, where I would need to name base rules. Here’s some pseudo code:

```ruby
class BookingContract < Dry::Validation::Contract
  params do
    required(:reference_number).value(:string)
    required(:type).value(:string)
    ...
  end

  rule(name: :must_be_in_external_system, keys: [:reference_number, :type]) do |context:|
    payment = ext_payment_api.find(type: values[:type], reference_number: values[:reference_number])
    unless (payment.nil?)
      context[:payment] = payment
    else 
      base.failure("no payment available in external system!")
    end
  end

  rule(name: :payment_has_to_be_paid) do |context:|
    next if base_rule_error?(:must_be_in_external_system)
    base.failure("payment has not yet been paid") if context[:payment].paid?
  end

  rule(name: :correct_payment_type) do |context:|
    next if base_rule_error?(:must_be_in_external_system)
    base.failure("payment has to be of type TEST") if context[:payment].type != "TEST"
  end
end

```

What do you think about the idea? I think it would offer new options and make rules more readable.

Thanks  
best regards

---

<div class="post-metadata">

### Author: ![bryszard](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/bryszard/32/835_2.png) [@bryszard](https://discourse.dry-rb.org/u/bryszard)
#### Post date: [January 7, 2023, 10:46pm UTC](https://discourse.dry-rb.org/t/dry-validation-improve-base-rules-with-names/1335/2 "2023-01-07T22:46:11Z")

</div>

I agree here, this is the idea that I also had.

Right now the biggest disadvantage of `dry-validation` that I see is the fact that rules are anonymous. To understand what is validated in the rule, I need to read the code. The workaround for that are comment annotations, but that is not good enough, I’d rather have it as a code.

The internal rule name could be also useful in debugging and referencing. Eg. you could tell your colleague that `:must_be_in_external_system rule is incorrect, it does not need to be in external system`, instead of pointing the “rule on line 10” or “rule about [:reference\_number, :type]”.

My heart falls seeing that this idea stays here without any answer from 2021. Please, maintainers, at least tell us what do you think :).
