-
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #617 from dry-rb/rule_error_default
Add `Evaluator#schema_error?` and `Evaluator#rule_error?` [changelog] version: unreleased added: - "`schema_error?` rule helper (@waiting-for-dev)" - "`rule_error?` rule helper (@waiting-for-dev)"
- Loading branch information
Showing
4 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Dry::Validation::Evaluator do | ||
describe '#schema_error?' do | ||
let(:contract) do | ||
Class.new(Dry::Validation::Contract) do | ||
schema do | ||
required(:email).filled(:string) | ||
required(:name).filled(:string) | ||
end | ||
|
||
rule(:name) do | ||
key.failure('first introduce a valid email') if schema_error?(:email) | ||
end | ||
end | ||
end | ||
|
||
it 'checks for errors in given key' do | ||
expect(contract.new.(email: nil, name: 'foo').errors.to_h).to eql( | ||
email: ['must be a string'], | ||
name: ['first introduce a valid email'] | ||
) | ||
end | ||
end | ||
|
||
describe '#rule_error?' do | ||
let(:contract) do | ||
Class.new(Dry::Validation::Contract) do | ||
schema do | ||
required(:foo).filled(:string) | ||
end | ||
|
||
rule(:foo) do | ||
key.failure('failure added') | ||
key.failure('failure added after checking') if rule_error? | ||
end | ||
end | ||
end | ||
|
||
it 'checks for errors in current rule' do | ||
expect(contract.new.(foo: 'some@email.com').errors.to_h).to eql( | ||
foo: ['failure added', 'failure added after checking'] | ||
) | ||
end | ||
end | ||
end |