Last active
April 26, 2016 05:41
-
-
Save joevandyk/5acb7bf6711a53ca2fcdb1ec8b33fd2e 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
require 'dry-validation' | |
require 'rspec' | |
NestedSchema = Dry::Validation.JSON do | |
key(:skus).each do | |
optional(:cost).required(:decimal?) | |
optional(:price).required(:decimal?) | |
rule(price_or_cost: [:cost, :price]) do |cost, price| | |
cost.filled? | price.filled? | |
end | |
end | |
end | |
FlatSchema = Dry::Validation.JSON do | |
optional(:cost).required(:decimal?) | |
optional(:price).required(:decimal?) | |
rule(price_or_cost: [:cost, :price]) do |cost, price| | |
cost.filled? | price.filled? | |
end | |
end | |
RSpec.describe NestedSchema do | |
it do | |
expect(described_class.(skus: [{}]).messages).to_not be_empty | |
end | |
it do | |
expect(described_class.(skus: [{price: 1}]).messages).to be_empty | |
end | |
it do | |
expect(described_class.(skus: [{cost: 1}]).messages).to be_empty | |
end | |
end | |
RSpec.describe FlatSchema do | |
it do | |
expect(described_class.({}).messages).to_not be_empty | |
end | |
it do | |
expect(described_class.(price: 1).messages).to be_empty | |
end | |
it do | |
expect(described_class.(cost: 1).messages).to be_empty | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment