From b88bef20a90d1e3b2f8466f64ffe516b423c0892 Mon Sep 17 00:00:00 2001 From: Emanuele Magliozzi Date: Wed, 9 Oct 2019 15:35:44 +1100 Subject: [PATCH] Add some tests for new schema/params dry-v api --- test/validation/dry_validation_new_api.rb | 49 +++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/test/validation/dry_validation_new_api.rb b/test/validation/dry_validation_new_api.rb index 3bf16483..de6ee522 100644 --- a/test/validation/dry_validation_new_api.rb +++ b/test/validation/dry_validation_new_api.rb @@ -749,6 +749,55 @@ class AlbumForm < TestForm end end + class DryVWithSchemaAndParams < MiniTest::Spec + Foo = Struct.new(:age) + + class ParamsForm < TestForm + property :age + + validation do + params { required(:age).value(:integer) } + + rule(:age) { key.failure("value exceeded") if value > 999 } + end + end + + class SchemaForm < TestForm + property :age + + validation do + schema { required(:age).value(:integer) } + + rule(:age) { key.failure("value exceeded") if value > 999 } + end + end + + it "using params" do + model = Foo.new + form = ParamsForm.new(model) + form.validate(age: "99").must_equal true + form.sync + model.age.must_equal "99" + + form = ParamsForm.new(Foo.new) + form.validate(age: "1000").must_equal false + form.errors.messages.must_equal age: ["value exceeded"] + end + + it "using schema" do + model = Foo.new + form = SchemaForm.new(model) + form.validate(age: "99").must_equal false + form.validate(age: 99).must_equal true + form.sync + model.age.must_equal 99 + + form = SchemaForm.new(Foo.new) + form.validate(age: 1000).must_equal false + form.errors.messages.must_equal age: ["value exceeded"] + end + end + # Currenty dry-v don't support that option, it doesn't make sense # I've talked to @solnic and he plans to add a "hint" feature to show # more errors messages than only those that have failed.