Skip to content

Commit

Permalink
Add some tests for new schema/params dry-v api
Browse files Browse the repository at this point in the history
  • Loading branch information
emaglio committed Nov 1, 2019
1 parent 87c1d83 commit b88bef2
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions test/validation/dry_validation_new_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b88bef2

Please sign in to comment.