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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f2da127cb8628fa7c0221d15f700d3c39f5d82b5d7a30ae124d5c1030737d209
4
- data.tar.gz: 7e2b59f422d2a4935780461b996215434141bc69acbb285eca77b2577478964e
3
+ metadata.gz: 2d0b040bc6ef9353a9c6d8e4f48c195cebb4e2547bd66c6c709e57605cf6c18d
4
+ data.tar.gz: e226a8483f9655aacb337de251363e23c24ef517d4005f577ca470a5e7bd8fd5
5
5
  SHA512:
6
- metadata.gz: 8db92bdc350444e67d507462febf68db7db4b7455ee968a3a60f9e5f72b69ef82daf07552ba30daa31d59b06adca0d07b40b10016c96fea56117068287d5ab71
7
- data.tar.gz: 3365ec2c482c5489763dee97c8363669647113da4ba58b64f6e2979073b2f0689a8bdc8d174fc70ae8151131cd3fa2d31d3aa6a9977648e9684c037736d99204
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 should
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 |users|
117
- users.action :create do |create|
118
- create.request do |params|
119
- params.required :user, type: :hash do |user|
120
- user.required :first_name, type: :string
121
- user.required :last_name, type: :string
122
- user.required :emails, type: :array, elements: :email
123
- user.required :birth_date,
124
- type: :datetime,
125
- validate: lambda { |value| value <= 18.years.ago.to_date }
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
- users.action :notify do |notify|
131
- notify.request do |params|
132
- params.required :user_id, type: :integer
133
- params.required :message, type: :string, length: { min: 10, max: 250 }
134
- params.optional :by, inclusion: %w(email text_msg push), default: :email
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
- some_action.request do |params|
175
- params.required :key_1
176
- params.required :key_2, allow_blank: true
177
- params.optional :key_3
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
- some_action.request do |params|
205
- params.required :key_1, type: :boolean
206
- params.required :key_2, type: :decimal
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
- some_action.request do |params|
219
+ request do
220
220
  # Allows any keys and values for the hash
221
- params.required :key_1, type: :hash
221
+ required :key_1, type: :hash
222
222
 
223
223
  # Only allows the keys nested_key_1 and nested_key_2
224
- params.required :key_2, type: :hash do |key_name|
225
- key_name.required :nested_key_1, type: :string
226
- key_name.required :nested_key_2, type: :integer
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
- some_action.request do |params|
243
+ request do
244
244
  # Allows any value for the elements of the array
245
- params.required :key_1, type: :array
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
- params.required :key_2, type: :array, elements: { type: :decimal, value: { max: 1_000 }
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
- params.required :key_3, type: :array, elements: :hash do |key_3|
253
- key_3.required :nested_key, type: :email
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
- some_action.request do |params|
301
- params.required :key_1, type: :date
302
- params.required :key_2, type: :date, format: '%Y-%m-%e'
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
- some_action.request do |params|
324
- params.required :key_1, type: :string, inclusion: %w(asc desc)
325
- params.required :key_2,
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
- some_action.request do |params|
342
- params.required :key_1, type: :string, length: 10
343
- params.required :key_2, type: :string, length: { min: 5, max: 12 }
344
- params.required :key_3, type: :array, elements: :email, length: { max: 3 }
345
- params.required :key_4, type: :string, length: { max: 25,
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
- some_action.request do |params|
357
- params.required :key_1, type: :integer, value: { min: 0 }
358
- params.required :key_2, type: :integer, value: { max: 1_000_000, message: 'Value too big!' }
359
- params.required :key_3, type: :decimal, value: { min: 0, max: 1 }
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 de format of the value with a regular expression.
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
- some_action.request do |params|
376
- params.required :key_1, type: :string, format: /^5[1-5]\d{14}$/
377
- params.required :key_2, type: :string, format: { regexp: /^1.*/,
378
- message: 'Value should start with a 1' }
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
- some_action.request do |params|
392
- params.required :key_1, type: :date, validate: { function: lambda { |value| value >= Date.today },
393
- message: 'The date can not be in the past' }
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
- some_action.request do |params|
411
- params.required :key_1, type: :decimal, precision: 2
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
- some_action.request do |params|
423
- params.optional :key_1, type: :string, default: 'Jane'
424
- params.optional :key_2, type: :string, default: lambda { Date.today.strftime('%A') }
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
- some_action.request do |params|
439
- params.optional :key_1, type: :string, transform: :strip
440
- params.optional :key_2,
441
- type: :string,
442
- format: /^\d{3}-\d{3}-\d{3}$/,
443
- transform: lambda { |value| value.gsub(/-/, '') }
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
- some_action.request do |params|
452
- params.required :email_address, type: :email, as: :email
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
- some_action.request do |params|
465
- params.optional :label, type: :string
466
- params.required :description, type: :string, if_given: :label
463
+ request do
464
+ optional :label, type: :string
465
+ required :description, type: :string, if_given: :label
467
466
  #...
468
- params.required :card_type, inclusion: %w(credit_card debit_card)
469
- params.required :ccv, if_given: { card_type: lambda { |value| value == 'credit_card' } }
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 { |file| load file }
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
- block.call(resource)
19
+ resource.instance_eval(&block)
20
20
 
21
21
  @@definitions[resource_name] = resource
22
22
  end
@@ -9,11 +9,11 @@ module RequestParamsValidation
9
9
  @name = name
10
10
  end
11
11
 
12
- def request
12
+ def request(&block)
13
13
  if block_given?
14
14
  @request = Request.new
15
15
 
16
- yield @request
16
+ @request.instance_eval(&block)
17
17
  else
18
18
  @request
19
19
  end
@@ -208,7 +208,7 @@ module RequestParamsValidation
208
208
 
209
209
  request = Request.new
210
210
 
211
- block.call(request) if block_given?
211
+ request.instance_eval(&block) if block_given?
212
212
 
213
213
  request.params
214
214
  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
- action_definition = Action.new(action_name.to_s)
19
+ action = Action.new(action_name.to_s)
20
20
 
21
- yield action_definition
21
+ action.instance_eval(&block)
22
22
 
23
- @actions[action_name.to_s] = action_definition
23
+ @actions[action_name.to_s] = action
24
24
  rescue DefinitionArgumentError => e
25
25
  e.resource = name
26
26
  raise
@@ -1,3 +1,3 @@
1
1
  module RequestParamsValidation
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
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.3.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-07-06 00:00:00.000000000 Z
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: []