validate-params 0.4.1 → 0.5.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: 2d696ca11ac7883af0c1213783efa1f9d5b0ea21643c5fb91af78f6d1edfe7d3
4
- data.tar.gz: 4646124157428bbd8906accd5bb1d3c50debb88a0010abd3d1543a72c2c4265d
3
+ metadata.gz: e2d071e5e303d7093044e9d93df803d42cbab04a50d3982930d1a2caa5b430da
4
+ data.tar.gz: a3c75285f19ca61d41206e6714ce87884e9219f897d2293120438c9aa8affe50
5
5
  SHA512:
6
- metadata.gz: 59247d076c2d88dcc3b277660f109b3194844da3c78ed4fd41d3d07bb79f51728e60fb2c8cfe6668b1ce479826d5f24ac58eb2a07a924b5cfa4b086934662cd5
7
- data.tar.gz: c42359ad0c204034e6ca1d545301a907bbe92c5f7d857a13e41b1c41bbfe9cc31cb2904d3fccdf684552bdd903ab314f46c34c22fa9d2e799e70e41d731cf304
6
+ metadata.gz: a88e088ed079b06bed177dbdd9fdcf1ce01af371923b0539fa23336c7a8db32ec153f45bd191eba9d80b3990709e85f32441558886b09f26edf77f88b474f436
7
+ data.tar.gz: 5ba5d625af372572ac1060c5e181a5931a68a3c8a3ef725409899fda8638c21493d4223bc27ca04b77fc83fdd3922a52a1a17794fb713992f235587a53c69ce6
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.5.0] - 2023-05-15
4
+
5
+ - Support for Proc as options for default to set param default values
6
+ - Add :in options to validate param values against a list of values for String and Integer types
7
+ - Updated JSON formats of error messages to be more consistent
8
+
3
9
  ## [0.3.0] - 2023-05-12
4
10
 
5
11
  - Add required attribute support
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- validate-params (0.3.0)
4
+ validate-params (0.5.0)
5
5
  activesupport (>= 6.1.0)
6
6
 
7
7
  GEM
@@ -43,8 +43,6 @@ GEM
43
43
  nokogiri (1.14.3)
44
44
  mini_portile2 (~> 2.8.0)
45
45
  racc (~> 1.4)
46
- nokogiri (1.14.3-arm64-darwin)
47
- racc (~> 1.4)
48
46
  parallel (1.23.0)
49
47
  parser (3.2.2.1)
50
48
  ast (~> 2.4.1)
@@ -93,8 +91,6 @@ GEM
93
91
  zeitwerk (2.6.8)
94
92
 
95
93
  PLATFORMS
96
- arm64-darwin-21
97
- arm64-darwin-22
98
94
  ruby
99
95
 
100
96
  DEPENDENCIES
data/README.md CHANGED
@@ -48,7 +48,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
48
48
 
49
49
  ## Contributing
50
50
 
51
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/validate_params.
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/peopleforce/validate_params.
52
52
 
53
53
  ## Credits
54
54
 
@@ -1,4 +1,5 @@
1
1
  en:
2
2
  validate_params:
3
- required: "The parameter %{field} is required"
4
- invalid: "Invalid %{type} value '%{value}' for parameter %{field}"
3
+ required: "%{param} is required"
4
+ invalid_type: "%{param} must be a valid %{type}"
5
+ invalid_in: "%{param} is an invalid value"
@@ -12,13 +12,13 @@ module ValidateParams
12
12
  class_methods do
13
13
  attr_accessor :params_validations, :method
14
14
 
15
- def param(field, type, required: false, default: nil, &block)
15
+ def param(field, type, options = {}, &block)
16
16
  @params_validations ||= []
17
17
  @params_validations <<
18
18
  if block
19
19
  yield(ParamBuilder.new(field))
20
20
  else
21
- ParamBuilder.new.param(field, type, required: required, default: default)
21
+ ParamBuilder.new.param(field, type, options)
22
22
  end
23
23
  end
24
24
 
@@ -39,12 +39,12 @@ module ValidateParams
39
39
 
40
40
  private
41
41
 
42
- def build_error_message(field, type, value)
43
- I18n.t("validate_params.invalid", field: field, type: type, value: value)
42
+ def build_error_message(param, type)
43
+ I18n.t("validate_params.invalid_type", param: param, type: type)
44
44
  end
45
45
 
46
- def build_required_message(field)
47
- I18n.t("validate_params.required", field: field)
46
+ def build_required_message(param)
47
+ I18n.t("validate_params.required", param: param)
48
48
  end
49
49
 
50
50
  def error_param_name(field)
@@ -60,16 +60,27 @@ module ValidateParams
60
60
 
61
61
  def set_params_defaults
62
62
  params_validations.each do |params_validation|
63
- next if params_validation[:default].blank?
63
+ next if params_validation[:options][:default].blank?
64
64
 
65
65
  if params_validation[:field].is_a?(Hash)
66
66
  params_validation[:field].each_key do |key|
67
67
  next if params.dig(key, params_validation[:field][key])
