I’ve started using dry-validation (v 0.10.4) for the first time today in a Trailblazer v1.1.2/Rails 5 app and I need to replicate some of the functionality provided by the file_validators gem for an optional image file upload.
At first I tried this:
configure do
option :form
def logo?(value)
# return true if value.nil? or value == a valid image upload
end
end
required(:image).maybe(:logo?)
But this fails when the image is nil, though I don’t understand why.
I have written the following (simplified/abbreviated) code:
configure do
option :form
def file?(value)
# true if an uploaded file
end
def allowable_file_type?(allowed, value)
# true if allowed includes value
end
def max_file_size?(max, value)
# true if value <= max
end
rule(nil_or_valid_image_upload: [:image]) do |image|
image.none? ^ (image.file? &
image.max_file_size?(2.megabytes) &
image.allowable_file_type?(['image/jpeg', 'image/jpg', 'image/png', 'image/gif']))
end
When running tests, it’s clear that the custom predicates on the right side of my xor logic are not being run and validation passes for invalid files.
Ideally, I would like to not have to run max_file_size? if file? returns false etc.
I assume I’m misusing the dry-validation API somehow and it feels like the latter code is too complex to be correct, so perhaps someone more experienced would kindly point the error of my ways?
Thanks in advance!