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

**URL:** <https://discourse.dry-rb.org/t/how-to-begin-using-dry-validation-and-with-dry-struct/481>\
**Category:** Support\
**Created:** [February 20, 2018, 3:13pm UTC](https://discourse.dry-rb.org/t/how-to-begin-using-dry-validation-and-with-dry-struct/481 "2018-02-20T15:13:52Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![skoona](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/skoona/32/180_2.png) [@skoona](https://discourse.dry-rb.org/u/skoona)\
**Post date:** [February 20, 2018, 3:13pm UTC](https://discourse.dry-rb.org/t/how-to-begin-using-dry-validation-and-with-dry-struct/481/1 "2018-02-20T15:13:52Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![timriley](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/timriley/32/25_2.png) [@timriley](https://discourse.dry-rb.org/u/timriley)\
**Post date:** [February 20, 2018, 10:25pm UTC](https://discourse.dry-rb.org/t/how-to-begin-using-dry-validation-and-with-dry-struct/481/2 "2018-02-20T22:25:34Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![skoona](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/skoona/32/180_2.png) [@skoona](https://discourse.dry-rb.org/u/skoona)\
**Post date:** [March 1, 2018, 3:58pm UTC](https://discourse.dry-rb.org/t/how-to-begin-using-dry-validation-and-with-dry-struct/481/3 "2018-03-01T15:58:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![joelvh](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/joelvh/32/276_2.png) [@joelvh](https://discourse.dry-rb.org/u/joelvh)\
**Post date:** [July 31, 2019, 1:21am UTC](https://discourse.dry-rb.org/t/how-to-begin-using-dry-validation-and-with-dry-struct/481/4 "2019-07-31T01:21:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![skoona](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/skoona/32/180_2.png) [@skoona](https://discourse.dry-rb.org/u/skoona)\
**Post date:** [September 30, 2019, 9:03pm UTC](https://discourse.dry-rb.org/t/how-to-begin-using-dry-validation-and-with-dry-struct/481/5 "2019-09-30T21:03:16Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![mtomov](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/mtomov/32/543_2.png) [@mtomov](https://discourse.dry-rb.org/u/mtomov)\
**Post date:** [October 21, 2019, 9:51am UTC](https://discourse.dry-rb.org/t/how-to-begin-using-dry-validation-and-with-dry-struct/481/6 "2019-10-21T09:51:21Z")

</div>

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/](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)
```

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [October 21, 2019, 4:20pm UTC](https://discourse.dry-rb.org/t/how-to-begin-using-dry-validation-and-with-dry-struct/481/7 "2019-10-21T16:20:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![joelvh](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/joelvh/32/276_2.png) [@joelvh](https://discourse.dry-rb.org/u/joelvh)\
**Post date:** [October 21, 2019, 4:52pm UTC](https://discourse.dry-rb.org/t/how-to-begin-using-dry-validation-and-with-dry-struct/481/8 "2019-10-21T16:52:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![solnic](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/solnic/32/779_2.png) [@solnic](https://discourse.dry-rb.org/u/solnic)\
**Post date:** [October 22, 2019, 7:28am UTC](https://discourse.dry-rb.org/t/how-to-begin-using-dry-validation-and-with-dry-struct/481/9 "2019-10-22T07:28:19Z")

</div>

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](https://github.com/dry-rb/dry-schema/blob/6697100a17ece928e736f849fe06c71df1a44b02/lib/dry/schema/extensions/open_api/schema_compiler.rb).

---

<div class="post-metadata">

**Author:** ![mtomov](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/mtomov/32/543_2.png) [@mtomov](https://discourse.dry-rb.org/u/mtomov)\
**Post date:** [October 22, 2019, 1:10pm UTC](https://discourse.dry-rb.org/t/how-to-begin-using-dry-validation-and-with-dry-struct/481/10 "2019-10-22T13:10:11Z")

</div>

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!
