parameters_schema 0.42
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/README.md +286 -0
- data/Rakefile +8 -0
- data/lib/parameters_schema/core_ext.rb +21 -0
- data/lib/parameters_schema/exceptions.rb +23 -0
- data/lib/parameters_schema/options.rb +80 -0
- data/lib/parameters_schema/schema.rb +296 -0
- data/lib/parameters_schema.rb +8 -0
- data/test/helpers.rb +56 -0
- data/test/test_options.rb +74 -0
- data/test/test_schema_allow.rb +114 -0
- data/test/test_schema_allow_empty.rb +30 -0
- data/test/test_schema_allow_nil.rb +34 -0
- data/test/test_schema_hash.rb +90 -0
- data/test/test_schema_required.rb +74 -0
- data/test/test_schema_simple.rb +58 -0
- data/test/test_schema_types.rb +452 -0
- data/test/test_schema_types_complex.rb +126 -0
- metadata +77 -0
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'parameters_schema'
|
3
|
+
require_relative 'helpers'
|
4
|
+
|
5
|
+
describe 'Empty schema' do
|
6
|
+
before do
|
7
|
+
ParametersSchema::Options.reset_defaults
|
8
|
+
@schema = ParametersSchema::Schema.new do end
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'accepts nil params' do
|
12
|
+
@schema
|
13
|
+
.validate!(nil)
|
14
|
+
.must_equal_hash({})
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'accepts empty params' do
|
18
|
+
@schema
|
19
|
+
.validate!({})
|
20
|
+
.must_equal_hash({})
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'wont accept non-empty params' do
|
24
|
+
Proc
|
25
|
+
.new{ @schema.validate!(potatoe: 'Eramosa') }
|
26
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
27
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::UNKNOWN)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'Simple schema' do
|
32
|
+
before do
|
33
|
+
@schema = ParametersSchema::Schema.new do
|
34
|
+
param :potatoe
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'accepts string or symbol keys' do
|
39
|
+
['potatoe', :potatoe].each do |key|
|
40
|
+
@schema
|
41
|
+
.validate!(key => 'Eramosa')
|
42
|
+
.must_equal_hash(key => 'Eramosa')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'validates a valid input' do
|
47
|
+
@schema
|
48
|
+
.validate!(potatoe: 'Eramosa')
|
49
|
+
.must_equal_hash(potatoe: 'Eramosa')
|
50
|
+
end
|
51
|
+
|
52
|
+
it 'validates an invalid input because of a missing key' do
|
53
|
+
exception = Proc
|
54
|
+
.new{ @schema.validate!({}) }
|
55
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
56
|
+
.errors.must_equal_hash(potatoe: :missing)
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,452 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'parameters_schema'
|
3
|
+
require_relative 'helpers'
|
4
|
+
|
5
|
+
describe 'Types' do
|
6
|
+
describe String do
|
7
|
+
before do
|
8
|
+
ParametersSchema::Options.reset_defaults
|
9
|
+
|
10
|
+
@schema = ParametersSchema::Schema.new do
|
11
|
+
param :potatoe, type: String
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'allow this type' do
|
16
|
+
@schema
|
17
|
+
.validate!(potatoe: 'Eramosa')
|
18
|
+
.must_equal_hash(potatoe: 'Eramosa')
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'allow a Symbol value' do
|
22
|
+
@schema
|
23
|
+
.validate!(potatoe: :eramosa)
|
24
|
+
.must_equal_hash(potatoe: 'eramosa')
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'wont allow other values (no implicit conversion with #to_s)' do
|
28
|
+
[true, 1, 1.0, Date.today, DateTime.now, [1], { nope: true }].each do |value|
|
29
|
+
Proc
|
30
|
+
.new{ @schema.validate!(potatoe: value) }
|
31
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
32
|
+
.errors.must_equal_hash(potatoe: :disallowed)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
it 'must have a value' do
|
37
|
+
Proc
|
38
|
+
.new{ @schema.validate!(potatoe: nil) }
|
39
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
40
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::NIL)
|
41
|
+
|
42
|
+
Proc
|
43
|
+
.new{ @schema.validate!(potatoe: '') }
|
44
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
45
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::EMPTY)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe Symbol do
|
50
|
+
before do
|
51
|
+
ParametersSchema::Options.reset_defaults
|
52
|
+
|
53
|
+
@schema = ParametersSchema::Schema.new do
|
54
|
+
param :potatoe, type: Symbol
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'allow this type' do
|
59
|
+
@schema
|
60
|
+
.validate!(potatoe: :eramosa)
|
61
|
+
.must_equal_hash(potatoe: :eramosa)
|
62
|
+
end
|
63
|
+
|
64
|
+
it 'allow a String value' do
|
65
|
+
@schema
|
66
|
+
.validate!(potatoe: 'eramosa')
|
67
|
+
.must_equal_hash(potatoe: :eramosa)
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'wont allow other values' do
|
71
|
+
[true, 1, 1.0, Date.today, DateTime.now, [1], { nope: true }].each do |value|
|
72
|
+
Proc
|
73
|
+
.new{ @schema.validate!(potatoe: value) }
|
74
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
75
|
+
.errors.must_equal_hash(potatoe: :disallowed)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'must have a value' do
|
80
|
+
Proc
|
81
|
+
.new{ @schema.validate!(potatoe: nil) }
|
82
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
83
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::NIL)
|
84
|
+
|
85
|
+
Proc
|
86
|
+
.new{ @schema.validate!(potatoe: :'') }
|
87
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
88
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::EMPTY)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe Fixnum do
|
93
|
+
before do
|
94
|
+
ParametersSchema::Options.reset_defaults
|
95
|
+
|
96
|
+
@schema = ParametersSchema::Schema.new do
|
97
|
+
param :potatoe, type: Fixnum
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
it 'allow this type' do
|
102
|
+
@schema
|
103
|
+
.validate!(potatoe: 1)
|
104
|
+
.must_equal_hash(potatoe: 1)
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'allow a String value' do
|
108
|
+
@schema
|
109
|
+
.validate!(potatoe: '2')
|
110
|
+
.must_equal_hash(potatoe: 2)
|
111
|
+
end
|
112
|
+
|
113
|
+
it 'allow a Float value' do
|
114
|
+
@schema
|
115
|
+
.validate!(potatoe: 2.1)
|
116
|
+
.must_equal_hash(potatoe: 2)
|
117
|
+
end
|
118
|
+
|
119
|
+
it 'allow a String value representing a Float value' do
|
120
|
+
@schema
|
121
|
+
.validate!(potatoe: '2.1')
|
122
|
+
.must_equal_hash(potatoe: 2)
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'wont allow other values' do
|
126
|
+
[true, '1a', '1.2.3.4', Date.today, DateTime.now, [1], { nope: true }].each do |value|
|
127
|
+
Proc
|
128
|
+
.new{ @schema.validate!(potatoe: value) }
|
129
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
130
|
+
.errors.must_equal_hash(potatoe: :disallowed)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
it 'must have a value' do
|
135
|
+
Proc
|
136
|
+
.new{ @schema.validate!(potatoe: nil) }
|
137
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
138
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::NIL)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
describe Float do
|
143
|
+
before do
|
144
|
+
ParametersSchema::Options.reset_defaults
|
145
|
+
|
146
|
+
@schema = ParametersSchema::Schema.new do
|
147
|
+
param :potatoe, type: Float
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
it 'allow this type' do
|
152
|
+
@schema
|
153
|
+
.validate!(potatoe: 1.0)
|
154
|
+
.must_equal_hash(potatoe: 1.0)
|
155
|
+
end
|
156
|
+
|
157
|
+
it 'allow a String value' do
|
158
|
+
@schema
|
159
|
+
.validate!(potatoe: '1.2')
|
160
|
+
.must_equal_hash(potatoe: 1.2)
|
161
|
+
end
|
162
|
+
|
163
|
+
it 'allow a Fixnum value' do
|
164
|
+
@schema
|
165
|
+
.validate!(potatoe: 3)
|
166
|
+
.must_equal_hash(potatoe: 3.0)
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'allow a String value representing a Fixnum value' do
|
170
|
+
@schema
|
171
|
+
.validate!(potatoe: '3')
|
172
|
+
.must_equal_hash(potatoe: 3)
|
173
|
+
end
|
174
|
+
|
175
|
+
it 'wont allow other values' do
|
176
|
+
[true, '1a', '1.2.3.4', Date.today, DateTime.now, [1], { nope: true }].each do |value|
|
177
|
+
Proc
|
178
|
+
.new{ @schema.validate!(potatoe: value) }
|
179
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
180
|
+
.errors.must_equal_hash(potatoe: :disallowed)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
it 'must have a value' do
|
185
|
+
Proc
|
186
|
+
.new{ @schema.validate!(potatoe: nil) }
|
187
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
188
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::NIL)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
|
192
|
+
describe :boolean do
|
193
|
+
before do
|
194
|
+
ParametersSchema::Options.reset_defaults
|
195
|
+
|
196
|
+
@schema = ParametersSchema::Schema.new do
|
197
|
+
param :potatoe, type: :boolean
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
it 'allow this type' do
|
202
|
+
(ParametersSchema::Options.boolean_true_values + ParametersSchema::Options.boolean_false_values).each do |value|
|
203
|
+
@schema
|
204
|
+
.validate!(potatoe: value)
|
205
|
+
.must_equal_hash(potatoe: ParametersSchema::Options.boolean_true_values.include?(value))
|
206
|
+
end
|
207
|
+
end
|
208
|
+
|
209
|
+
it 'is not case-sensitive' do
|
210
|
+
%w(t T true True TRUE tRuE).each do |value|
|
211
|
+
@schema
|
212
|
+
.validate!(potatoe: value)
|
213
|
+
.must_equal_hash(potatoe: true)
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
it 'wont allow other values' do
|
218
|
+
[2, 1.2, 'whatever', Date.today, DateTime.now, [1], { nope: true }].each do |value|
|
219
|
+
Proc
|
220
|
+
.new{ @schema.validate!(potatoe: value) }
|
221
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
222
|
+
.errors.must_equal_hash(potatoe: :disallowed)
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
it 'must have a value' do
|
227
|
+
Proc
|
228
|
+
.new{ @schema.validate!(potatoe: nil) }
|
229
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
230
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::NIL)
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
describe :any do
|
235
|
+
before do
|
236
|
+
ParametersSchema::Options.reset_defaults
|
237
|
+
|
238
|
+
@schema = ParametersSchema::Schema.new do
|
239
|
+
param :potatoe, type: :any
|
240
|
+
end
|
241
|
+
end
|
242
|
+
|
243
|
+
it 'allow this type' do
|
244
|
+
[2, 1.2, 'whatever', Date.today, DateTime.now, [1], { nope: true }].each do |value|
|
245
|
+
@schema
|
246
|
+
.validate!(potatoe: value)
|
247
|
+
.must_equal_hash(potatoe: value)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
it 'must have a value' do
|
252
|
+
Proc
|
253
|
+
.new{ @schema.validate!(potatoe: nil) }
|
254
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
255
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::NIL)
|
256
|
+
end
|
257
|
+
end
|
258
|
+
|
259
|
+
describe Date do
|
260
|
+
before do
|
261
|
+
ParametersSchema::Options.reset_defaults
|
262
|
+
|
263
|
+
@schema = ParametersSchema::Schema.new do
|
264
|
+
param :potatoe, type: Date
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
it 'allow this type' do
|
269
|
+
date = Date.today
|
270
|
+
|
271
|
+
@schema
|
272
|
+
.validate!(potatoe: date)
|
273
|
+
.must_equal_hash(potatoe: date)
|
274
|
+
end
|
275
|
+
|
276
|
+
it 'accepts a String using the default format' do
|
277
|
+
date = Date.today
|
278
|
+
|
279
|
+
@schema
|
280
|
+
.validate!(potatoe: date.to_s)
|
281
|
+
.must_equal_hash(potatoe: date)
|
282
|
+
end
|
283
|
+
|
284
|
+
it 'accepts a DateTime' do
|
285
|
+
datetime = DateTime.now
|
286
|
+
|
287
|
+
@schema
|
288
|
+
.validate!(potatoe: datetime)
|
289
|
+
.must_equal_hash(potatoe: datetime.to_date)
|
290
|
+
end
|
291
|
+
|
292
|
+
it 'accepts a String representing a DateTime' do
|
293
|
+
datetime = DateTime.now
|
294
|
+
|
295
|
+
@schema
|
296
|
+
.validate!(potatoe: datetime.to_s)
|
297
|
+
.must_equal_hash(potatoe: datetime.to_date)
|
298
|
+
end
|
299
|
+
|
300
|
+
it 'wont allow other values' do
|
301
|
+
[2, 1.2, 'whatever', true, [1], { nope: true }, :lol].each do |value|
|
302
|
+
Proc
|
303
|
+
.new{ @schema.validate!(potatoe: value) }
|
304
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
305
|
+
.errors.must_equal_hash(potatoe: :disallowed)
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
it 'must have a value' do
|
310
|
+
Proc
|
311
|
+
.new{ @schema.validate!(potatoe: nil) }
|
312
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
313
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::NIL)
|
314
|
+
end
|
315
|
+
end
|
316
|
+
|
317
|
+
describe DateTime do
|
318
|
+
before do
|
319
|
+
ParametersSchema::Options.reset_defaults
|
320
|
+
|
321
|
+
@schema = ParametersSchema::Schema.new do
|
322
|
+
param :potatoe, type: DateTime
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
it 'allow this type' do
|
327
|
+
time = DateTime.now
|
328
|
+
|
329
|
+
@schema
|
330
|
+
.validate!(potatoe: time)
|
331
|
+
.must_equal_hash(potatoe: time)
|
332
|
+
end
|
333
|
+
|
334
|
+
it 'accepts a String using the default format' do
|
335
|
+
time = DateTime.now
|
336
|
+
|
337
|
+
@schema
|
338
|
+
.validate!(potatoe: time.to_s)
|
339
|
+
.must_equal_hash(potatoe: DateTime.parse(time.to_s))
|
340
|
+
end
|
341
|
+
|
342
|
+
it 'accepts a Date' do
|
343
|
+
date = Date.today
|
344
|
+
|
345
|
+
@schema
|
346
|
+
.validate!(potatoe: date)
|
347
|
+
.must_equal_hash(potatoe: date.to_datetime)
|
348
|
+
end
|
349
|
+
|
350
|
+
it 'accepts a String representing a Date' do
|
351
|
+
date = Date.today
|
352
|
+
|
353
|
+
@schema
|
354
|
+
.validate!(potatoe: date.to_s)
|
355
|
+
.must_equal_hash(potatoe: date.to_datetime)
|
356
|
+
end
|
357
|
+
|
358
|
+
it 'wont allow other values' do
|
359
|
+
[2, 1.2, 'whatever', true, [1], { nope: true }, :lol].each do |value|
|
360
|
+
Proc
|
361
|
+
.new{ @schema.validate!(potatoe: value) }
|
362
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
363
|
+
.errors.must_equal_hash(potatoe: :disallowed)
|
364
|
+
end
|
365
|
+
end
|
366
|
+
|
367
|
+
it 'must have a value' do
|
368
|
+
Proc
|
369
|
+
.new{ @schema.validate!(potatoe: nil) }
|
370
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
371
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::NIL)
|
372
|
+
end
|
373
|
+
end
|
374
|
+
|
375
|
+
describe Hash do
|
376
|
+
before do
|
377
|
+
ParametersSchema::Options.reset_defaults
|
378
|
+
|
379
|
+
@schema = ParametersSchema::Schema.new do
|
380
|
+
param :potatoe, type: Hash
|
381
|
+
end
|
382
|
+
end
|
383
|
+
|
384
|
+
it 'allow this type' do
|
385
|
+
[{ id: 1 }, { id: '1' }, { id: :'1' }, { description: { name: 'Eramosa' } }].each do |value|
|
386
|
+
@schema
|
387
|
+
.validate!(potatoe: value)
|
388
|
+
.must_equal_hash(potatoe: value)
|
389
|
+
end
|
390
|
+
end
|
391
|
+
|
392
|
+
it 'wont allow other values' do
|
393
|
+
[2, 1.2, 'whatever', true, [1], :lol].each do |value|
|
394
|
+
Proc
|
395
|
+
.new{ @schema.validate!(potatoe: value) }
|
396
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
397
|
+
.errors.must_equal_hash(potatoe: :disallowed)
|
398
|
+
end
|
399
|
+
end
|
400
|
+
|
401
|
+
it 'must have a value' do
|
402
|
+
Proc
|
403
|
+
.new{ @schema.validate!(potatoe: nil) }
|
404
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
405
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::NIL)
|
406
|
+
|
407
|
+
Proc
|
408
|
+
.new{ @schema.validate!(potatoe: '') }
|
409
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
410
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::EMPTY)
|
411
|
+
end
|
412
|
+
end
|
413
|
+
|
414
|
+
describe Array do
|
415
|
+
before do
|
416
|
+
ParametersSchema::Options.reset_defaults
|
417
|
+
|
418
|
+
@schema = ParametersSchema::Schema.new do
|
419
|
+
param :potatoe, type: Array
|
420
|
+
end
|
421
|
+
end
|
422
|
+
|
423
|
+
it 'allow this type' do
|
424
|
+
[[1], [:a], ['a', 'b', 'c'], [nil], [1, true, 'a', :b]].each do |value|
|
425
|
+
@schema
|
426
|
+
.validate!(potatoe: value)
|
427
|
+
.must_equal_hash(potatoe: value)
|
428
|
+
end
|
429
|
+
end
|
430
|
+
|
431
|
+
it 'wont allow other values' do
|
432
|
+
[2, 1.2, 'whatever', true, { test: 1 }, :lol].each do |value|
|
433
|
+
Proc
|
434
|
+
.new{ @schema.validate!(potatoe: value) }
|
435
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
436
|
+
.errors.must_equal_hash(potatoe: :disallowed)
|
437
|
+
end
|
438
|
+
end
|
439
|
+
|
440
|
+
it 'must have a value' do
|
441
|
+
Proc
|
442
|
+
.new{ @schema.validate!(potatoe: nil) }
|
443
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
444
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::NIL)
|
445
|
+
|
446
|
+
Proc
|
447
|
+
.new{ @schema.validate!(potatoe: []) }
|
448
|
+
.must_raise(ParametersSchema::InvalidParameters)
|
449
|
+
.errors.must_equal_hash(potatoe: ParametersSchema::ErrorCode::EMPTY)
|
450
|
+
end
|
451
|
+
end
|
452
|
+
end
|