Open tarolandia opened 10 years ago
@tarolandia You can achieve something similar with the example below.
class Foo
include Hatch
attr :foo
errors = []
certify(:foo, errors) do |foo|
errors.clear
errors << "no foo" if foo.nil?
errors << "foo is bar" if foo == "bar"
errors.empty?
end
not_foo = Foo.hatch
not_foo.errors.on(:foo)
# => ["no foo"]
not_foo = Foo.hatch(foo: "bar")
not_foo.errors.on(:foo)
# => ["foo is bar"]
Foo.hatch(foo: "foo").valid?
# => true
On the plus side this
:foo
is valid or not.errors
array could be any object you need, which makes it easy to handle.But...
certify(:email, :not_empty)
I already know what was intended without needing to read the block.@tarolandia what do you think?
Improve Hatch to allow multiple validations for each parameter/property.
This feature will allow the developer to use multiple error messages when validating.