Should dry-validation/dry-schema replace all model validations?

I have been trying out dry-validation and dry-schema on a new Rails project, and finding it super handy for testing JSON schema. But I am wondering if it is meant to be used for validating a model itself. Is the recommended use only for validating external incoming params/JSON data in a controller, or do you recommend using it for validating a model itself before saving to the database in a model’s callback? For example, if I had a service object and I am just updating one attribute for User model and saving it, I’d like to have to something like below.

class User < ApplicationRecord
  before_save :verify_contract
  ...
  private

  def verify_contract
    UserContract.new.call(self.attributes.to_h)
  end
end

I may be way off, and wanted to check-in to see if I am heading a wrong direction on using dry-validation/dry-schema. Should the gems be used in conjunction with validations in models (traditional Rails way) or is it meant to entirely eradicate all the validation statements in models?

1 Like

Short answer: yes. I wouldn’t couple models with contracts though. A nice pattern is using an object that uses a contract to validate data then passes it into the model.

1 Like

OK. Thank you for the clarification @solnic !

1 Like