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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9c0456b98e781cc72ec716aa4d06f18d32ba7cf26c97ba4bba193b995593ad8
4
- data.tar.gz: 57e4e359a5f42f050db45b92355dd150de80bcd4698016ba8936dad4aabc6178
3
+ metadata.gz: c20b8745b806808c41cbc90ec9eea641bd2bf4530621225d68fcf5678367da59
4
+ data.tar.gz: 567570643a25a91a22cdbfde4ae416a26e32c83b27447be9210cd3309747043c
5
5
  SHA512:
6
- metadata.gz: 1999797dee6855a5520933f665117d72acdd69d9014a14f87e1c88dea54dedac009a38c49b9dc11dac834cf026d4db1d0c695e9b7b95dbe461a4de5daebad27d
7
- data.tar.gz: 4e9ba6145463b777212b7cde7b99346a61a5921e7dfb196818d65dd99dca74f96dae3009bf16d42d6de2320d366fa176055534e537fb1021b6f556e9b4e36e89
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 for all
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> &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}` |
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
- params[key] = value
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])
@@ -24,8 +24,6 @@ module RequestParamsValidation
24
24
  def self.apply_transformation(param, value)
25
25
  transform = param.transform
26
26
 
27
- return value unless transform
28
-
29
27
  transform.respond_to?(:call) ? transform.call(value) : value.send(transform)
30
28
  end
31
29
  end
@@ -45,7 +45,7 @@ module RequestParamsValidation
45
45
  validate_format! if param.validate_format?
46
46
  validate_custom_validation! if param.validate_custom_validation?
47
47
 
48
- @value = Params::Converter.apply_transformation(param, value)
48
+ @value
49
49
  end
50
50
 
51
51
  private
@@ -1,3 +1,3 @@
1
1
  module RequestParamsValidation
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
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.2
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-05 00:00:00.000000000 Z
11
+ date: 2020-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails