Hello, I’m testing rom-rails with dry-ruby gems.
Specifically, I want to implement dry-validation schemes. So I want to implement uniqueness rule with scope. I have name, label, and platform_code. Name should be unique within the scope of label and platform_code. So I have something like this:
AbonentDevTypeSchema = Dry::Validation.Schema(AppSchema) do
configure do
option :abonent_dev_types
option :record, {}
def unique_scoped?(attr_name, scopes, value)
name, label, platform_code = scopes
not abonent_dev_types
.where{ id != record[:id] }
.where(name: name)
.where(label: label)
.where(platform_code: platform_code)
.exist?
end
end
required(:name).filled(:str?)
required(:label).filled(:str?)
required(:platform_code).filled(:str?)
rule(name: [:name, :label, :platform_code]) do |name, label, code|
name.unique_scoped? :name, [name, label, code]
end
end
Predicate gets kind of this as scopes:
[:rule, [:name, [:check, [[:name], [:predicate, [:unique_scoped?, [[:attr_name, :name], [:label, [...]], [:value, Undefined]]]]]]]], [:rule, [:name, [:check, [[:name], [:predicate, [:unique_scoped?, [[:attr_name, :name], [:label, [...]], [:value, Undefined]]]]]]]]]
So I would like to know, if there is a way to pass hash from rule to predicate, because I want to pass hash scopes like {name: name, code: code} and then process it in predicate. Any help?