request_params_validation 0.2.1 → 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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2da127cb8628fa7c0221d15f700d3c39f5d82b5d7a30ae124d5c1030737d209
|
4
|
+
data.tar.gz: 7e2b59f422d2a4935780461b996215434141bc69acbb285eca77b2577478964e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8db92bdc350444e67d507462febf68db7db4b7455ee968a3a60f9e5f72b69ef82daf07552ba30daa31d59b06adca0d07b40b10016c96fea56117068287d5ab71
|
7
|
+
data.tar.gz: 3365ec2c482c5489763dee97c8363669647113da4ba58b64f6e2979073b2f0689a8bdc8d174fc70ae8151131cd3fa2d31d3aa6a9977648e9684c037736d99204
|
data/README.md
CHANGED
@@ -115,7 +115,7 @@ Then, we will need to create the definition for the `users` resource:
|
|
115
115
|
|
116
116
|
RequestParamsValidation.define do |users|
|
117
117
|
users.action :create do |create|
|
118
|
-
|
118
|
+
create.request do |params|
|
119
119
|
params.required :user, type: :hash do |user|
|
120
120
|
user.required :first_name, type: :string
|
121
121
|
user.required :last_name, type: :string
|
@@ -456,6 +456,31 @@ end
|
|
456
456
|
This means that in the request params you expect a valid email value in the key `email_address`,
|
457
457
|
but in your controller you will access with the key `email`.
|
458
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
|
+
|
459
484
|
---
|
460
485
|
### NOTE
|
461
486
|
|
@@ -4,7 +4,7 @@ module RequestParamsValidation
|
|
4
4
|
module Definitions
|
5
5
|
class Param
|
6
6
|
attr_reader :key, :required, :allow_blank, :type, :rename_as, :transform, :decimal_precision,
|
7
|
-
:inclusion, :length, :value, :format, :custom_validation, :elements
|
7
|
+
:inclusion, :length, :value, :format, :custom_validation, :if_given, :elements
|
8
8
|
|
9
9
|
def initialize(options, &block)
|
10
10
|
@key = options[:key]
|
@@ -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)
|
@@ -82,6 +83,18 @@ module RequestParamsValidation
|
|
82
83
|
!!@transform
|
83
84
|
end
|
84
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
|
+
|
85
98
|
private
|
86
99
|
|
87
100
|
def build_inclusion_option(inclusion)
|
@@ -158,6 +171,20 @@ module RequestParamsValidation
|
|
158
171
|
Struct.new(:function, :message).new(function, message)
|
159
172
|
end
|
160
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
|
+
|
161
188
|
def build_elements_option(elements, &block)
|
162
189
|
return unless @type == Params::ARRAY_TYPE
|
163
190
|
|
@@ -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
|
|
@@ -42,6 +44,8 @@ module RequestParamsValidation
|
|
42
44
|
return params if definition.empty?
|
43
45
|
|
44
46
|
params_keys = definition.map do |param_definition|
|
47
|
+
next if param_definition.skip?(params)
|
48
|
+
|
45
49
|
key = param_definition.rename? ? param_definition.rename_as : param_definition.key
|
46
50
|
|
47
51
|
if param_definition.sub_definition
|
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.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-
|
11
|
+
date: 2020-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -79,8 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
79
|
- !ruby/object:Gem::Version
|
80
80
|
version: '0'
|
81
81
|
requirements: []
|
82
|
-
|
83
|
-
rubygems_version: 2.7.9
|
82
|
+
rubygems_version: 3.0.4
|
84
83
|
signing_key:
|
85
84
|
specification_version: 4
|
86
85
|
summary: Validates rails request params
|