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
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:
- is the input value in the right iso8601 format?
- is the output a time object?
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|
and using type_specs (which aren’t linked from the main navigation, btw):
config.type_specs = true
Ah yeah, custom types can be used for that…for now