Is it safe to use Dry::Validation directly, or should we use it through Hanami::Validations?

Hey,

I’m interested in your opinion. If validation-contracts are needed in a Hanami 2 app. Is it safe to create contracts like

module PaymentSlice
  module Contracts
    class PaymentContract < Dry::Validation::Contract
      schema do
        required(:amount).value(:integer)
        # ...
      end
    end
  end
end

or is it better to use include Hanami::Validations like

module PaymentSlice
  module Contracts
    class PaymentContract
      include Hanami::Validations

      validations do
        required(:amount).value(:integer)
        # ...
      end
    end
  end
end

Thanks and best regards

I like to have my own top-level contract and always inherit from there class MyContract < BaseContract

This makes it easier to change my choices later if needed.

Inside base contracts I use hanami-validations though.

Yes it is. Hanami-validations is mostly a helper for validations at the params level but you have more control and more functionality if you use dry-validation standalone. We’re still exploring how exactly we want to structure validations in a hanami app and what exactly hanami-validations should do. Ideas are welcome btw!

2 Likes

Some examples where we use or plan to use hanami-validations (or dry-schema)

  • Action params validations.
  • Sidekiq Workers params validation.
  • Input File content validations (CSV rows)
  • Expected event structure published by other microservices (dry-schema)
  • Input data read from DBT views
1 Like