Skip to content

Instantly share code, notes, and snippets.

@katafrakt katafrakt/validation.rb
Last active Feb 4, 2020

Embed
What would you like to do?
require 'hanami/validations'
class ReservationValidation
include Hanami::Validations
predicate :before? do |other, current|
current < other
end
validations do
required(:check_in).filled
required(:check_out).filled
rule valid_dates: [:check_in, :check_out] do |check_in, check_out|
check_in.before?(check_out)
end
end
end
date1 = Date.new(2020,1,20)
date2 = Date.new(2010,2,1)
ReservationValidation.new(check_in: date1, check_out: date2).validate.success?
# => true
ReservationValidation.new(check_in: date2, check_out: date1).validate.success?
# => false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.