dry-bot on master
[devtools] sync (compare)
flash-gordon on master
It's 2021 everyone! (compare)
flash-gordon on v1.5.0
dry-bot on master
[devtools] sync (compare)
flash-gordon on master
Set release date (compare)
flash-gordon on master
Add docs for fallbacks and cust… (compare)
dry-bot on master
[devtools] sync (compare)
flash-gordon on master
Update changelog.yml (compare)
require "dry/validation"
require "time_math"
schema = Dry::Validation.Schema do
configure do
def self.messages
super.merge(en: {errors: {ends_at_within_two_weeks_of_starts_at: 'must be within two weeks of start time'}})
end
end
required(:starts_at).filled(:time?)
required(:ends_at).filled(:time?)
validate(ends_at_within_two_weeks_of_starts_at: [:starts_at, :ends_at]) do |starts_at, ends_at|
two_weeks = 2 * 7 * 24 * 60
(ends_at - starts_at) <= two_weeks
end
end
valid_input = {
starts_at: TimeMath.day.decrease(Time.now, 10),
ends_at: Time.now,
}
schema.(valid_input).messages
# => {}
invalid_input = {
starts_at: TimeMath.day.decrease(Time.now, 20),
ends_at: Time.now,
}
schema.(invalid_input).messages
# => {:ends_at_within_two_weeks_of_starts_at=>["must be within two weeks of start time"]}
”true” => true
. For now we’ll go with just whitelisting. Some people freak out when seeing types in ruby :scream_cat:
dry-validations
, but am having a tough time. I have a field that needs to: 1. be optional 2. be an array, if it is present 3. have an each predicate run on the contents. It seems like chaining maybe(:array?).each(predicate)
does not throw errors when the value is not an array.
Success
object with the value
Failure
object with the value and the error message
class SubForm < Form
define! do
required(:foo).filled(:int?)
optional(:bar).maybe(:int?)
end
end
class MainForm < Form
define! do
required(:qux).filled(:int?)
optional(:bar).maybe do
schema(SubForm) # Using class directly
end
end
end
(byebug) params[:company_branch]
<ActionController::Parameters {"company_id"=>"9", "name"=>"", "address"=>"", "post_code"=>"", "phone"=>""} permitted: true>
(byebug) CreateCompanyBranch.new.call(params[:company_branch])
Failure({:company_id=>["must be an integer"], :name=>["must be filled"]})
(byebug) CreateCompanyBranch.new.call(params[:company_branch].to_h)
Failure({:name=>["must be filled"]})