How to begin using Dry-Validation and/with Dry-Struct


I suspect I’m approaching this task wrongly.

In my web application, I would like to gather the URL params into a Class that can validate the incoming params the way dry-validation does, with the utility of Dry-Struct. The class would be passed to a command handler the would invoke the validation method before processing the contents of the command.

I’ve read the doc and understand how to define a validation schema, and how to define a typed struct. However, for what I’m trying to achieve I’d like to call the inside the struct class. Creating a schema and a typed struct for each logical URL entry seems too heavy or overdone.

The result of these efforts should produce a single value object-like class whose contents can be trusted according to the validation rules that filtered the input. Once I become comfortable with the approach I expect to create a couple hundred of these, one for each URL entry.

The URL input will always be a hash with string keys, could make them symbols in a preprocessor, and will likely be nested; with an average of eleven params. I’m interested in existence and value types and ranges.

Should I just use Dry-Struct and depend on it throwing an exception during create?

Or somehow incorporate Dry-Validation and call the schema method to validate in a more controlled fashion.

Or only use Dry-validation and use the validation method as needed, and use the result as the command object to pass onto the handler. note: yes I want dot-notation on the result.

Or wrap Dry-Validation into a class of my own making with the aid of Dry-Struct. This seems like a workable approach.

Help me take the first step.



Hi @skoona, feels to me like what you’re describing is exactly what dry-validation purports to do, except you’re wanting the final output to be a struct instead of just a hash of data. Could you do without the structs?


Sorry for the delayed response; the flu tried to take me out!

Yes, I do want the struct – it is the primary thing I’m after. I envision my command handlers will use the Class name of the structs to determine a course of action. I’m going to put an example together to see what I can come up with. Dry::Validation.Form seems to be a good starting point, since URL params are stringified, etc.