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 schema.call() 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.

James,

1 Like

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.

1 Like

Hi @skoona, looking to do this same exact thing. Any progress?

joelvh,

No luck with my original path. I ending up creating dedicated PORO for each web entry-point with a mix of validation strategies. Each class supports the method :valid?, :message, :value, :to_hash, and :action

While creating a large number of entry-point classes has taken some effort; I do think it has been worth it.

James,

Hi,

I was wondering exactly the same things - how to have the final output of Dry Validation be a Struct instead of hash of data?

I need the struct so that I can afterwards place it in a rails form.

My intention is the same as Trailblazer Reform - http://trailblazer.to/gems/reform/ - but Reform seems to be a bit behind right now, and I wanted to do something simple with just Dry Validation and Dry Struct.

I’ve done it using something along the lines of this, but I wonder if there’s an even more elegant way:

class ApplicationForm

  # Required dependency for ActiveModel::Errors
  extend ActiveModel::Naming
  extend ActiveModel::Translation

  attr_reader :errors

  def initialize(attributes = {})
    assign_attributes(attributes) if attributes
    @errors = ActiveModel::Errors.new(self)
  end

  def assign_attributes(attributes)
    attributes.each do |key, value|
      setter = :"#{key}="
      public_send(setter, value) if respond_to?(setter)
    end
  end

  class << self
    include Dry::Monads

    def validate(contract, params)
      outcome = contract.call(params)

      new.assign_attributes(outcome.to_h)
    end
end

and then I call it it with:

MyForm.validate(ValidationContract.new, params)

The best way to handle this is to write a dry-schema extension that would generate structs automatically based on the schema AST. This way you won’t have to duplicate key/attribute definitions.

Is there an example of this? Been wondering the same.

Alternatively, also been wondering about a simple way to “extract” the schema structure in a way that can convert to other types, like generating a GraphQL type from a dry-schema… I’ve done this to some degree, but not easily on nested schemas or combined with custom types.

I recently experimented with an OpenAPI property generator. It’s very basic but it gives an idea how AST conversion can work. It’s right here.

Great, thank you @solnic , I’ll have to dig a bit deeper to understand how the AST works, but I’m glad that it’s clear what the best way to approach this is. Thanks for the example as well.

Thank you!

1 Like