Skip to content

Instantly share code, notes, and snippets.

@katafrakt
Last active February 4, 2020 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katafrakt/d328a37f3937d32fce3c26198ac733d9 to your computer and use it in GitHub Desktop.
Save katafrakt/d328a37f3937d32fce3c26198ac733d9 to your computer and use it in GitHub Desktop.
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