Is it possible to access the raw input before coercion?

I’m trying to validate a time parameter that should be iso8601 formatted. Is there a way to check the raw string input against a regex before it is coerced into a Time object when using Dry::Validation.Form? Because using

required(:from, :time).filled(:time?).format(/complicated_regex/)

will always be correctly formatted, since it’s the string representation of already coerced Time object.

Unfortunately no, but I’m glad you asked about this, as it is precisely one of the big things I’m planning to improve in dry-validation 1.0. There’s a big missing concept here, which is validating input prior coercion. I don’t know how the API will look like, but something like this, more or less:

required(:from).coerce_to(:time, format: :iso8601)

What will happen in such case, is that since there are no rules for the output value, it will assume that coercion and its result is everything we need to check, which is:

  1. is the input value in the right iso8601 format?
  2. is the output a time object?
1 Like

Cool, I’m looking forward to that!

In the meantime, I solved this by defining a custom type like this:

IsoTime = Types::Time.constructor do |values|
    begin
      ::Time.iso8601(values)
    rescue ArgumentError
      values
    end
  end

and using type_specs (which aren’t linked from the main navigation, btw):

configure do
  config.type_specs = true
end

required(:from, Types::IsoTime).filled(:time?)

Ah yeah, custom types can be used for that…for now :slight_smile: