request_params_validation 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +76 -85
- data/lib/request_params_validation/definitions.rb +3 -3
- data/lib/request_params_validation/definitions/action.rb +2 -2
- data/lib/request_params_validation/definitions/param.rb +1 -1
- data/lib/request_params_validation/definitions/resource.rb +4 -4
- data/lib/request_params_validation/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2d0b040bc6ef9353a9c6d8e4f48c195cebb4e2547bd66c6c709e57605cf6c18d
|
4
|
+
data.tar.gz: e226a8483f9655aacb337de251363e23c24ef517d4005f577ca470a5e7bd8fd5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d9e496129c0335c2ef2bb2ba9ee5c0715f4d49ca3890d428d8200e329d7e5dcbaf8fd45625fa3325132e95967c7e496eaff6f5234f7b5a867f8972f31083e520
|
7
|
+
data.tar.gz: ba5c464e0fe423f287a7c5808b401f884a7288520b328f8bfac2f54cd9b5d835f3f255ab6974252fb8d7136ccf5b267959f8f46d163aa69bc8697b0eae772d1f
|
data/README.md
CHANGED
@@ -45,7 +45,7 @@ documentation and allows to keep controllers code clean, ensuring that `params`
|
|
45
45
|
always have the parameters you suppose to receive.
|
46
46
|
|
47
47
|
The default path for the definitions files is `app/definitions`, and their names should be the same
|
48
|
-
as their respective controller's name, but ending with the suffix `_definition`. They also
|
48
|
+
as their respective controller's name, but ending with the suffix `_definition`. They should also
|
49
49
|
respect the folder structure of the controllers folder. Please see the following project structure
|
50
50
|
to clarify the idea:
|
51
51
|
|
@@ -113,25 +113,25 @@ Then, we will need to create the definition for the `users` resource:
|
|
113
113
|
```ruby
|
114
114
|
# app/definitions/users_definition.rb
|
115
115
|
|
116
|
-
RequestParamsValidation.define do
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
116
|
+
RequestParamsValidation.define do
|
117
|
+
action :create do
|
118
|
+
request do
|
119
|
+
required :user, type: :hash do
|
120
|
+
required :first_name, type: :string
|
121
|
+
required :last_name, type: :string
|
122
|
+
required :emails, type: :array, elements: :email
|
123
|
+
required :birth_date,
|
124
|
+
type: :datetime,
|
125
|
+
validate: lambda { |value| value <= 18.years.ago.to_date }
|
126
126
|
end
|
127
127
|
end
|
128
128
|
end
|
129
129
|
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
130
|
+
action :notify do
|
131
|
+
request do
|
132
|
+
required :user_id, type: :integer
|
133
|
+
required :message, type: :string, length: { min: 10, max: 250 }
|
134
|
+
optional :by, inclusion: %w(email text_msg push), default: :email
|
135
135
|
end
|
136
136
|
end
|
137
137
|
end
|
@@ -171,10 +171,10 @@ param. Otherwise use the `optional` method. For default, required parameters don
|
|
171
171
|
values, if you would like to allow them for that parameter, you can use the option `allow_blank`
|
172
172
|
|
173
173
|
```ruby
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
174
|
+
request do
|
175
|
+
required :key_1
|
176
|
+
required :key_2, allow_blank: true
|
177
|
+
optional :key_3
|
178
178
|
end
|
179
179
|
```
|
180
180
|
|
@@ -201,9 +201,9 @@ configuration option `extend.types`. See [here](#configuration) all globals
|
|
201
201
|
configuration options.
|
202
202
|
|
203
203
|
```ruby
|
204
|
-
|
205
|
-
|
206
|
-
|
204
|
+
request do
|
205
|
+
required :key_1, type: :boolean
|
206
|
+
required :key_2, type: :decimal
|
207
207
|
# ...
|
208
208
|
end
|
209
209
|
```
|
@@ -216,14 +216,14 @@ If no block is passed, the gem will only check that the value of the parameter b
|
|
216
216
|
object, without validating the content of it.
|
217
217
|
|
218
218
|
```ruby
|
219
|
-
|
219
|
+
request do
|
220
220
|
# Allows any keys and values for the hash
|
221
|
-
|
221
|
+
required :key_1, type: :hash
|
222
222
|
|
223
223
|
# Only allows the keys nested_key_1 and nested_key_2
|
224
|
-
|
225
|
-
|
226
|
-
|
224
|
+
required :key_2, type: :hash do
|
225
|
+
required :nested_key_1, type: :string
|
226
|
+
required :nested_key_2, type: :integer
|
227
227
|
end
|
228
228
|
end
|
229
229
|
```
|
@@ -237,20 +237,20 @@ The value for this option can be a type or a hash. `elements: :integer` is equiv
|
|
237
237
|
`elements: { type: :integer }`.
|
238
238
|
|
239
239
|
The second way is useful when you want to validate other things of the elements than just the
|
240
|
-
type. The option elements accepts all validations options.
|
240
|
+
type. The option `elements` accepts all validations options.
|
241
241
|
|
242
242
|
```ruby
|
243
|
-
|
243
|
+
request do
|
244
244
|
# Allows any value for the elements of the array
|
245
|
-
|
245
|
+
required :key_1, type: :array
|
246
246
|
|
247
247
|
# Only allows decimals with a value less than 1_000 for the elements of the array
|
248
|
-
|
248
|
+
required :key_2, type: :array, elements: { type: :decimal, value: { max: 1_000 } }
|
249
249
|
|
250
250
|
# Only allows objects with a required key 'nested_key' of type 'email' for the
|
251
251
|
# elements of the array
|
252
|
-
|
253
|
-
|
252
|
+
required :key_3, type: :array, elements: :hash do
|
253
|
+
required :nested_key, type: :email
|
254
254
|
end
|
255
255
|
end
|
256
256
|
```
|
@@ -262,7 +262,7 @@ Any value is a valid string.
|
|
262
262
|
Accepts only valid integers like `5` or `"5"`.
|
263
263
|
|
264
264
|
#### Decimal type
|
265
|
-
Accepts only valid decimals like `5` or `1.5` or `10.45`. With decimals parameters you can use
|
265
|
+
Accepts only valid decimals like `5` or `"1.5"` or `10.45`. With decimals parameters you can use
|
266
266
|
the option `precision`. Go [here](#precision) for more details about this option.
|
267
267
|
|
268
268
|
#### Boolean type
|
@@ -297,9 +297,9 @@ Notice that if no format is specified, the date will be validated using the ruby
|
|
297
297
|
method.
|
298
298
|
|
299
299
|
```ruby
|
300
|
-
|
301
|
-
|
302
|
-
|
300
|
+
request do
|
301
|
+
required :key_1, type: :date
|
302
|
+
required :key_2, type: :date, format: '%Y-%m-%e'
|
303
303
|
end
|
304
304
|
```
|
305
305
|
|
@@ -320,9 +320,9 @@ Besides from the `in` option, you can also use the `message` option for passing
|
|
320
320
|
detail when the parameter is not valid.
|
321
321
|
|
322
322
|
```ruby
|
323
|
-
|
324
|
-
|
325
|
-
|
323
|
+
request do
|
324
|
+
required :key_1, type: :string, inclusion: %w(asc desc)
|
325
|
+
required :key_2,
|
326
326
|
type: :string,
|
327
327
|
inclusion: { in: %w(s m l), message: 'Value is not a valid size' }
|
328
328
|
end
|
@@ -338,12 +338,11 @@ Besides from the `min` and `max` options, you can also use the `message` option
|
|
338
338
|
custom error detail when the parameter is not valid.
|
339
339
|
|
340
340
|
```ruby
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
message: '25 characters is the maximum allowed' }
|
341
|
+
request do
|
342
|
+
required :key_1, type: :string, length: 10
|
343
|
+
required :key_2, type: :string, length: { min: 5, max: 12 }
|
344
|
+
required :key_3, type: :array, elements: :email, length: { max: 3 }
|
345
|
+
required :key_4, type: :string, length: { max: 25, message: '25 characters is the maximum allowed' }
|
347
346
|
end
|
348
347
|
```
|
349
348
|
|
@@ -353,15 +352,15 @@ The `value` option is for validating the value size of numerics parameters.
|
|
353
352
|
The value for this option is a hash with the following options: `min`, `max` and `message`.
|
354
353
|
|
355
354
|
```ruby
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
|
355
|
+
request do
|
356
|
+
required :key_1, type: :integer, value: { min: 0 }
|
357
|
+
required :key_2, type: :integer, value: { max: 1_000_000, message: 'Value too big!' }
|
358
|
+
required :key_3, type: :decimal, value: { min: 0, max: 1 }
|
360
359
|
end
|
361
360
|
```
|
362
361
|
|
363
362
|
### Format
|
364
|
-
The `format` option allows to validate
|
363
|
+
The `format` option allows to validate the format of the value with a regular expression.
|
365
364
|
|
366
365
|
The value for this option is a `regexp`, `string` or a `hash`. The string value is only valid
|
367
366
|
when the type is a `date` or a `datetime`. Otherwise, you should use a regexp. The options for
|
@@ -372,10 +371,10 @@ So, for `date` and `datetime` types, `format: '%u%F'` is equivalent to
|
|
372
371
|
equivalent to `format: { regexp: /^5[1-5]\d{14}$/ }`.
|
373
372
|
|
374
373
|
```ruby
|
375
|
-
|
376
|
-
|
377
|
-
|
378
|
-
|
374
|
+
request do
|
375
|
+
required :key_1, type: :string, format: /^5[1-5]\d{14}$/
|
376
|
+
required :key_2, type: :string, format: { regexp: /^1.*/,
|
377
|
+
message: 'Value should start with a 1' }
|
379
378
|
end
|
380
379
|
```
|
381
380
|
|
@@ -388,9 +387,9 @@ This option accepts a Proc as value or a hash. For example,
|
|
388
387
|
also accepts the `message` option.
|
389
388
|
|
390
389
|
```ruby
|
391
|
-
|
392
|
-
|
393
|
-
|
390
|
+
request do
|
391
|
+
required :key_1, type: :date, validate: { function: lambda { |value| value >= Date.today },
|
392
|
+
message: 'The date can not be in the past' }
|
394
393
|
end
|
395
394
|
```
|
396
395
|
|
@@ -407,8 +406,8 @@ for all globals configuration options.
|
|
407
406
|
This option accepts an integer as value.
|
408
407
|
|
409
408
|
```ruby
|
410
|
-
|
411
|
-
|
409
|
+
request do
|
410
|
+
required :key_1, type: :decimal, precision: 2
|
412
411
|
end
|
413
412
|
```
|
414
413
|
|
@@ -419,9 +418,9 @@ present.
|
|
419
418
|
The value for the option `default` could be anything, including a proc.
|
420
419
|
|
421
420
|
```ruby
|
422
|
-
|
423
|
-
|
424
|
-
|
421
|
+
request do
|
422
|
+
optional :key_1, type: :string, default: 'Jane'
|
423
|
+
optional :key_2, type: :string, default: lambda { Date.today.strftime('%A') }
|
425
424
|
end
|
426
425
|
```
|
427
426
|
|
@@ -435,12 +434,12 @@ specified in the definition. So, `transform: :strip` is equivalent to
|
|
435
434
|
`transform: lambda { |value| value.strip }`.
|
436
435
|
|
437
436
|
```ruby
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
437
|
+
request do
|
438
|
+
optional :key_1, type: :string, transform: :strip
|
439
|
+
optional :key_2,
|
440
|
+
type: :string,
|
441
|
+
format: /^\d{3}-\d{3}-\d{3}$/,
|
442
|
+
transform: lambda { |value| value.gsub(/-/, '') }
|
444
443
|
end
|
445
444
|
```
|
446
445
|
|
@@ -448,8 +447,8 @@ end
|
|
448
447
|
You can rename parameters using the `as` option.
|
449
448
|
|
450
449
|
```ruby
|
451
|
-
|
452
|
-
|
450
|
+
request do
|
451
|
+
required :email_address, type: :email, as: :email
|
453
452
|
end
|
454
453
|
```
|
455
454
|
|
@@ -461,12 +460,12 @@ If you want to receive and validate a parameter only if another one is given, yo
|
|
461
460
|
the `is_given` option.
|
462
461
|
|
463
462
|
```ruby
|
464
|
-
|
465
|
-
|
466
|
-
|
463
|
+
request do
|
464
|
+
optional :label, type: :string
|
465
|
+
required :description, type: :string, if_given: :label
|
467
466
|
#...
|
468
|
-
|
469
|
-
|
467
|
+
required :card_type, inclusion: %w(credit_card debit_card)
|
468
|
+
required :ccv, if_given: { card_type: lambda { |value| value == 'credit_card' } }
|
470
469
|
end
|
471
470
|
```
|
472
471
|
|
@@ -572,14 +571,6 @@ end
|
|
572
571
|
To see a complete initializer file of the configuration with all the options and their description,
|
573
572
|
please see [here](./examples/initializer.rb).
|
574
573
|
|
575
|
-
## Future Work
|
576
|
-
In the near future the plan is to continue adding features to the gem. Next incoming changes
|
577
|
-
could be:
|
578
|
-
- Add doc generation from the definitions
|
579
|
-
- Add representations for DRY definitions
|
580
|
-
- Add more options to the actions definitions
|
581
|
-
- Add handler for responses
|
582
|
-
|
583
574
|
## Acknowledgments
|
584
575
|
This gem is strongly inspired in a Ruby framework named [Angus](https://github.com/moove-it/angus)
|
585
576
|
developed by [Moove It](https://moove-it.com/)
|
@@ -5,9 +5,9 @@ module RequestParamsValidation
|
|
5
5
|
module Definitions
|
6
6
|
@@definitions = {}
|
7
7
|
|
8
|
-
def self.load_all
|
8
|
+
def self.load_all(use_load = false)
|
9
9
|
definitions_suffix = RequestParamsValidation.definitions_suffix
|
10
|
-
Dir["#{definitions_path}/**/*#{definitions_suffix}.rb"].each { |
|
10
|
+
Dir["#{definitions_path}/**/*#{definitions_suffix}.rb"].each { |f| use_load ? load(f) : require(f) }
|
11
11
|
end
|
12
12
|
|
13
13
|
def self.register_resource(&block)
|
@@ -16,7 +16,7 @@ module RequestParamsValidation
|
|
16
16
|
resource_name = resource_name_from_block(&block)
|
17
17
|
resource = Resource.new(resource_name)
|
18
18
|
|
19
|
-
|
19
|
+
resource.instance_eval(&block)
|
20
20
|
|
21
21
|
@@definitions[resource_name] = resource
|
22
22
|
end
|
@@ -11,16 +11,16 @@ module RequestParamsValidation
|
|
11
11
|
@actions = {}
|
12
12
|
end
|
13
13
|
|
14
|
-
def action(action_name)
|
14
|
+
def action(action_name, &block)
|
15
15
|
unless block_given?
|
16
16
|
raise DefinitionArgumentError.new("Expecting block for action '#{action_name}'")
|
17
17
|
end
|
18
18
|
|
19
|
-
|
19
|
+
action = Action.new(action_name.to_s)
|
20
20
|
|
21
|
-
|
21
|
+
action.instance_eval(&block)
|
22
22
|
|
23
|
-
@actions[action_name.to_s] =
|
23
|
+
@actions[action_name.to_s] = action
|
24
24
|
rescue DefinitionArgumentError => e
|
25
25
|
e.resource = name
|
26
26
|
raise
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: request_params_validation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Felipe Fava
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-08-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -64,7 +64,7 @@ homepage: https://github.com/felipefava/request_params_validation
|
|
64
64
|
licenses:
|
65
65
|
- MIT
|
66
66
|
metadata: {}
|
67
|
-
post_install_message:
|
67
|
+
post_install_message:
|
68
68
|
rdoc_options: []
|
69
69
|
require_paths:
|
70
70
|
- lib
|
@@ -80,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
80
|
version: '0'
|
81
81
|
requirements: []
|
82
82
|
rubygems_version: 3.0.4
|
83
|
-
signing_key:
|
83
|
+
signing_key:
|
84
84
|
specification_version: 4
|
85
85
|
summary: Validates rails request params
|
86
86
|
test_files: []
|