-
-
Save francois/0367f5ecd94182c774f6264ed6bbbb7b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NewFamilyForm = Dry::Validation.Form do | |
required(:email).filled(format?: /.@.+[.][a-z]{1}/i) | |
# I've tried various things here, all to no effect | |
# required(:adult).filled(:array?, min_size?: 1) | |
required(:adult).value(:array?, :filled?, min_size?: 1) | |
required(:adult).each do | |
none? | (str? & min_size?(1)) | |
end | |
required(:child).each do | |
none? | (str? & min_size?(1)) | |
end | |
end | |
class NewFamilyFormTest < Minitest::Test | |
def test_requires_email | |
result = NewFamilyForm.call({}) | |
assert_equal ["is missing"], result.errors[:email] | |
assert_equal({}, result.output) | |
end | |
def test_requires_adult_key | |
result = NewFamilyForm.call({}) | |
assert_equal ["is missing"], result.errors[:adult] | |
assert_equal({}, result.output) | |
end | |
# This test FAILS, what must I change to make it green? | |
def test_requires_at_least_one_adult | |
result = NewFamilyForm.call("adult" => []) | |
assert_equal 1, result.errors[:adult], result.errors.inspect | |
assert_equal({adult: []}, result.output) | |
end | |
# I can live with this | |
def test_ignores_empty_names | |
result = NewFamilyForm.call("adult" => ["francois", ""]) | |
assert_nil result.errors[:adult] | |
assert_equal({adult: ["francois", nil]}, result.output) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1) Failure: | |
NewFamilyFormTest#test_requires_at_least_one_adult [test/test_new_family_form.rb:19]: | |
{:email=>["is missing"], :child=>["is missing"]}. | |
Expected: 1 | |
Actual: nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} | |
#=> errors: missing the email field, missing the adult field | |
{"email" => "", | |
"adult" => [], | |
"child" => []} | |
#=> errors: email doesn't look like an email address, must have at least one adult | |
{"email" => "francois", | |
"adult" => ["", " ", "\t"], | |
"child" => []} | |
#=> errors: email doesn't look like an email address, must have at least one adult |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# NOTE: I can live with nil values, because I can compact before using | |
{"email" => "francois@teksol.info", | |
"adult" => ["francois", "\t"], | |
"child" => [" ", "", nil]} | |
#=> {email: "francois@teksol.info", adult: ["francois"], child: []} | |
{"email" => "francois@teksol.info", | |
"adult" => [" ", "francois"], | |
"child" => ["harry", "ron", "hermione", "ginny", "fred", "george", " "]} | |
#=> {email: "francois@teksol.info", adult: ["francois"], child: %w(harry ron hermione ginny fred george)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment