# Proper usage of dry-transaction with dry-validation

**URL:** <https://discourse.dry-rb.org/t/proper-usage-of-dry-transaction-with-dry-validation/594>\
**Category:** Support\
**Created:** [July 23, 2018, 10:57pm UTC](https://discourse.dry-rb.org/t/proper-usage-of-dry-transaction-with-dry-validation/594 "2018-07-23T22:57:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SheepD](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@SheepD](https://discourse.dry-rb.org/u/SheepD)\
**Post date:** [July 23, 2018, 10:57pm UTC](https://discourse.dry-rb.org/t/proper-usage-of-dry-transaction-with-dry-validation/594/1 "2018-07-23T22:57:18Z")

</div>

Currently in my organization, we are using dry-validation a lot with dry-transaction for the usual CRUD operations in our app. One thing that i find a bit weird is that we had to write a wrapper for the dry-validation schemas that looked like this to wrap the result of the schema in a dry-monad result

```ruby
    result = schema.call(input)

    if result.success?
      Success(result.to_h)
    else
      Failure(result.errors)
    end

```

so upon digging a bit deeper, I found out that dry-validation schemas return `Dry::Validation::Result` instead of `Dry::Monad::Result`. So my questions would be…

1. is there a better way to incorporate dry-validation with dry-transaction? (perhaps we’ve been using it wrong in our organization)
2. are there plans to one day use dry-monad results as the result of dry-transaction schemas?

---

<div class="post-metadata">

**Author:** ![rickenharp](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/rickenharp/32/12_2.png) [@rickenharp](https://discourse.dry-rb.org/u/rickenharp)\
**Post date:** [July 25, 2018, 8:19am UTC](https://discourse.dry-rb.org/t/proper-usage-of-dry-transaction-with-dry-validation/594/2 "2018-07-25T08:19:35Z")

</div>

There’s already a method on Dry::Validation::Result to handle this: [to\_monad](https://www.rubydoc.info/gems/dry-validation/Dry%2FValidation%2FResult:to_monad) aka `to_either`

So you can shorten the code to

```auto
Dry::Validation.load_extensions(:monads)
result = schema.call(input)
result.to_monad

```

and then handle the `to_h` in the following step.

---

<div class="post-metadata">

**Author:** ![SheepD](https://avatars.discourse-cdn.com/v4/letter/s/51bf81/32.png) [@SheepD](https://discourse.dry-rb.org/u/SheepD)\
**Post date:** [July 25, 2018, 9:45pm UTC](https://discourse.dry-rb.org/t/proper-usage-of-dry-transaction-with-dry-validation/594/3 "2018-07-25T21:45:21Z")

</div>

thanks @rickenharp. that does make it easier to work with schemas in the context of transactions. much cleaner too!

---

<div class="post-metadata">

**Author:** ![alexandru-calinoiu](https://yyz1.discourse-cdn.com/flex031/user_avatar/discourse.dry-rb.org/alexandru-calinoiu/32/98_2.png) [@alexandru-calinoiu](https://discourse.dry-rb.org/u/alexandru-calinoiu)\
**Post date:** [July 30, 2018, 10:34am UTC](https://discourse.dry-rb.org/t/proper-usage-of-dry-transaction-with-dry-validation/594/4 "2018-07-30T10:34:24Z")

</div>

I on the other hand I use the validation output to create an entity and pass it along to the next steps for processing.

```ruby
          if validation.success?
            Right(Entities::SomeEntity.new(validation.output))
          else
            Left(validation.errors)
          end

```