68
68
 
69
- params.merge!(key => { params_validation[:field][key] => params_validation[:default] })
69
+ value = if params_validation[:options][:default].is_a?(Proc)
70
+ params_validation[:options][:default].call
71
+ else
72
+ params_validation[:options][:default]
73
+ end
74
+ params.merge!(key => { params_validation[:field][key] => value })
70
75
  end
71
76
  else
72
- params[params_validation[:field]] ||= params_validation[:default]
77
+ value = if params_validation[:options][:default].is_a?(Proc)
78
+ params_validation[:options][:default].call
79
+ else
80
+ params_validation[:options][:default]
81
+ end
82
+
83
+ params[params_validation[:field]] ||= value
73
84
  end
74
85
  end
75
86
  end
@@ -87,37 +98,48 @@ module ValidateParams
87
98
  params[params_validation[:field]]
88
99
  end
89
100
 
90
- next if parameter_value.blank? && !params_validation[:required]
101
+ next if parameter_value.blank? && !params_validation[:options][:required]
91
102
 
92
- if parameter_value.blank? && params_validation[:required]
93
- errors << build_required_message(error_param_name(params_validation[:field]))
103
+ if parameter_value.blank? && params_validation[:options][:required]
104
+ errors << { message: build_required_message(error_param_name(params_validation[:field])) }
94
105
  next
95
106
  end
96
107
 
97
108
  case params_validation[:type].to_s
98
109
  when "Date"
99
110
  if invalid_date?(parameter_value)
100
- errors << build_error_message(
101
- error_param_name(params_validation[:field]),
102
- params_validation[:type],
103
- parameter_value
104
- )
111
+ errors << {
112
+ message: build_error_message(error_param_name(params_validation[:field]), params_validation[:type])
113
+ }
105
114
  end
106
115
  when "DateTime"
107
116
  if invalid_datetime?(parameter_value)
108
- errors << build_error_message(
109
- error_param_name(params_validation[:field]),
110
- params_validation[:type],
111
- parameter_value
112
- )
117
+ errors << {
118
+ message: build_error_message(error_param_name(params_validation[:field]), params_validation[:type])
119
+ }
113
120
  end
114
121
  when "Integer"
115
- if invalid_integer?(parameter_value)
116
- errors << build_error_message(
117
- error_param_name(params_validation[:field]),
118
- params_validation[:type],
119
- parameter_value
120
- )
122
+ if invalid_integer?(parameter_value)
123
+ errors << {
124
+ message: build_error_message(error_param_name(params_validation[:field]), params_validation[:type])
125
+ }
126
+ next
127
+ end
128
+
129
+ parameter_value = parameter_value.to_i
130
+ if params_validation[:options][:in].present? && !params_validation[:options][:in].include?(parameter_value)
131
+ errors << {
132
+ message: I18n.t("validate_params.invalid_in", param: error_param_name(params_validation[:field])),
133
+ valid_values: params_validation[:options][:in]
134
+ }
135
+ end
136
+ when "String"
137
+ parameter_value = parameter_value.to_s
138
+ if params_validation[:options][:in].present? && !params_validation[:options][:in].include?(parameter_value)
139
+ errors << {
140
+ message: I18n.t("validate_params.invalid_in", param: error_param_name(params_validation[:field])),
141
+ valid_values: params_validation[:options][:in]
142
+ }
121
143
  end
122
144
  end
123
145
  end
@@ -131,10 +153,10 @@ module ValidateParams
131
153
  return true unless /\d{4}-\d{2}-\d{2}/.match?(value)
132
154
 
133
155
  parsed_date = begin
134
- Date.strptime(value, "%Y-%m-%d")
135
- rescue StandardError
136
- nil
137
- end
156
+ Date.strptime(value, "%Y-%m-%d")
157
+ rescue StandardError
158
+ nil
159
+ end
138
160
  parsed_date.blank? || parsed_date.year > 9999
139
161
  end
140
162
 
@@ -146,7 +168,7 @@ module ValidateParams
146
168
  end
147
169
 
148
170
  def invalid_integer?(value)
149
- value !~ /\A[-+]?[0-9]+\z/
171
+ value.to_s !~ /\A[-+]?[0-9]+\z/
150
172
  end
151
173
 
152
174
  class ParamBuilder
@@ -154,11 +176,12 @@ module ValidateParams
154
176
  @parent_field = parent_field
155
177
  end
156
178
 
157
- def param(field, type, required: false, default: nil)
179
+ def param(field, type, options = {})
180
+
158
181
  if @parent_field
159
- { field: { @parent_field => field }, type: type, required: required, default: default }
182
+ { field: { @parent_field => field }, type: type, options: options }
160
183
  else
161
- { field: field, type: type, required: required, default: default }
184
+ { field: field, type: type, options: options }
162
185
  end
163
186
  end
164
187
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ValidateParams
4
- VERSION = "0.4.1"
4
+ VERSION = "0.5.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: validate-params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - dcherevatenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-12 00:00:00.000000000 Z
11
+ date: 2023-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack