/
schema_spec.rb
493 lines (396 loc) · 14.1 KB
/
schema_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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# frozen_string_literal: true
RSpec.describe Dry::Types::Schema do
subject(:schema) do
Dry::Types['hash'].schema(
name: 'coercible.string',
age: 'integer',
active: 'params.bool'
)
end
it_behaves_like 'a constrained type' do
let(:type) { schema }
end
describe '#each' do
it 'iterates over keys' do
expect(schema.each.to_a).to eql(schema.name_key_map.values_at(:name, :age, :active))
end
it 'makes schema act as Enumerable' do
expect(schema.map(&:name)).to eql(%i[name age active])
end
it 'returns enumerator' do
enumerator = schema.each
expect(enumerator.next).to be(schema.name_key_map[:name])
end
end
describe '#key?' do
it 'checks key presence' do
expect(schema.key?(:name)).to be true
expect(schema.key?(:missing)).to be false
end
end
describe '#constrained?' do
subject(:type) { Dry::Types['nominal.hash'].schema({}) }
it 'is a contrained type' do
expect(type).to be_constrained
end
context 'sum' do
subject(:sum) { type.strict | type.strict }
it 'produces a constrained sum' do
expect { sum.(foo: 1) }.to raise_error(Dry::Types::UnknownKeysError)
end
end
end
describe '#key' do
it 'fetches a key type' do
expect(schema.key(:name)).to be(schema.name_key_map[:name])
end
it 'raises a key error if key is unknown' do
expect { schema.key(:missing) }.to raise_error(KeyError, 'key not found: :missing')
end
it 'accepts a fallback' do
expect(schema.key(:missing, :fallback)).to eql(:fallback)
end
it 'accepts a fallback block' do
expect(schema.key(:missing) { :fallback }).to eql(:fallback)
end
it 'discards schema constructor' do
expect(schema.constructor(&:to_hash).key(:name)).to be(schema.name_key_map[:name])
end
context 'nested schema' do
let(:name_schema) do
Dry::Types['hash'].schema(first_name: 'string', last_name: 'string')
end
subject(:schema) do
Dry::Types['hash'].schema(name: name_schema)
end
it 'still discards constructor' do
expect(schema.constructor(&:to_hash).key(:name).type).to eql(name_schema)
end
end
end
describe '#call' do
let(:type) { Dry::Types['nominal.hash'].schema({}).strict }
it 'takes a block for fallback case' do
expect(type.(extra_key: :invalid) { 'fallback' }).to eql('fallback')
end
end
describe 'omittable keys' do
subject(:schema) do
Dry::Types['strict.hash'].schema(
age: Dry::Types['strict.integer'].meta(omittable: true)
)
end
example 'keys can be set as not required via meta' do
expect(schema.({})).to eql({})
end
end
describe 'hash schema' do
subject(:type) { Dry::Types['nominal.hash'] }
let(:phone_struct) do
Struct.new(:prefix, :number) do
def self.name
'Phone'
end
def self.to_ary
[prefix, number]
end
end
end
before do
PhoneType = Dry::Types::Nominal.new(phone_struct).constructor(-> args { phone_struct.new(*args) })
end
after do
Object.send(:remove_const, :PhoneType)
end
let(:hash_schema) do
{
name: 'coercible.string',
age: 'strict.integer',
active: 'params.bool',
phone: PhoneType
}
end
let(:primitive) do
type.meta(my: :metadata)
end
subject(:hash) { primitive.schema(hash_schema) }
let(:valid_input) { { name: 'John', age: 23, active: true, phone: phone_struct.new(1, 23) } }
let(:phone) { PhoneType.primitive }
it_behaves_like Dry::Types::Nominal do
let(:type) { Dry::Types['nominal.hash'].schema(hash_schema) }
end
it_behaves_like 'Dry::Types::Nominal#meta' do
let(:type) { Dry::Types['nominal.hash'].schema(hash_schema) }
end
context 'members with default values' do
let(:hash) do
primitive.schema({
**hash_schema,
age: Dry::Types['strict.integer'].default(21)
})
end
it 'resolves missing keys with default values' do
params = { name: 'Jane', active: true, phone: [] }
expect(hash[params][:age]).to eql(21)
end
end
it 'preserves metadata' do
expect(hash.meta[:my]).to eql(:metadata)
end
it 'has a Hash primitive' do
expect(hash.primitive).to be(::Hash)
end
it 'is callable via #[]' do
params = { name: :Jane, age: 21, active: true, phone: [] }
expect(hash[params]).to eql(hash.call(params))
end
it 'builds hash using provided schema' do
user_hash = hash.call(
name: :Jane, age: 21, active: true,
phone: ['+48', '123-456-789']
)
expect(user_hash)
.to eql(
name: 'Jane', age: 21, active: true,
phone: phone.new('+48', '123-456-789')
)
end
it 'applies member types' do
input = { name: :John, age: 21, active: 'true', phone: %w[1 234] }
result = hash.try(input)
expect(result).to be_success
expect(result.input).to eql(name: 'John', age: 21, active: true, phone: phone.new('1', '234'))
end
it 'keeps original values when applying a member type fails' do
input = { age: 'twenty one', active: '0', name: 'John', phone: %w[1 234] }
result = hash.try(input)
expect(result).to be_failure
expect(result.input).to eql(age: 'twenty one', active: false, name: 'John', phone: phone.new('1', '234'))
end
it 'yields failure on #try when applying a member type fails' do
input = { age: 'twenty one', active: '0', name: 'John', phone: %w[1 234] }
# assert that a failed #try yields a failure result
expect { |rspec_probe| hash.try(input, &rspec_probe) }
.to yield_with_args(instance_of(Dry::Types::Result::Failure))
# assert that the failure result provides context for the failing input
hash.try(input) do |failure|
expect(failure.error.meta[:age].success?).to be(false)
end
end
it 'returns failure on #try when a member is missing' do
input = { active: '0', name: 'John', phone: %w[1 234] }
result = hash.try(input)
expect(result).to be_failure
end
describe '#valid?' do
it 'returns boolean' do
expect(hash.valid?(name: 'John', age: 23, active: 1, phone: 1)).to eql(true)
expect(hash.valid?(name: 'John', age: '23', active: 1, phone: 1)).to eql(false)
end
end
describe '#===' do
it 'returns boolean' do
expect(hash.===(name: 'John', age: 23, active: 1, phone: 1)).to eql(true)
expect(hash.===(name: 'John', age: '23', active: 1, phone: 1)).to eql(false)
end
end
it 'raises MissingKeyError if input is missing a key' do
expect {
hash.call(name: :Jane, active: true, phone: ['+48', '123-456-789'])
}.to raise_error(
Dry::Types::MissingKeyError, /:age is missing in Hash input/
)
end
it 'fails if any coercions are unsuccessful' do
expect {
hash.call(name: :Jane, age: 'oops', active: true, phone: [])
}.to raise_error(
Dry::Types::SchemaError,
'"oops" (String) has invalid type for :age violates '\
'constraints (type?(Integer, "oops") failed)'
)
end
it 'provides a key for type errors' do
expect {
hash.schema(age: 'coercible.integer').call(name: :Jane, age: nil, active: true, phone: [])
}.to raise_error(
Dry::Types::SchemaError,
"nil (NilClass) has invalid type for :age violates constraints"\
" (can't convert nil into Integer failed)"
)
end
it 'provides a key for argument errors' do
expect {
hash.schema(age: 'coercible.integer').call(name: :Jane, age: 'oops', active: true, phone: [])
}.to raise_error(
Dry::Types::SchemaError,
"\"oops\" (String) has invalid type for :age violates constraints"\
" (invalid value for Integer(): \"oops\" failed)"
)
end
it 'ignores unexpected keys' do
expect(subject.({**valid_input, not: :expect})).not_to have_key(:not)
end
it 'uses type even if it is a class with a registered name' do
bool = Class.new do
extend Dry::Types::Type
def self.meta
{}
end
end
stub_const('Bool', bool)
expect(hash.schema(bool: bool).key(:bool).type).to be(bool)
end
describe '#strict' do
subject { hash.strict }
it 'makes the schema strict' do
expected_input = { name: :Jane, age: 21, active: true, phone: %w[1 2] }
unexpected_input = { gender: 'F', email: 'Jane@hotmail.biz' }
expect {
subject.(expected_input.merge(unexpected_input))
}.to raise_error(Dry::Types::UnknownKeysError)
.with_message('unexpected keys [:gender, :email] in Hash input')
end
it 'can make a strict schema not strict again' do
expect(subject.strict(false)).not_to be_strict
end
end
describe '#strict?' do
example do
expect(subject).not_to be_strict
expect(subject.strict).to be_strict
end
end
describe '#with_key_transform' do
it 'adds a key transformation' do
schema = subject.with_key_transform { |k| k.downcase.to_sym }
expect(schema.('NAME' => 'John', 'AGE' => 23, 'ACTIVE' => true, 'PHONE' => [1, 23]))
.to eql(valid_input)
end
it 'accepts a proc' do
schema = subject.with_key_transform(-> k { k.downcase.to_sym })
expect(schema.('NAME' => 'John', 'AGE' => 23, 'ACTIVE' => true, 'PHONE' => [1, 23]))
.to eql(valid_input)
end
it 'raises an error on missing fn' do
expect { subject.with_key_transform }.to raise_error(ArgumentError)
end
end
describe 'optional keys' do
let(:hash_schema) do
{
name: 'coercible.string',
age: 'strict.integer',
active: 'params.bool',
phone: PhoneType.meta(required: false)
}
end
it 'allows to skip certain keys' do
expect(subject.(name: :Jane, age: 21, active: '1'))
.to eql(name: 'Jane', age: 21, active: true)
end
end
describe '#schema' do
it 'extends existing schema' do
extended = subject.schema(city: 'coercible.string')
expect(extended.({ **valid_input, city: :London })).to include(city: 'London')
expect(extended.key(:city).type).to eql(Dry::Types['coercible.string'])
end
example 'in presence of type transformation' do
extended = subject.with_type_transform { |k| k.meta(transformed: true) }.schema(city: 'coercible.string')
expect(extended.({ **valid_input, city: :London })).to include(city: 'London')
expect(extended.key(:city)).to eql(Dry::Types::Schema::Key.new(Dry::Types['coercible.string'], :city))
end
end
describe 'keys ending with question mark' do
let(:hash_schema) do
{ name: 'coercible.string', age?: 'strict.integer' }
end
example 'not required' do
expect(subject.key(:age)).not_to be_required
end
end
end
describe '#to_s' do
context 'empty schema' do
subject(:type) { Dry::Types['nominal.hash'].schema({}) }
it 'returns string representation of the type' do
expect(type.to_s).to eql('#<Dry::Types[Schema<keys={}>]>')
end
end
context 'strict schema' do
subject(:type) { Dry::Types['nominal.hash'].schema({}).strict }
it 'returns string representation of the type' do
expect(type.to_s).to eql('#<Dry::Types[Schema<strict keys={}>]>')
end
end
context 'key transformation' do
let(:key_transformation) { :to_sym.to_proc }
subject(:type) { Dry::Types['nominal.hash'].schema({}).with_key_transform(key_transformation) }
it 'returns string representation of the type' do
expect(type.to_s)
.to eql('#<Dry::Types[Schema<key_fn=.to_sym keys={}>]>')
end
end
context 'type transformation' do
let(:type_transformation) { :lax.to_proc }
subject(:type) { Dry::Types['nominal.hash'].with_type_transform(type_transformation).schema({}) }
it 'returns string representation of the type' do
expect(type.to_s)
.to eql('#<Dry::Types[Schema<type_fn=.lax keys={}>]>')
end
end
context 'schema with keys' do
subject(:type) do
Dry::Types['nominal.hash'].schema(
name: 'nominal.string',
age?: 'nominal.integer'
)
end
it 'returns string representation of the type' do
expect(type.to_s)
.to eql('#<Dry::Types[Schema<keys={name: Nominal<String> age?: Nominal<Integer>}>]>')
end
end
context 'strict schema with keys' do
subject(:type) { Dry::Types['strict.hash'].schema(age: 'strict.integer') }
it 'returns string representation of the type' do
expect(type.to_s)
.to eql(
'#<Dry::Types[Constrained<Schema<'\
'keys={age: Constrained<Nominal<Integer> rule=[type?(Integer)]>'\
'}> rule=[type?(Hash)]>]>'
)
end
end
end
describe '#apply' do
context 'ignoring missing keys' do
subject(:type) do
Dry::Types['nominal.hash'].schema(
name: 'coercible.string',
age: 'coercible.integer',
city: Dry::Types['strict.string'].default('New York')
)
end
it 'can be partially applied' do
expect(type.apply({ age: '18' }, skip_missing: true)).to eql(age: 18, city: 'New York')
end
it 'can skip resolving default values' do
expect(type.apply({ age: '18' }, skip_missing: true, resolve_defaults: false))
.to eql(age: 18)
end
end
end
describe '#transform_keys?' do
let(:schema) { Dry::Types['hash'].schema(name: 'string') }
context 'without key transformation' do
subject(:type) { schema }
specify { expect(type.transform_keys?).to be(false) }
end
context 'with key transformation' do
subject(:type) { schema.with_key_transform(:to_sym.to_proc) }
specify { expect(type.transform_keys?).to be(true) }
end
end
end