request_params_validation 0.1.1 → 0.1.2
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 +39 -38
- data/lib/request_params_validation/definitions/param.rb +0 -1
- data/lib/request_params_validation/engine.rb +2 -2
- data/lib/request_params_validation/params.rb +2 -2
- data/lib/request_params_validation/params/types/validations.rb +1 -1
- data/lib/request_params_validation/params/validator.rb +1 -1
- data/lib/request_params_validation/params/validators/presence.rb +1 -1
- data/lib/request_params_validation/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9c0456b98e781cc72ec716aa4d06f18d32ba7cf26c97ba4bba193b995593ad8
|
4
|
+
data.tar.gz: 57e4e359a5f42f050db45b92355dd150de80bcd4698016ba8936dad4aabc6178
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1999797dee6855a5520933f665117d72acdd69d9014a14f87e1c88dea54dedac009a38c49b9dc11dac834cf026d4db1d0c695e9b7b95dbe461a4de5daebad27d
|
7
|
+
data.tar.gz: 4e9ba6145463b777212b7cde7b99346a61a5921e7dfb196818d65dd99dca74f96dae3009bf16d42d6de2320d366fa176055534e537fb1021b6f556e9b4e36e89
|
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
|
+
[](https://badge.fury.io/rb/request_params_validation)
|
4
5
|
[](https://circleci.com/gh/felipefava/request_params_validation)
|
6
|
+
[](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
|
```
|
@@ -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.
|
183
|
-
2.
|
184
|
-
3.
|
185
|
-
4.
|
186
|
-
5.
|
187
|
-
6.
|
188
|
-
7.
|
189
|
-
|
190
|
-
9.
|
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.
|
286
|
-
|
287
|
-
|
288
|
-
|
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.
|
@@ -442,7 +445,7 @@ end
|
|
442
445
|
```
|
443
446
|
|
444
447
|
---
|
445
|
-
|
448
|
+
### NOTE
|
446
449
|
|
447
450
|
RequestParamsValidation will start validating the presence of the parameters. Then, if the value is
|
448
451
|
not present and the parameter has a default value, it will assign that value and not execute any
|
@@ -450,8 +453,8 @@ further validation. Otherwise, it will validate the type, convert it to the righ
|
|
450
453
|
continue with the others validations. So, all others validations will be executed with the parameter
|
451
454
|
value already converter to the specified type, so keep in mind that at defining the validations.
|
452
455
|
|
453
|
-
|
454
456
|
## Errors & Messages
|
457
|
+
|
455
458
|
For default, when a required parameter failed the presence validation, the exception
|
456
459
|
`RequestParamsValidation::MissingParameterError` will be raised. If it failed for any of the others
|
457
460
|
validations, the raised exception will be `RequestParamsValidation::InvalidParameterValueError`
|
@@ -502,17 +505,15 @@ an element of an array or not. If you **have specified the `message` option in t
|
|
502
505
|
definition**, then the details will be that value, otherwise it will took a default value from
|
503
506
|
the table below:
|
504
507
|
|
505
|
-
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
| Invalid
|
510
|
-
| Invalid
|
511
|
-
| Invalid
|
512
|
-
| Invalid
|
513
|
-
| Invalid
|
514
|
-
| Invalid custom validation | N/A |
|
515
|
-
|
508
|
+
| Failure | Default Message |
|
509
|
+
| ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
510
|
+
| Missing parameter | N/A |
|
511
|
+
| 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 |
|
512
|
+
| Invalid inclusion | - `Value should be in %{include_in}` <br> - `All elements values of the array should have be in %{include_in}` |
|
513
|
+
| 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 ...` |
|
514
|
+
| 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 ...` |
|
515
|
+
| Invalid format | - `Value format is invalid` <br> - `An element of the array has an invalid format` |
|
516
|
+
| Invalid custom validation | N/A | |
|
516
517
|
|
517
518
|
### Custom Exceptions
|
518
519
|
However, if the above is not enough for your app, and you need to fully customize the exceptions
|
@@ -537,21 +538,21 @@ please see [here](./examples/initializer.rb).
|
|
537
538
|
## Future Work
|
538
539
|
In the near future the plan is to continue adding features to the gem. Next incoming changes
|
539
540
|
could be:
|
540
|
-
-
|
541
|
-
-
|
542
|
-
-
|
543
|
-
-
|
541
|
+
- Add doc generation from the definitions
|
542
|
+
- Add representations for DRY definitions
|
543
|
+
- Add more options to the actions definitions
|
544
|
+
- Add handler for responses
|
544
545
|
|
545
546
|
## Acknowledgments
|
546
547
|
This gem is strongly inspired in a Ruby framework named [Angus](https://github.com/moove-it/angus)
|
547
548
|
developed by [Moove It](https://moove-it.com/)
|
548
549
|
|
549
550
|
## Contributing
|
550
|
-
1.
|
551
|
-
2.
|
552
|
-
3.
|
553
|
-
4.
|
554
|
-
5.
|
551
|
+
1. Fork it
|
552
|
+
2. Create your feature branch (git checkout -b my-new-feature)
|
553
|
+
3. Commit your changes (git commit -am 'Add some feature')
|
554
|
+
4. Push to the branch (git push origin my-new-feature)
|
555
|
+
5. Create a Pull Request
|
555
556
|
|
556
557
|
## License
|
557
558
|
This software is released under the MIT license. See the MIT-LICENSE file for more info.
|
@@ -6,12 +6,12 @@ module RequestParamsValidation
|
|
6
6
|
isolate_namespace RequestParamsValidation
|
7
7
|
|
8
8
|
initializer 'request_params_validation.load_definitions' do
|
9
|
-
|
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
|
14
|
+
include Helpers
|
15
15
|
end
|
16
16
|
end
|
17
17
|
end
|
@@ -16,8 +16,8 @@ module RequestParamsValidation
|
|
16
16
|
def self.filter!(definition, params)
|
17
17
|
extra_keys = [:controller, :action] # Keys added by Rails
|
18
18
|
|
19
|
-
filter_params(definition, params, extra_keys).tap do |
|
20
|
-
|
19
|
+
filter_params(definition, params, extra_keys).tap do |filtered_params|
|
20
|
+
filtered_params.permit! if filtered_params.respond_to?(:permit!)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -57,7 +57,7 @@ module RequestParamsValidation
|
|
57
57
|
end
|
58
58
|
|
59
59
|
def iterate_hash
|
60
|
-
Params
|
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 = {})
|
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.
|
4
|
+
version: 0.1.2
|
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-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|