These are chat archives for dry-rb/chat

23rd
Jun 2018
Jaromír Červenka
@Cervajz
Jun 23 2018 21:03
Hello:) Based on @radar 's book (nice job :+1:) I started to write my services as transactions and the validation layer in dry-validation. Staying with AR for now - just for storing data and for realationships only. Quick quesion: How do you feel about having different validation schemas for different trasnactions? As I am thinking about validations not on the "model level" (aka User.validates :email, presence: true) but on the "operation level". Does it make any sense? How do you use it?
For example:
# app/schemas/users/user_schema.rb:1
module Users
  UserSchema = Dry::Validation.Schema(ApplicationSchema) do
    required(:full_name).filled(:str?)
    required(:email).filled(:str?, :email?, unique?: :email)
    required(:country).filled(:str?)
    required(:password).filled(:str?, min_size?: 5)

    optional(:phone).maybe(:str?)
    optional(:admin).maybe(:bool?)
    optional(:blocked).maybe(:bool?)
  end
end

# app/schemas/users/confirm_new_password_schema.rb:1
module Users
  ConfirmNewPasswordSchema = Dry::Validation.Schema(ApplicationSchema) do
    required(:authentication_token).filled(:str?)
    required(:password).filled(:str?, min_size?: 5)
  end
end
Both of them are still related to User model but used in different situations
Tim Riley
@timriley
Jun 23 2018 21:45
@Cervajz yes, I 100% endorse this! Validations should be specific to each distinct user action or use case.