request_params_validation 0.1.2 → 0.2.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 +4 -4
- data/README.md +15 -3
- data/lib/request_params_validation.rb +10 -0
- data/lib/request_params_validation/definitions/param.rb +10 -1
- data/lib/request_params_validation/params.rb +8 -2
- data/lib/request_params_validation/params/converter.rb +0 -2
- data/lib/request_params_validation/params/validator.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: c20b8745b806808c41cbc90ec9eea641bd2bf4530621225d68fcf5678367da59
|
4
|
+
data.tar.gz: 567570643a25a91a22cdbfde4ae416a26e32c83b27447be9210cd3309747043c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd6e167c9a3edc7dbc52306405d66aa370aa73cfe90c225bfb90d6dbd4a6750a26448adf3a4d25d9ffe7443c26681a0c190efa7bad9a0f99b436f99b07cfaf8d
|
7
|
+
data.tar.gz: e27ef25b9b6969f089fd78bd6f7b1b12efd43f90dd7fa4c97ee40c080c828e2483bbe987bffe456704b29b8bfac829c23a7503537c99ea63f016a25d53e7b09c
|
data/README.md
CHANGED
@@ -401,8 +401,8 @@ the specified type.
|
|
401
401
|
|
402
402
|
If you want to set a precision value to all `decimal` parameters, you can use the global
|
403
403
|
configuration option `format.decimal_precision`. Keep in mind that if you set the `precision`
|
404
|
-
option on a parameter, it will locally override the global configuration. See here
|
405
|
-
globals configuration options.
|
404
|
+
option on a parameter, it will locally override the global configuration. See [here](#configuration)
|
405
|
+
for all globals configuration options.
|
406
406
|
|
407
407
|
This option accepts an integer as value.
|
408
408
|
|
@@ -444,6 +444,18 @@ some_action.request do |params|
|
|
444
444
|
end
|
445
445
|
```
|
446
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
|
+
|
447
459
|
---
|
448
460
|
### NOTE
|
449
461
|
|
@@ -509,7 +521,7 @@ the table below:
|
|
509
521
|
| ------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
510
522
|
| Missing parameter | N/A |
|
511
523
|
| 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
|
524
|
+
| Invalid inclusion | - `Value should be in %{include_in}` <br> - `All elements values of the array should be in %{include_in}` |
|
513
525
|
| 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
526
|
| 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
527
|
| Invalid format | - `Value format is invalid` <br> - `An element of the array has an invalid format` |
|
@@ -344,3 +344,13 @@ module RequestParamsValidation
|
|
344
344
|
end
|
345
345
|
end
|
346
346
|
end
|
347
|
+
|
348
|
+
# given some optional param validate another one if the given is present
|
349
|
+
# same_as option for example for password confirmation
|
350
|
+
# Parameters can be defined as mutually_exclusive, ensuring that they aren't present at the same time in a request.
|
351
|
+
# https://github.com/ruby-grape/grape#allow_blank
|
352
|
+
|
353
|
+
|
354
|
+
# Warning: Never define mutually exclusive sets with any required params. Two mutually exclusive required params will mean params are never valid, thus making the endpoint useless. One required param mutually exclusive with an optional param will mean the latter is never valid.
|
355
|
+
|
356
|
+
# Grape returns all validation and coercion errors found by default. To skip all subsequent validation checks when a specific param is found invalid, use fail_fast: true.
|
@@ -3,7 +3,7 @@ require 'request_params_validation/params'
|
|
3
3
|
module RequestParamsValidation
|
4
4
|
module Definitions
|
5
5
|
class Param
|
6
|
-
attr_reader :key, :required, :allow_blank, :type, :transform, :decimal_precision,
|
6
|
+
attr_reader :key, :required, :allow_blank, :type, :rename_as, :transform, :decimal_precision,
|
7
7
|
:inclusion, :length, :value, :format, :custom_validation, :elements
|
8
8
|
|
9
9
|
def initialize(options, &block)
|
@@ -11,6 +11,7 @@ module RequestParamsValidation
|
|
11
11
|
@required = options[:required]
|
12
12
|
@allow_blank = options[:allow_blank]
|
13
13
|
@type = options[:type].try(:to_sym)
|
14
|
+
@rename_as = options[:as].try(:to_sym)
|
14
15
|
@default = options[:default]
|
15
16
|
|
16
17
|
@transform = options[:transform]
|
@@ -73,6 +74,14 @@ module RequestParamsValidation
|
|
73
74
|
!!@custom_validation
|
74
75
|
end
|
75
76
|
|
77
|
+
def rename?
|
78
|
+
!!@rename_as
|
79
|
+
end
|
80
|
+
|
81
|
+
def transform?
|
82
|
+
!!@transform
|
83
|
+
end
|
84
|
+
|
76
85
|
private
|
77
86
|
|
78
87
|
def build_inclusion_option(inclusion)
|
@@ -26,8 +26,14 @@ module RequestParamsValidation
|
|
26
26
|
value = params[key]
|
27
27
|
|
28
28
|
value = Validator.new(param_definition, value).validate_and_coerce
|
29
|
+
value = Converter.apply_transformation(param_definition, value) if param_definition.transform?
|
29
30
|
|
30
|
-
|
31
|
+
if param_definition.rename?
|
32
|
+
params.delete(key)
|
33
|
+
params[param_definition.rename_as] = value
|
34
|
+
else
|
35
|
+
params[key] = value
|
36
|
+
end
|
31
37
|
end
|
32
38
|
private_class_method :validate_and_coerce_param
|
33
39
|
|
@@ -36,7 +42,7 @@ module RequestParamsValidation
|
|
36
42
|
return params if definition.empty?
|
37
43
|
|
38
44
|
params_keys = definition.map do |param_definition|
|
39
|
-
key = param_definition.key
|
45
|
+
key = param_definition.rename? ? param_definition.rename_as : param_definition.key
|
40
46
|
|
41
47
|
if param_definition.sub_definition
|
42
48
|
filter_params(param_definition.sub_definition, params[key])
|
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.2.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-05-
|
11
|
+
date: 2020-05-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|