request_params_validation 0.1.0 → 0.3.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: 5ccaf41aab1f3773847d076043efb7c16dcac54d48e525a101791f35f4fc6000
4
- data.tar.gz: 55b76ff4f3a8f1d6c433283b9e0962b28bd6dff76298672a0e92f9639019b57e
3
+ metadata.gz: f2da127cb8628fa7c0221d15f700d3c39f5d82b5d7a30ae124d5c1030737d209
4
+ data.tar.gz: 7e2b59f422d2a4935780461b996215434141bc69acbb285eca77b2577478964e
5
5
  SHA512:
6
- metadata.gz: cc0c695d265475d49f9142c1da677dd67341d96332077ab72fda3eaa5857a373609350f8a73df6c6e466795a7d384fd8b542585d58b9f27050770168cd879e4a
7
- data.tar.gz: 1f014823715f92ba77b6668753b382bb978ff881a3a228376595174a77aa832a573b3ad5e84cc398842acaf7eb05bbde16b413c3e7b9630135d2937064f4164d
6
+ metadata.gz: 8db92bdc350444e67d507462febf68db7db4b7455ee968a3a60f9e5f72b69ef82daf07552ba30daa31d59b06adca0d07b40b10016c96fea56117068287d5ab71
7
+ data.tar.gz: 3365ec2c482c5489763dee97c8363669647113da4ba58b64f6e2979073b2f0689a8bdc8d174fc70ae8151131cd3fa2d31d3aa6a9977648e9684c037736d99204
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  # RequestParamsValidation
2
2
  _Request parameters validations, type coercion and filtering for Rails params_
3
3
 
