Dry-Validation - no implicit conversion of Float into String on rules

658 Views Asked by At

I'm trying to validate the value of a key into my schema.

But I'm getting a no implicit conversion of Float into String because the value is a float and I'm using a regex to validate the format.

VALIDATION_PARAMETERS = Dry::Validation.Schema(ApplicationSchema) do
  required(:uid, :string).filled
  required(:value).filled

  rule(insulating_surface: [:uid, :value]) do |uid, value|
    uid.eql?('insulating_surface').then(value.format?(FLOAT_FORMAT))
  end
end

I also tried to convert Float into String but it returns undefined method format?' for #<String:0x0000557d8877a910>

rule(insulating_surface: [:uid, :value]) do |uid, value|
  uid.eql?('insulating_surface').then(value.to_s.format?(FLOAT_FORMAT))
end
1

There are 1 best solutions below

2
On

I'm not very familiar with dry-validation, but I see a lot of built-in predicates documented, including the float? one. Try the following instead:

rule(insulating_surface: [:uid, :value]) do |uid, value|
  uid.eql?('insulating_surface') > value.float?
end