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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 532c37904a1c0cbab6d818039ebeabe0886eae35f2f8b7f911931ca41b323712
4
- data.tar.gz: ea982841155186b8a46de89b449c30031aa1fdb0017f7f748becdd1225235427
3
+ metadata.gz: d9c0456b98e781cc72ec716aa4d06f18d32ba7cf26c97ba4bba193b995593ad8
4
+ data.tar.gz: 57e4e359a5f42f050db45b92355dd150de80bcd4698016ba8936dad4aabc6178
5
5
  SHA512:
6
- metadata.gz: 6187c851e82740b4e00f47c0b9f40b2930312eb349467b6e6e2197d50134c27e3cd4bdc77a53778881c68b08caafcd897257e4d25412f86213cbaa8d519f687d
7
- data.tar.gz: 91bb251b9af8c43b901c1709abb4ff0590dade190ffe2be320db5f86691e648f42c93610b469e948ec17b5defaca078865b93c5c755f880982a62469bee64d86
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
+ [![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
  ```
@@ -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.
@@ -442,7 +445,7 @@ end
442
445
  ```
443
446
 
444
447
  ---
445
- **NOTE**
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
- | 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
-
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> &nbsp;&nbsp;&nbsp; - ` 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
- - 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
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. 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
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.
@@ -1,5 +1,4 @@
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,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
@@ -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 |params|
20
- params.permit! if params.respond_to?(:permit!)
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
 
@@ -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
 
@@ -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.1'
2
+ VERSION = '0.1.2'
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.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-04-30 00:00:00.000000000 Z
11
+ date: 2020-05-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails