-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
each_spec.rb
233 lines (191 loc) · 6.7 KB
/
each_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# frozen_string_literal: true
require 'dry/validation/contract'
RSpec.describe Dry::Validation::Contract, 'Rule#each' do
subject(:contract) { contract_class.new }
context 'using a block' do
let(:contract_class) do
Class.new(Dry::Validation::Contract) do
def self.name
'TestContract'
end
params do
required(:nums).array(:integer)
optional(:hash).hash do
optional(:another_nums).array(:integer)
end
end
rule(:nums).each do
key.failure('invalid') if value < 3
end
rule(hash: :another_nums).each do
key.failure('invalid') if value < 3
end
rule(:nums).each do |context:|
context[:sum] ||= 0
context[:sum] += value
end
end
end
it 'applies rule when an item passed schema checks' do
expect(contract.(nums: ['oops', 1, 4, 0]).errors.to_h)
.to eql(nums: { 0 => ['must be an integer'], 1 => ['invalid'], 3 => ['invalid'] })
end
it 'applies rule to nested values when an item passed schema checks' do
expect(contract.(nums: [4], hash: { another_nums: ['oops', 1, 4] }).errors.to_h)
.to eql(hash: { another_nums: { 0 => ['must be an integer'], 1 => ['invalid'] } })
end
it 'passes block options' do
expect(contract.(nums: [10, 20]).context[:sum]).to eql(30)
end
end
context 'using a simple macro' do
let(:contract_class) do
Class.new(Dry::Validation::Contract) do
def self.name
'TestContract'
end
register_macro(:even?) do
key.failure('invalid') unless value.even?
end
params do
required(:nums).filled(:array)
optional(:hash).hash do
optional(:another_nums).filled(:array)
end
end
rule(:nums).each(:even?)
rule('hash.another_nums').each(:even?)
end
end
it 'applies rule when an item passed schema checks' do
expect(contract.(nums: [2, 3]).errors.to_h)
.to eql(nums: { 1 => ['invalid'] })
end
it 'applies rule to nested values when an item passed schema checks' do
expect(contract.(nums: [4], hash: { another_nums: [2, 3] }).errors.to_h)
.to eql(hash: { another_nums: { 1 => ['invalid'] } })
end
end
context 'using multiple macros' do
let(:contract_class) do
Class.new(Dry::Validation::Contract) do
def self.name
'TestContract'
end
register_macro(:even?) do
key.failure('invalid') unless value.even?
end
register_macro(:below_ten?) do
key.failure('too big') unless value < 10
end
params do
required(:nums).filled(:array)
optional(:hash).hash do
required(:another_nums).filled(:array)
end
end
rule(:nums).each(:even?, :below_ten?)
rule(%i[hash another_nums]).each(:even?, :below_ten?)
end
end
it 'applies rules when an item passed schema checks' do
expect(contract.(nums: [2, 15]).errors.to_h)
.to eql(nums: { 1 => ['invalid', 'too big'] })
end
it 'applies rules for nested values when an item passed schema checks' do
expect(contract.(nums: [2], hash: { another_nums: [2, 15] }).errors.to_h)
.to eql(hash: { another_nums: { 1 => ['invalid', 'too big'] } })
end
end
context 'using a macro with args' do
let(:contract_class) do
Class.new(Dry::Validation::Contract) do
def self.name
'TestContract'
end
register_macro(:min) do |macro:|
min = macro.args[0]
key.failure('invalid') if value < min
end
params do
required(:nums).array(:integer)
optional(:hash).hash do
optional(:another_nums).array(:integer)
end
end
rule(:nums).each(min: 3)
rule(hash: :another_nums).each(min: 3)
end
end
it 'applies rule when an item passed schema checks' do
expect(contract.(nums: ['oops', 1, 4, 0]).errors.to_h)
.to eql(nums: { 0 => ['must be an integer'], 1 => ['invalid'], 3 => ['invalid'] })
end
it 'applies rule to nested values when an item passed schema checks' do
expect(contract.(nums: [4], hash: { another_nums: ['oops', 1, 4] }).errors.to_h)
.to eql(hash: { another_nums: { 0 => ['must be an integer'], 1 => ['invalid'] } })
end
end
context 'using a macro with multiple args' do
let(:contract_class) do
Class.new(Dry::Validation::Contract) do
def self.name
'TestContract'
end
register_macro(:between) do |macro:|
min, max = macro.args[0..1]
key.failure('invalid') unless (min..max).cover?(value)
end
params do
required(:nums).array(:integer)
optional(:hash).hash do
optional(:another_nums).array(:integer)
end
end
rule(:nums).each(between: [3, 5])
rule(hash: :another_nums).each(between: [3, 5])
end
end
it 'applies rule when an item passed schema checks' do
expect(contract.(nums: ['oops', 4, 0, 6]).errors.to_h)
.to eql(nums: { 0 => ['must be an integer'], 2 => ['invalid'], 3 => ['invalid'] })
end
it 'applies rule with nested values when an item passed schema checks' do
expect(contract.(nums: [4], hash: { another_nums: ['oops', 4, 0] }).errors.to_h)
.to eql(hash: { another_nums: { 0 => ['must be an integer'], 2 => ['invalid'] } })
end
end
context 'using multiple macros with args' do
let(:contract_class) do
Class.new(Dry::Validation::Contract) do
def self.name
'TestContract'
end
register_macro(:min) do |macro:|
min = macro.args[0]
key.failure('invalid') if value < min
end
register_macro(:max) do |macro:|
max = macro.args[0]
key.failure('invalid') if value > max
end
params do
required(:nums).array(:integer)
optional(:hash).hash do
optional(:another_nums).array(:integer)
end
end
rule(:nums).each(min: 3, max: 5)
rule(hash: :another_nums).each(min: 3, max: 5)
end
end
it 'applies rules when an item passed schema checks' do
expect(contract.(nums: ['oops', 4, 0, 6]).errors.to_h)
.to eql(nums: { 0 => ['must be an integer'], 2 => ['invalid'], 3 => ['invalid'] })
end
it 'applies rules for nested values when an item passed schema checks' do
expect(contract.(nums: [4], hash: { another_nums: ['oops', 4, 0] }).errors.to_h)
.to eql(hash: { another_nums: { 0 => ['must be an integer'], 2 => ['invalid'] } })
end
end
end