timriley on rich-component-dirs-config
Add component_dirs setting Rem… Scan component file for magic c… Make load_component easier to u… (compare)
timriley on rich-component-dirs-config
Fix wording Add Rubocop rule Clarify names Fixup naming and 4 more (compare)
flash-gordon on master
Add more predicates to mapping (compare)
timriley on rich-component-dirs-config
Use base Dry::Container missing… (compare)
timriley on rich-component-dirs-config
Make load_component easier to u… (compare)
dry-bot on master
[devtools] sync (compare)
flash-gordon on master
Make predicate inference truly … Update changelog.yml (compare)
flash-gordon on custom-builder-methods
Support for custom builder meth… (compare)
flash-gordon on or_nil-extension
Add Type#or_nil via builders ex… (compare)
dry-bot on master
[devtools] sync (compare)
flash-gordon on master
Fix changelog (compare)
dry-bot on master
[devtools] sync (compare)
Foo
is loaded first than Zoo
, then if I define like the standar way, it would be trigger an error that said Zoo
is undefined or something because Zoo
is not loaded yet, except I define require_relative 'zoo'
on top of foo.rb
file and I want to avoid this.
Zoo
in your case) to the top of my struct class files is exactly what I do in my apps.
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