Validations only run with certain option requirements

I have a Contract that has the following setup:

class MyContract < Dry::Validation::Contract
  option :some_activerecord

  params do
     optional(:amount).maybe(:number?, gt?: 0)
  end

  rule do
    binding.pry
  end
end

Is there a way to only have the optional param validation to run if a certain property in some_activerecord model is equal to a value without having to create an actual rule? Trying to keep the params as short as possible, and wondering if this is possible?

I’ve tried:

optional(:amount).maybe(:number?, gt?: 0) { some_activerecord.value == 0 }

But this failed with undefined local variable or method.

Thanks!

Yeah, what you want to do is look at how custom types are supported in Dry Schema. Specifically:

Depending on your needs, you could define all of this within a schema and then reference that schema withing your contract (validator) or you could just use your custom type directly within your validator as documented in the Dry Validation documentation.