4
+ [![Gem Version](https://badge.fury.io/rb/request_params_validation.svg)](https://badge.fury.io/rb/request_params_validation)
4
5
  [![CircleCI](https://circleci.com/gh/felipefava/request_params_validation.svg?style=shield&circle-token=a404cb4fd87e219299caeb36e1685ab75d335b84)](https://circleci.com/gh/felipefava/request_params_validation)
6
+ [![Codacy Quality Badge](https://api.codacy.com/project/badge/Grade/6ed6582228bd429a94dfabaa82071455)](https://www.codacy.com/manual/felipefava/request_params_validation?utm_source=github.com&utm_medium=referral&utm_content=felipefava/request_params_validation&utm_campaign=Badge_Grade)
5
7
 
6
8
  ## Introduction
7
9
  Validates the request params outside your controller logic in order to get a clean nice code, and
@@ -93,15 +95,15 @@ and `notify`:
93
95
 
94
96
  class UsersController < ApplicationController
95
97
  def create
96
- ...
98
+ params # will have only the defined parameters
97
99
  end
98
100
 
99
101
  def notify
100
- ...
102
+ params # will have only the defined parameters
101
103
  end
102
104
 
103
105
  def another_action
104
- ...
106
+ params # will have whatever the user sends
105
107
  end
106
108
  end
107
109
  ```
@@ -113,7 +115,7 @@ Then, we will need to create the definition for the `users` resource:
113
115
 
114
116
  RequestParamsValidation.define do |users|
115
117
  users.action :create do |create|
116
- users.request do |params|
118
+ create.request do |params|
117
119
  params.required :user, type: :hash do |user|
118
120
  user.required :first_name, type: :string
119
121
  user.required :last_name, type: :string
@@ -179,15 +181,15 @@ end
179
181
  ### Types
180
182
  The `type` option specified the type of the parameter. The supported types are:
181
183
 
182
- 1. hash
183
- 2. array
184
- 3. string
185
- 4. integer
186
- 5. decimal
187
- 6. boolean
188
- 7. date
189
- 9. datetime
190
- 9. email
184
+ 1. hash
185
+ 2. array
186
+ 3. string
187
+ 4. integer
188
+ 5. decimal
189
+ 6. boolean
190
+ 7. date
191
+ 8. datetime
192
+ 9. email
191
193
 
192
194
  So if this option is present, the gem will validate that the value of the parameter matches with
193
195
  the specified type. And if it does, it will convert the value to the right type. This means that
@@ -282,10 +284,11 @@ will be converter to a Date object like `Wed, 04 Oct 1995`.
282
284
  However, they are cases when you only want to accept a specific format for a date, like
283
285
  `"%Y-%m-%e"`. In this cases you have two options.
284
286
 
285
- 1. Use the global configuration option `format.date`, so all date types must have the specified
286
- format through all the requests. See [here](#configuration) all globals configuration
287
- options.
288
- 2. Specify the option `format: "%Y-%m-%e"` locally.
287
+ 1. Use the global configuration option `format.date`, so all date types must have the specified
288
+ format through all the requests. See [here](#configuration) all globals configuration
289
+ options.
290
+
291
+ 2. Specify the option `format: "%Y-%m-%e"` locally.
289
292
 
290
293
  You can perfectly use both approaches, but the second one will locally override the first one on
291
294
  that parameter validation.
@@ -398,8 +401,8 @@ the specified type.
398
401
 
399
402
  If you want to set a precision value to all `decimal` parameters, you can use the global
400
403
  configuration option `format.decimal_precision`. Keep in mind that if you set the `precision`
401
- option on a parameter, it will locally override the global configuration. See here for all
402
- globals configuration options.
404
+ option on a parameter, it will locally override the global configuration. See [here](#configuration)
405
+ for all globals configuration options.
403
406
 
404
407
  This option accepts an integer as value.
405
408
 
@@ -441,8 +444,45 @@ some_action.request do |params|
441
444
  end
442
445
  ```
443
446
 
447
+ ### Rename Parameters
448
+ You can rename parameters using the `as` option.
449
+
450
+ ```ruby
451
+ some_action.request do |params|
452
+ params.required :email_address, type: :email, as: :email
453
+ end
454
+ ```
455
+
456
+ This means that in the request params you expect a valid email value in the key `email_address`,
457
+ but in your controller you will access with the key `email`.
458
+
459
+ ### Dependent Parameters
460
+ If you want to receive and validate a parameter only if another one is given, you can use
461
+ the `is_given` option.
462
+
463
+ ```ruby
464
+ some_action.request do |params|
465
+ params.optional :label, type: :string
466
+ params.required :description, type: :string, if_given: :label
467
+ #...
468
+ params.required :card_type, inclusion: %w(credit_card debit_card)
469
+ params.required :ccv, if_given: { card_type: lambda { |value| value == 'credit_card' } }
470
+ end
471
+ ```
472
+
473
+ On the example above, the param `description` will be only validated if the param `label` is present.
474
+ RequestParamsValidation will use the method `blank?` to check that. On the other hand, the param
475
+ `ccv` will only be validated if the param `type_card` is equal to the string `credit_card`.
476
+
477
+ Notice that if the global option `filter_params` is set to `true` (default behaviour), then the
478
+ dependent parameters will be filtered from the `params object` if they haven't beeen validated.
479
+ This way we make sure to only receive those parameters that have been validated against our request
480
+ definitions.
481
+
482
+ Be aware that if you rename a param, then you should use the new name in the `if_given` option.
483
+
444
484
  ---
445
- **NOTE**
485
+ ### NOTE
446
486
 
447
487
  RequestParamsValidation will start validating the presence of the parameters. Then, if the value is
448
488
  not present and the parameter has a default value, it will assign that value and not execute any
@@ -450,8 +490,8 @@ further validation. Otherwise, it will validate the type, convert it to the righ
450
490
  continue with the others validations. So, all others validations will be executed with the parameter
451
491
  value already converter to the specified type, so keep in mind that at defining the validations.
452
492
 
453
-
454
493
  ## Errors & Messages
494
+
455
495
  For default, when a required parameter failed the presence validation, the exception
456
496
  `RequestParamsValidation::MissingParameterError` will be raised. If it failed for any of the others
457
497
  validations, the raised exception will be `RequestParamsValidation::InvalidParameterValueError`
@@ -502,17 +542,15 @@ an element of an array or not. If you **have specified the `message` option in t
502
542
  definition**, then the details will be that value, otherwise it will took a default value from
503
543
  the table below:
504
544
 
505
-
506
- | Failure | Default Message |
507
- | ------------------------- | ---------------------------------------------------- |
508
- | Missing parameter | N/A |
509
- | Invalid type | - `Value should be a valid %{param_type}` <br> - `All elements of the array should be a valid %{type}` <br> If has `date` or `datetime` type with specified `format`: <br> &nbsp;&nbsp;&nbsp; - ` with the format %{format}` is added to the message |
510
- | Invalid inclusion | - `Value should be in %{include_in}` <br> - `All elements values of the array should have be in %{include_in}` |
511
- | Invalid length | - `Length should be greater or equal than %{min}` <br> - `Length should be less or equal than %{max}` <br> - `Length should be equal to %{min/max}` </br> - `Length should be between %{min} and %{max}` <br> - `All elements of the array should have a length ...` |
512
- | Invalid value size | - `Value should be greater or equal than %{min}` <br> - `Value should be less or equal than %{max}` <br> - `Value should be between %{min} and %{max}` <br> - `All elements of the array should have a value ...` |
513
- | Invalid format | - `Value format is invalid` <br> - `An element of the array has an invalid format` |
514
- | Invalid custom validation | N/A |
515
-
545
+ | Failure | Default Message |
546
+ | ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
547
+ | Missing parameter | N/A |
548
+ | Invalid type | - `Value should be a valid %{param_type}` <br> - `All elements of the array should be a valid %{type}` <br> If has `date` or `datetime` type with specified `format`: <br> &nbsp;&nbsp;&nbsp; - ` with the format %{format}` is added to the message |
549
+ | Invalid inclusion | - `Value should be in %{include_in}` <br> - `All elements values of the array should be in %{include_in}` |
550
+ | Invalid length | - `Length should be greater or equal than %{min}` <br> - `Length should be less or equal than %{max}` <br> - `Length should be equal to %{min/max}` </br> - `Length should be between %{min} and %{max}` <br> - `All elements of the array should have a length ...` |
551
+ | Invalid value size | - `Value should be greater or equal than %{min}` <br> - `Value should be less or equal than %{max}` <br> - `Value should be between %{min} and %{max}` <br> - `All elements of the array should have a value ...` |
552
+ | Invalid format | - `Value format is invalid` <br> - `An element of the array has an invalid format` |
553
+ | Invalid custom validation | N/A | |
516
554
 
517
555
  ### Custom Exceptions
518
556
  However, if the above is not enough for your app, and you need to fully customize the exceptions
@@ -537,21 +575,21 @@ please see [here](./examples/initializer.rb).
537
575
  ## Future Work
538
576
  In the near future the plan is to continue adding features to the gem. Next incoming changes
539
577
  could be:
540
- - Add doc generation from the definitions
541
- - Add representations for DRY definitions
542
- - Add more options to the actions definitions
543
- - Add handler for responses
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
544
582
 
545
583
  ## Acknowledgments
546
584
  This gem is strongly inspired in a Ruby framework named [Angus](https://github.com/moove-it/angus)
547
585
  developed by [Moove It](https://moove-it.com/)
548
586
 
549
587
  ## Contributing
550
- 1. Fork it
551
- 2. Create your feature branch (git checkout -b my-new-feature)
552
- 3. Commit your changes (git commit -am 'Add some feature')
553
- 4. Push to the branch (git push origin my-new-feature)
554
- 5. Create a Pull Request
588
+ 1. Fork it
589
+ 2. Create your feature branch (git checkout -b my-new-feature)
590
+ 3. Commit your changes (git commit -am 'Add some feature')
591
+ 4. Push to the branch (git push origin my-new-feature)
592
+ 5. Create a Pull Request
555
593
 
556
594
  ## License
557
595
  This software is released under the MIT license. See the MIT-LICENSE file for more info.
@@ -1,17 +1,17 @@
1
1
  require 'request_params_validation/params'
2
- require 'request_params_validation/exceptions/definitions_errors'
3
2
 
4
3
  module RequestParamsValidation
5
4
  module Definitions
6
5
  class Param
7
- attr_reader :key, :required, :allow_blank, :type, :transform, :decimal_precision,
8
- :inclusion, :length, :value, :format, :custom_validation, :elements
6
+ attr_reader :key, :required, :allow_blank, :type, :rename_as, :transform, :decimal_precision,
7
+ :inclusion, :length, :value, :format, :custom_validation, :if_given, :elements
9
8
 
10
9
  def initialize(options, &block)
11
10
  @key = options[:key]
12
11
  @required = options[:required]
13
12
  @allow_blank = options[:allow_blank]
14
13
  @type = options[:type].try(:to_sym)
14
+ @rename_as = options[:as].try(:to_sym)
15
15
  @default = options[:default]
16
16
 
17
17
  @transform = options[:transform]
@@ -23,6 +23,7 @@ module RequestParamsValidation
23
23
  @value = build_value_option(options[:value])
24
24
  @format = build_format_option(options[:format])
25
25
  @custom_validation = build_custom_validation_option(options[:validate])
26
+ @if_given = build_if_given_option(options[:if_given])
26
27
 
27
28
  @elements = build_elements_option(options[:elements], &block)
28
29
  @sub_definition = build_sub_definition(&block)
@@ -74,6 +75,26 @@ module RequestParamsValidation
74
75
  !!@custom_validation
75
76
  end
76
77
 
78
+ def rename?
79
+ !!@rename_as
80
+ end
81
+
82
+ def transform?
83
+ !!@transform
84
+ end
85
+
86
+ def skip?(request_params)
87
+ return false unless @if_given
88
+
89
+ if_given_param_value = request_params[@if_given.param]
90
+
91
+ if @if_given.function
92
+ !@if_given.function.call(if_given_param_value)
93
+ else
94
+ if_given_param_value.blank?
95
+ end
96
+ end
97
+
77
98
  private
78
99
 
79
100
  def build_inclusion_option(inclusion)
@@ -150,6 +171,20 @@ module RequestParamsValidation
150
171
  Struct.new(:function, :message).new(function, message)
151
172
  end
152
173
 
174
+ def build_if_given_option(if_given)
175
+ case if_given
176
+ when String, Symbol
177
+ param = if_given.to_sym
178
+ when Hash
179
+ param = if_given.first.try(:first)
180
+ function = if_given.first.try(:last)
181
+ end
182
+
183
+ return unless param
184
+
185
+ Struct.new(:param, :function).new(param, function)
186
+ end
187
+
153
188
  def build_elements_option(elements, &block)
154
189
  return unless @type == Params::ARRAY_TYPE
155
190
 
@@ -6,12 +6,12 @@ module RequestParamsValidation
6
6
  isolate_namespace RequestParamsValidation
7
7
 
8
8
  initializer 'request_params_validation.load_definitions' do
9
- RequestParamsValidation::Definitions.load_all
9
+ Definitions.load_all
10
10
  end
11
11
 
12
12
  initializer 'request_params_validation.add_helpers' do
13
13
  ActiveSupport.on_load :action_controller do
14
- include RequestParamsValidation::Helpers
14
+ include Helpers
15
15
  end
16
16
  end
17
17
  end
@@ -7,6 +7,8 @@ module RequestParamsValidation
7
7
 
8
8
  def self.validate!(definition, params)
9
9
  definition.each do |param_definition|
10
+ next if param_definition.skip?(params)
11
+
10
12
  validate_and_coerce_param(param_definition, params)
11
13
  end
12
14
 
@@ -16,8 +18,8 @@ module RequestParamsValidation
16
18
  def self.filter!(definition, params)
17
19
  extra_keys = [:controller, :action] # Keys added by Rails
18
20
 
19
- filter_params(definition, params, extra_keys).tap do |params|
20
- params.permit! if params.respond_to?(:permit!)
21
+ filter_params(definition, params, extra_keys).tap do |filtered_params|
22
+ filtered_params.permit! if filtered_params.respond_to?(:permit!)
21
23
  end
22
24
  end
23
25
 
@@ -26,8 +28,14 @@ module RequestParamsValidation
26
28
  value = params[key]
27
29
 
28
30
  value = Validator.new(param_definition, value).validate_and_coerce
31
+ value = Converter.apply_transformation(param_definition, value) if param_definition.transform?
29
32
 
30
- params[key] = value
33
+ if param_definition.rename?
34
+ params.delete(key)
35
+ params[param_definition.rename_as] = value
36
+ else
37
+ params[key] = value
38
+ end
31
39
  end
32
40
  private_class_method :validate_and_coerce_param
33
41
 
@@ -36,7 +44,9 @@ module RequestParamsValidation
36
44
  return params if definition.empty?
37
45
 
38
46
  params_keys = definition.map do |param_definition|
39
- key = param_definition.key
47
+ next if param_definition.skip?(params)
48
+
49
+ key = param_definition.rename? ? param_definition.rename_as : param_definition.key
40
50
 
41
51
  if param_definition.sub_definition
42
52
  filter_params(param_definition.sub_definition, params[key])
@@ -24,8 +24,6 @@ module RequestParamsValidation
24
24
  def self.apply_transformation(param, value)
25
25
  transform = param.transform
26
26
 
27
- return value unless transform
28
-
29
27
  transform.respond_to?(:call) ? transform.call(value) : value.send(transform)
30
28
  end
31
29
  end
@@ -12,7 +12,7 @@ module RequestParamsValidation
12
12
  false
13
13
  end
14
14
 
15
- def valid_string?(value)
15
+ def valid_string?(_value)
16
16
  true
17
17
  end
18
18
 
@@ -45,7 +45,7 @@ module RequestParamsValidation
45
45
  validate_format! if param.validate_format?
46
46
  validate_custom_validation! if param.validate_custom_validation?
47
47
 
48
- @value = Params::Converter.apply_transformation(param, value)
48
+ @value
49
49
  end
50
50
 
51
51
  private
@@ -57,7 +57,7 @@ module RequestParamsValidation
57
57
  end
58
58
 
59
59
  def iterate_hash
60
- Params::validate!(param.sub_definition, value) # recursion for the sub_definition
60
+ Params.validate!(param.sub_definition, value) # recursion for the sub_definition
61
61
  end
62
62
 
63
63
  def raise_error(exception_type, options = {})
@@ -5,7 +5,7 @@ module RequestParamsValidation
5
5
  def validate_presence!
6
6
  not_present = param.allow_blank ? value.nil? : value.blank?
7
7
 
8
- raise_error(:on_missing_parameter) if not_present
8
+ raise_error(:on_missing_parameter) if not_present && value != false
9
9
  end
10
10
  end
11
11
  end
@@ -1,3 +1,3 @@
1
1
  module RequestParamsValidation
2
- VERSION = '0.1.0'
2
+ VERSION = '0.3.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.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Felipe Fava
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-29 00:00:00.000000000 Z
11
+ date: 2020-07-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -60,7 +60,7 @@ files:
60
60
  - lib/request_params_validation/params/validators/type.rb
61
61
  - lib/request_params_validation/params/validators/value.rb
62
62
  - lib/request_params_validation/version.rb
63
- homepage: https://github.com/felipefava/request_validation
63
+ homepage: https://github.com/felipefava/request_params_validation
64
64
  licenses:
65
65
  - MIT
66
66
  metadata: {}
@@ -72,16 +72,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: 1.9.3
76
76
  required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  requirements:
78
78
  - - ">="
79
79
  - !ruby/object:Gem::Version
80
80
  version: '0'
81
81
  requirements: []
82
- rubyforge_project:
83
- rubygems_version: 2.7.9
82
+ rubygems_version: 3.0.4
84
83
  signing_key:
85
84
  specification_version: 4
86
- summary: Validates request params
85
+ summary: Validates rails request params
87
86
  test_files: []