request_params_validation 0.1.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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +557 -0
- data/Rakefile +32 -0
- data/lib/request_params_validation.rb +346 -0
- data/lib/request_params_validation/definitions.rb +57 -0
- data/lib/request_params_validation/definitions/action.rb +23 -0
- data/lib/request_params_validation/definitions/param.rb +182 -0
- data/lib/request_params_validation/definitions/request.rb +31 -0
- data/lib/request_params_validation/definitions/resource.rb +30 -0
- data/lib/request_params_validation/engine.rb +18 -0
- data/lib/request_params_validation/exceptions/base_errors.rb +10 -0
- data/lib/request_params_validation/exceptions/definitions_errors.rb +31 -0
- data/lib/request_params_validation/exceptions/validator_errors.rb +49 -0
- data/lib/request_params_validation/handler.rb +26 -0
- data/lib/request_params_validation/helpers.rb +17 -0
- data/lib/request_params_validation/params.rb +60 -0
- data/lib/request_params_validation/params/constants.rb +22 -0
- data/lib/request_params_validation/params/converter.rb +33 -0
- data/lib/request_params_validation/params/types/conversions.rb +41 -0
- data/lib/request_params_validation/params/types/validations.rb +57 -0
- data/lib/request_params_validation/params/validator.rb +74 -0
- data/lib/request_params_validation/params/validators/custom.rb +18 -0
- data/lib/request_params_validation/params/validators/format.rb +27 -0
- data/lib/request_params_validation/params/validators/inclusion.rb +27 -0
- data/lib/request_params_validation/params/validators/length.rb +37 -0
- data/lib/request_params_validation/params/validators/presence.rb +13 -0
- data/lib/request_params_validation/params/validators/type.rb +59 -0
- data/lib/request_params_validation/params/validators/value.rb +37 -0
- data/lib/request_params_validation/version.rb +3 -0
- metadata +87 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5ccaf41aab1f3773847d076043efb7c16dcac54d48e525a101791f35f4fc6000
|
4
|
+
data.tar.gz: 55b76ff4f3a8f1d6c433283b9e0962b28bd6dff76298672a0e92f9639019b57e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cc0c695d265475d49f9142c1da677dd67341d96332077ab72fda3eaa5857a373609350f8a73df6c6e466795a7d384fd8b542585d58b9f27050770168cd879e4a
|
7
|
+
data.tar.gz: 1f014823715f92ba77b6668753b382bb978ff881a3a228376595174a77aa832a573b3ad5e84cc398842acaf7eb05bbde16b413c3e7b9630135d2937064f4164d
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2020 Felipe Fava
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,557 @@
|
|
1
|
+
# RequestParamsValidation
|
2
|
+
_Request parameters validations, type coercion and filtering for Rails params_
|
3
|
+
|
4
|
+
[](https://circleci.com/gh/felipefava/request_params_validation)
|
5
|
+
|
6
|
+
## Introduction
|
7
|
+
Validates the request params outside your controller logic in order to get a clean nice code, and
|
8
|
+
also working as code documentation for the operations. It ensure that all endpoints input data is
|
9
|
+
right and well formed before it even hits your controller action.
|
10
|
+
|
11
|
+
This gem allows you to validate the presence, type, length, format, value and more, of your request
|
12
|
+
parameters. It also coerces the params to the specified type and filter the hash to only those you
|
13
|
+
expect to receive.
|
14
|
+
|
15
|
+
It is designed to work for any expected params structure, from a simple hash to a complex one with
|
16
|
+
deeply nested data. It pretends to be a flexible library where you can change and customize
|
17
|
+
several options.
|
18
|
+
|
19
|
+
It is intended for REST-like Ruby on Rails APIs.
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'request_params_validation'
|
26
|
+
```
|
27
|
+
|
28
|
+
And then execute `bundle install` from your shell.
|
29
|
+
|
30
|
+
Or, if you want to install it manually, run:
|
31
|
+
```ruby
|
32
|
+
gem install request_params_validation
|
33
|
+
```
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
To start using the gem without setting up any configuration is as simple as adding a
|
37
|
+
`before_action` with the helper method `validate_params!` and define your expected request
|
38
|
+
parameters for your resources actions.
|
39
|
+
|
40
|
+
The approach of this gem is to have, for each controller file, a definition file. This definitions
|
41
|
+
files is where it should be all data related to the endpoints of your API. This works as code
|
42
|
+
documentation and allows to keep controllers code clean, ensuring that `params` object will
|
43
|
+
always have the parameters you suppose to receive.
|
44
|
+
|
45
|
+
The default path for the definitions files is `app/definitions`, and their names should be the same
|
46
|
+
as their respective controller's name, but ending with the suffix `_definition`. They also should
|
47
|
+
respect the folder structure of the controllers folder. Please see the following project structure
|
48
|
+
to clarify the idea:
|
49
|
+
|
50
|
+
```
|
51
|
+
.
|
52
|
+
├── app
|
53
|
+
│ ├── controllers
|
54
|
+
│ │ ├── commerces
|
55
|
+
| | | └── branches_controller.rb
|
56
|
+
| | |
|
57
|
+
| | ├── transactions_controller.rb
|
58
|
+
│ │ └── users_controller.rb
|
59
|
+
| |
|
60
|
+
│ ├── definitions
|
61
|
+
│ │ ├── commerces
|
62
|
+
| | | └── branches_definition.rb
|
63
|
+
| | |
|
64
|
+
| | ├── transactions_definition.rb
|
65
|
+
│ │ └── users_definition.rb
|
66
|
+
│ └── ...
|
67
|
+
|
|
68
|
+
└── ...
|
69
|
+
```
|
70
|
+
|
71
|
+
This gem comes with a set of configurable options allowing you to customize it to your needs.
|
72
|
+
For example, you can change the default helper method `validate_params!` for whatever name you
|
73
|
+
want. You can also change the default path folder for the definitions `app/definitions` and even
|
74
|
+
the suffix `_definition` of the file names. [Here](#configuration) you can see all
|
75
|
+
globals configuration options
|
76
|
+
|
77
|
+
### Example
|
78
|
+
Add the `before_action` callback for all actions:
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# app/controllers/application_controller.rb
|
82
|
+
|
83
|
+
class ApplicationController < ActionController::Base
|
84
|
+
before_action :validate_params!
|
85
|
+
end
|
86
|
+
```
|
87
|
+
|
88
|
+
Imagine we have the following resource and we want to define the params for the action `create`
|
89
|
+
and `notify`:
|
90
|
+
|
91
|
+
```ruby
|
92
|
+
# app/controllers/users_controller.rb
|
93
|
+
|
94
|
+
class UsersController < ApplicationController
|
95
|
+
def create
|
96
|
+
...
|
97
|
+
end
|
98
|
+
|
99
|
+
def notify
|
100
|
+
...
|
101
|
+
end
|
102
|
+
|
103
|
+
def another_action
|
104
|
+
...
|
105
|
+
end
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
Then, we will need to create the definition for the `users` resource:
|
110
|
+
|
111
|
+
```ruby
|
112
|
+
# app/definitions/users_definition.rb
|
113
|
+
|
114
|
+
RequestParamsValidation.define do |users|
|
115
|
+
users.action :create do |create|
|
116
|
+
users.request do |params|
|
117
|
+
params.required :user, type: :hash do |user|
|
118
|
+
user.required :first_name, type: :string
|
119
|
+
user.required :last_name, type: :string
|
120
|
+
user.required :emails, type: :array, elements: :email
|
121
|
+
user.required :birth_date,
|
122
|
+
type: :datetime,
|
123
|
+
validate: lambda { |value| value <= 18.years.ago.to_date }
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
users.action :notify do |notify|
|
129
|
+
notify.request do |params|
|
130
|
+
params.required :user_id, type: :integer
|
131
|
+
params.required :message, type: :string, length: { min: 10, max: 250 }
|
132
|
+
params.optional :by, inclusion: %w(email text_msg push), default: :email
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
```
|
137
|
+
|
138
|
+
The above definition is just a silly example, but is good enough to explain some important things.
|
139
|
+
|
140
|
+
The first thing to say is, as we already mentioned, that each controller file matches with a
|
141
|
+
definition file with the same name and path of it, as you can see in the first line of the example
|
142
|
+
above. Be aware that if the definition file doesn't exist for a controller, then the gem will not
|
143
|
+
validate any param, unless you change this behavior with the global configuration option
|
144
|
+
`config.on_definition_not_found`. [Here](#configuration) you can see all globals
|
145
|
+
configuration options.
|
146
|
+
|
147
|
+
As you might notice, the method `RequestParamsValidation.define` allow you to define a
|
148
|
+
resource/controller. Notice that the resource you are defining is given by the current
|
149
|
+
definition file path/name. After defining the resource, you can continue defining the
|
150
|
+
actions for that resource with the `action` method. Then, for each action you can define the
|
151
|
+
request using the `request` method, and there is where you will define the params validations
|
152
|
+
for the current resource/action. You could think that the `request` step is not strictly
|
153
|
+
necessary, because we could just defined the params validations inside de action block. However,
|
154
|
+
it will have more sense in the future, when more extra options be added.
|
155
|
+
|
156
|
+
For defining required parameters we use the `required` method, otherwise
|
157
|
+
we have the `optional` method. This two methods accept 2 arguments and a block. The first argument
|
158
|
+
is the only one required, and is the name or key of the parameter. The second argument is an
|
159
|
+
options hash for specifing the extra validations, and the block is for defining nested params.
|
160
|
+
|
161
|
+
In the following section we will see all the options validations in-depth look.
|
162
|
+
|
163
|
+
## Validations & Options
|
164
|
+
None of the below options are required, so they can be omitted if you don't need to use them.
|
165
|
+
|
166
|
+
### Presence
|
167
|
+
If a parameter is required, then you should use the `required` method on the definition of the
|
168
|
+
param. Otherwise use the `optional` method. For default, required parameters don't accept blank
|
169
|
+
values, if you would like to allow them for that parameter, you can use the option `allow_blank`
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
some_action.request do |params|
|
173
|
+
params.required :key_1
|
174
|
+
params.required :key_2, allow_blank: true
|
175
|
+
params.optional :key_3
|
176
|
+
end
|
177
|
+
```
|
178
|
+
|
179
|
+
### Types
|
180
|
+
The `type` option specified the type of the parameter. The supported types are:
|
181
|
+
|
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
|
191
|
+
|
192
|
+
So if this option is present, the gem will validate that the value of the parameter matches with
|
193
|
+
the specified type. And if it does, it will convert the value to the right type. This means that
|
194
|
+
if a parameter should be an `integer`, a valid string integer like `"100"` will be converter to
|
195
|
+
`100`. The same applies to the other types.
|
196
|
+
|
197
|
+
If you want to add your own types, you can extend the supported types with the global
|
198
|
+
configuration option `extend.types`. See [here](#configuration) all globals
|
199
|
+
configuration options.
|
200
|
+
|
201
|
+
```ruby
|
202
|
+
some_action.request do |params|
|
203
|
+
params.required :key_1, type: :boolean
|
204
|
+
params.required :key_2, type: :decimal
|
205
|
+
# ...
|
206
|
+
end
|
207
|
+
```
|
208
|
+
|
209
|
+
Let's see each of the types now.
|
210
|
+
|
211
|
+
#### Hash type
|
212
|
+
When defining a hash parameter, you will need to pass a block for specifing the nested object.
|
213
|
+
If no block is passed, the gem will only check that the value of the parameter be a valid hash
|
214
|
+
object, without validating the content of it.
|
215
|
+
|
216
|
+
```ruby
|
217
|
+
some_action.request do |params|
|
218
|
+
# Allows any keys and values for the hash
|
219
|
+
params.required :key_1, type: :hash
|
220
|
+
|
221
|
+
# Only allows the keys nested_key_1 and nested_key_2
|
222
|
+
params.required :key_2, type: :hash do |key_name|
|
223
|
+
key_name.required :nested_key_1, type: :string
|
224
|
+
key_name.required :nested_key_2, type: :integer
|
225
|
+
end
|
226
|
+
end
|
227
|
+
```
|
228
|
+
|
229
|
+
#### Array type
|
230
|
+
If you define an array parameter, the gem will only check the value to be a valid array, allowing
|
231
|
+
the elements of the array to be anything. If you also want to validate the elements, you can use
|
232
|
+
the option `elements`.
|
233
|
+
|
234
|
+
The value for this option can be a type or a hash. `elements: :integer` is equivalent to
|
235
|
+
`elements: { type: :integer }`.
|
236
|
+
|
237
|
+
The second way is useful when you want to validate other things of the elements than just the
|
238
|
+
type. The option elements accepts all validations options.
|
239
|
+
|
240
|
+
```ruby
|
241
|
+
some_action.request do |params|
|
242
|
+
# Allows any value for the elements of the array
|
243
|
+
params.required :key_1, type: :array
|
244
|
+
|
245
|
+
# Only allows decimals with a value less than 1_000 for the elements of the array
|
246
|
+
params.required :key_2, type: :array, elements: { type: :decimal, value: { max: 1_000 }
|
247
|
+
|
248
|
+
# Only allows objects with a required key 'nested_key' of type 'email' for the
|
249
|
+
# elements of the array
|
250
|
+
params.required :key_3, type: :array, elements: :hash do |key_3|
|
251
|
+
key_3.required :nested_key, type: :email
|
252
|
+
end
|
253
|
+
end
|
254
|
+
```
|
255
|
+
|
256
|
+
#### String type
|
257
|
+
Any value is a valid string.
|
258
|
+
|
259
|
+
#### Integer type
|
260
|
+
Accepts only valid integers like `5` or `"5"`.
|
261
|
+
|
262
|
+
#### Decimal type
|
263
|
+
Accepts only valid decimals like `5` or `1.5` or `10.45`. With decimals parameters you can use
|
264
|
+
the option `precision`. Go [here](#precision) for more details about this option.
|
265
|
+
|
266
|
+
#### Boolean type
|
267
|
+
Accepts only valid boolean values. The default valid boolean values are:
|
268
|
+
|
269
|
+
```ruby
|
270
|
+
[true, false, 'true', 'false']
|
271
|
+
```
|
272
|
+
|
273
|
+
If you need to add more values for the boolean type, for example `['yes', 'no', 1, 0, 't', 'f']`,
|
274
|
+
you can extend the `true values` and the `false values` independently, with the global
|
275
|
+
configuration options `extend.boolean_true_values` and `extend.boolean_false_values` respectively.
|
276
|
+
See [here](#configuration) all globals configuration options.
|
277
|
+
|
278
|
+
#### Date type
|
279
|
+
Date type accepts only valid dates. This means that values like `'04/10/1995'` are valids, and
|
280
|
+
will be converter to a Date object like `Wed, 04 Oct 1995`.
|
281
|
+
|
282
|
+
However, they are cases when you only want to accept a specific format for a date, like
|
283
|
+
`"%Y-%m-%e"`. In this cases you have two options.
|
284
|
+
|
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.
|
289
|
+
|
290
|
+
You can perfectly use both approaches, but the second one will locally override the first one on
|
291
|
+
that parameter validation.
|
292
|
+
|
293
|
+
Notice that if no format is specified, the date will be validated using the ruby `Date.parse`
|
294
|
+
method.
|
295
|
+
|
296
|
+
```ruby
|
297
|
+
some_action.request do |params|
|
298
|
+
params.required :key_1, type: :date
|
299
|
+
params.required :key_2, type: :date, format: '%Y-%m-%e'
|
300
|
+
end
|
301
|
+
```
|
302
|
+
|
303
|
+
#### Datetime type
|
304
|
+
Same as `date` type but for `datetime`.
|
305
|
+
|
306
|
+
#### Email type
|
307
|
+
Accepts only valid emails like `john.doe@mail.com`. It's just a helper for a string type with
|
308
|
+
an email regexp format.
|
309
|
+
|
310
|
+
### Inclusion
|
311
|
+
The `inclusion` option is for validating that the param value is included in a given array.
|
312
|
+
|
313
|
+
The value for this option can be an enumerable or a hash. `inclusion: %w(asc desc)` is equivalent
|
314
|
+
to `inclusion: { in: %w(asc desc) }`.
|
315
|
+
|
316
|
+
Besides from the `in` option, you can also use the `message` option for passing a custom error
|
317
|
+
detail when the parameter is not valid.
|
318
|
+
|
319
|
+
```ruby
|
320
|
+
some_action.request do |params|
|
321
|
+
params.required :key_1, type: :string, inclusion: %w(asc desc)
|
322
|
+
params.required :key_2,
|
323
|
+
type: :string,
|
324
|
+
inclusion: { in: %w(s m l), message: 'Value is not a valid size' }
|
325
|
+
end
|
326
|
+
```
|
327
|
+
|
328
|
+
### Length
|
329
|
+
The `length` option is for validating the length of the param value.
|
330
|
+
|
331
|
+
The value for this option can be an integer or a hash. `length: 5` is equivalent
|
332
|
+
to `length: { min: 5, max: 5 }`.
|
333
|
+
|
334
|
+
Besides from the `min` and `max` options, you can also use the `message` option for passing a
|
335
|
+
custom error detail when the parameter is not valid.
|
336
|
+
|
337
|
+
```ruby
|
338
|
+
some_action.request do |params|
|
339
|
+
params.required :key_1, type: :string, length: 10
|
340
|
+
params.required :key_2, type: :string, length: { min: 5, max: 12 }
|
341
|
+
params.required :key_3, type: :array, elements: :email, length: { max: 3 }
|
342
|
+
params.required :key_4, type: :string, length: { max: 25,
|
343
|
+
message: '25 characters is the maximum allowed' }
|
344
|
+
end
|
345
|
+
```
|
346
|
+
|
347
|
+
### Value Size
|
348
|
+
The `value` option is for validating the value size of numerics parameters.
|
349
|
+
|
350
|
+
The value for this option is a hash with the following options: `min`, `max` and `message`.
|
351
|
+
|
352
|
+
```ruby
|
353
|
+
some_action.request do |params|
|
354
|
+
params.required :key_1, type: :integer, value: { min: 0 }
|
355
|
+
params.required :key_2, type: :integer, value: { max: 1_000_000, message: 'Value too big!' }
|
356
|
+
params.required :key_3, type: :decimal, value: { min: 0, max: 1 }
|
357
|
+
end
|
358
|
+
```
|
359
|
+
|
360
|
+
### Format
|
361
|
+
The `format` option allows to validate de format of the value with a regular expression.
|
362
|
+
|
363
|
+
The value for this option is a `regexp`, `string` or a `hash`. The string value is only valid
|
364
|
+
when the type is a `date` or a `datetime`. Otherwise, you should use a regexp. The options for
|
365
|
+
the hash are: `regexp`, `strptime` and `message`.
|
366
|
+
|
367
|
+
So, for `date` and `datetime` types, `format: '%u%F'` is equivalent to
|
368
|
+
`format: { strptime: '%u%F' }`. For the other types, `format: /^5[1-5]\d{14}$/` is
|
369
|
+
equivalent to `format: { regexp: /^5[1-5]\d{14}$/ }`.
|
370
|
+
|
371
|
+
```ruby
|
372
|
+
some_action.request do |params|
|
373
|
+
params.required :key_1, type: :string, format: /^5[1-5]\d{14}$/
|
374
|
+
params.required :key_2, type: :string, format: { regexp: /^1.*/,
|
375
|
+
message: 'Value should start with a 1' }
|
376
|
+
end
|
377
|
+
```
|
378
|
+
|
379
|
+
### Custom Validation
|
380
|
+
You can add custom validations to the parameter with the option `validate`.
|
381
|
+
|
382
|
+
This option accepts a Proc as value or a hash. For example,
|
383
|
+
`validate: lambda { |value| value > Date.today }` is equivalent to
|
384
|
+
`validate: { function: lambda { |value| value > Date.today } }`. The hash value
|
385
|
+
also accepts the `message` option.
|
386
|
+
|
387
|
+
```ruby
|
388
|
+
some_action.request do |params|
|
389
|
+
params.required :key_1, type: :date, validate: { function: lambda { |value| value >= Date.today },
|
390
|
+
message: 'The date can not be in the past' }
|
391
|
+
end
|
392
|
+
```
|
393
|
+
|
394
|
+
### Precision <a name='precision'></a>
|
395
|
+
The `precision` option are for `decimal` types. This option does not execute any validation
|
396
|
+
on the value of the parameter, but it will round the decimal when the value is converter to
|
397
|
+
the specified type.
|
398
|
+
|
399
|
+
If you want to set a precision value to all `decimal` parameters, you can use the global
|
400
|
+
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.
|
403
|
+
|
404
|
+
This option accepts an integer as value.
|
405
|
+
|
406
|
+
```ruby
|
407
|
+
some_action.request do |params|
|
408
|
+
params.required :key_1, type: :decimal, precision: 2
|
409
|
+
end
|
410
|
+
```
|
411
|
+
|
412
|
+
### Default Values
|
413
|
+
When parameters are optional, with the `default` option you can set a default value when the parameter is not
|
414
|
+
present.
|
415
|
+
|
416
|
+
The value for the option `default` could be anything, including a proc.
|
417
|
+
|
418
|
+
```ruby
|
419
|
+
some_action.request do |params|
|
420
|
+
params.optional :key_1, type: :string, default: 'Jane'
|
421
|
+
params.optional :key_2, type: :string, default: lambda { Date.today.strftime('%A') }
|
422
|
+
end
|
423
|
+
```
|
424
|
+
|
425
|
+
### Transformations
|
426
|
+
Transformations are functions that are called to the value of the parameter, after it has already
|
427
|
+
been validated. The option for this is `transform`.
|
428
|
+
|
429
|
+
The `transform` option could be a symbol, or a proc. The proc will receive the value of the
|
430
|
+
parameter as an argument, so keep in mind that the value will be already of the type
|
431
|
+
specified in the definition. So, `transform: :strip` is equivalent to
|
432
|
+
`transform: lambda { |value| value.strip }`.
|
433
|
+
|
434
|
+
```ruby
|
435
|
+
some_action.request do |params|
|
436
|
+
params.optional :key_1, type: :string, transform: :strip
|
437
|
+
params.optional :key_2,
|
438
|
+
type: :string,
|
439
|
+
format: /^\d{3}-\d{3}-\d{3}$/,
|
440
|
+
transform: lambda { |value| value.gsub(/-/, '') }
|
441
|
+
end
|
442
|
+
```
|
443
|
+
|
444
|
+
---
|
445
|
+
**NOTE**
|
446
|
+
|
447
|
+
RequestParamsValidation will start validating the presence of the parameters. Then, if the value is
|
448
|
+
not present and the parameter has a default value, it will assign that value and not execute any
|
449
|
+
further validation. Otherwise, it will validate the type, convert it to the right type and then
|
450
|
+
continue with the others validations. So, all others validations will be executed with the parameter
|
451
|
+
value already converter to the specified type, so keep in mind that at defining the validations.
|
452
|
+
|
453
|
+
|
454
|
+
## Errors & Messages
|
455
|
+
For default, when a required parameter failed the presence validation, the exception
|
456
|
+
`RequestParamsValidation::MissingParameterError` will be raised. If it failed for any of the others
|
457
|
+
validations, the raised exception will be `RequestParamsValidation::InvalidParameterValueError`
|
458
|
+
with a proper descriptive error message.
|
459
|
+
|
460
|
+
This two exceptions inherits from `RequestParamsValidation::RequestParamError`, so you
|
461
|
+
can rescue the exceptions like this:
|
462
|
+
|
463
|
+
```ruby
|
464
|
+
# app/controllers/application_controller.rb
|
465
|
+
|
466
|
+
class ApplicationController < ActionController::Base
|
467
|
+
rescue_from RequestParamsValidation::RequestParamError do |exception|
|
468
|
+
# do whatever you want
|
469
|
+
end
|
470
|
+
end
|
471
|
+
```
|
472
|
+
|
473
|
+
Both exceptions has getters methods to access data related to the failure. For example, the
|
474
|
+
`RequestParamsValidation::MissingParameterError` exception has two public methods `param_key`
|
475
|
+
and `param_type` for getting the name and type of the parameter which failed. And the
|
476
|
+
`RequestParamsValidation::InvalidParameterValueError` exception has the two mentioned methods,
|
477
|
+
plus the methods `param_value` and `details`. `param_value` returns the value of the parameter,
|
478
|
+
and `details` give more information about the reason of the failure.
|
479
|
+
|
480
|
+
### Errors messages
|
481
|
+
For the exception `RequestParamsValidation::MissingParameterError`, the error message is the
|
482
|
+
following:
|
483
|
+
|
484
|
+
```ruby
|
485
|
+
"The parameter '#{param_key}' is missing"
|
486
|
+
```
|
487
|
+
|
488
|
+
And for `RequestParamsValidation::InvalidParameterValueError` the message is:
|
489
|
+
|
490
|
+
```ruby
|
491
|
+
"The value for the parameter '#{param_key}' is invalid"
|
492
|
+
```
|
493
|
+
|
494
|
+
Or, if `details` is present:
|
495
|
+
|
496
|
+
```ruby
|
497
|
+
"The value for the parameter '#{param_key}' is invalid. #{details}"
|
498
|
+
```
|
499
|
+
|
500
|
+
The details is different depending on the reason of the failure, and whether the parameter is
|
501
|
+
an element of an array or not. If you **have specified the `message` option in the parameter
|
502
|
+
definition**, then the details will be that value, otherwise it will took a default value from
|
503
|
+
the table below:
|
504
|
+
|
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> - ` 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
|
+
|
516
|
+
|
517
|
+
### Custom Exceptions
|
518
|
+
However, if the above is not enough for your app, and you need to fully customize the exceptions
|
519
|
+
and the messages, you can setup your own exceptions classes for each type of failure. They are
|
520
|
+
globals configurations options that allow you to do that. See below to see them all.
|
521
|
+
|
522
|
+
## Global Configurations <a name='configuration'></a>
|
523
|
+
Global configurations help you to customize the gem to fulfill your needs. To change this
|
524
|
+
configuration, you need to create an initializer and configure what you want to change:
|
525
|
+
|
526
|
+
```ruby
|
527
|
+
# config/initializers/request_params_validation.rb
|
528
|
+
|
529
|
+
RequestParamsValidation.configure do |config|
|
530
|
+
#... here goes the configuration
|
531
|
+
end
|
532
|
+
```
|
533
|
+
|
534
|
+
To see a complete initializer file of the configuration with all the options and their description,
|
535
|
+
please see [here](./examples/initializer.rb).
|
536
|
+
|
537
|
+
## Future Work
|
538
|
+
In the near future the plan is to continue adding features to the gem. Next incoming changes
|
539
|
+
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
|
544
|
+
|
545
|
+
## Acknowledgments
|
546
|
+
This gem is strongly inspired in a Ruby framework named [Angus](https://github.com/moove-it/angus)
|
547
|
+
developed by [Moove It](https://moove-it.com/)
|
548
|
+
|
549
|
+
## 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
|
555
|
+
|
556
|
+
## License
|
557
|
+
This software is released under the MIT license. See the MIT-LICENSE file for more info.
|