Is there a way to coerce an input class instance that responds to
to_h into an actual hash before running validations?
Say I have an attribute that is being verified by a
.hash(NestedSchema) and that, in turn, contains an attribute and verifies it with
.hash(DeeperNestedSchema). Input arguments to these attributes are long and complex instances of different classes (they come from the “outside world” and are not regular Rails params). These classes, among other things, “know” how to be normalized into a hash (responds to
to_h message). Is there a way to convert these instances into hashes and then operate on that?
Otherwise validations do work, but coerced params contain the same input instances of classes and not coerced hashes that can be used to create / update data.
So the idea is something like this:
class ParentSchema < Dry::Validation::Contract required(:user)hash(:to_h, ChildSchema) end ChildSchema = Dry::Schema.Params do required(:post).hash(:to_h, PostSchema) end
And not when I say
ParentSchema.new.(@instance_of_some_class) I get its
to_h version (which is just a hash of validated / parsed / filtered attributes).