sinatra-param-validator 0.16.1 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 78acd25fda0c630a67c5ce3e2b24a9300c70432b40621a1ebd2822e4d63a549b
4
- data.tar.gz: 6ad51ab220feb9b3ec422532d1ae46bc2b812ec9969eec4e3b7a5b6b0387f4a6
3
+ metadata.gz: cc96ce9554cce1728ccfe8611b8a32683f59ecab0cb0c30f2048a4f36546cb85
4
+ data.tar.gz: 5a9a45fee8f03500085750f0af21c30e0d36c1d66dcd7c0799049435aae9cba4
5
5
  SHA512:
6
- metadata.gz: 224a086b11dc650444326980951756850ae049e4f09b17dac6ae73881327ae694ca2c38f3855887300ebeeed6a9fc36d61651d61884cbe237f057d964858e6e2
7
- data.tar.gz: 1b80d184a2e078f22a265cfcc3d38aa083f4cb6ad0e0850d193b5c39ef2bbe190ea87c7edbd628c9b3121472b7704367d58e9e9a8c4b357338daf14c0e70d8cf
6
+ metadata.gz: 87356c1bd08e3969b8e5d35709af6a18b4be740a38eedf9db3744741823fe4c00a7a494aaaece03cfe5495cd7acc149585f7eaa4036b4a36e91117063bf96fa8
7
+ data.tar.gz: d71e87ff1462b200e8a36a5f0d0fdd7321fa4b28356951baeba2704afa0246989c6bd95d517cb913b0bb66e2355cb332a5776c04108f7bbc1feb36d8e776047c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.17.0] - 2022-07-19
4
+
5
+ - `param` should return the coerced value
6
+ - Add `transform` to alter a validated parameter
7
+
3
8
  ## [0.16.1] - 2022-07-19
4
9
 
5
10
  - Fix URIs in gemspec
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sinatra-param-validator (0.16.1)
4
+ sinatra-param-validator (0.17.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -85,4 +85,4 @@ DEPENDENCIES
85
85
  sinatra-param-validator!
86
86
 
87
87
  BUNDLED WITH
88
- 2.3.17
88
+ 2.3.16
data/README.md CHANGED
@@ -76,6 +76,9 @@ All parameters have the following validations available:
76
76
  * If this is set, all other validations are skipped if the value is nil
77
77
  * `required`
78
78
  * The parameter must be present and cannot be nil
79
+ * `transform`
80
+ * Run a method against the validated parameter, allowing it to be changed
81
+ * Anything that responds to `to_proc` will work; procs, lambdas, symbols...
79
82
  * `in`
80
83
  * The value is in the given array / range
81
84
  * `is`
@@ -89,6 +92,41 @@ All parameters have the following validations available:
89
92
 
90
93
  * `min` / `max`
91
94
 
95
+ ### Running as a helper
96
+
97
+ `param` is available as a helper in routes, and will raise `Sinatra::ParamValidator::InvalidParameterError` if validation fails.
98
+ It will return the parsed parameter, after coercion, defaults and transformations have been applied.
99
+
100
+ ```ruby
101
+ get '/page' do
102
+ param :a, String
103
+ end
104
+ ```
105
+
106
+ ## Rules
107
+
108
+ Rules work on multiple parameters:
109
+
110
+ ```ruby
111
+ rule :all_or_none_of, :a, :b
112
+ ```
113
+
114
+ * `all_or_none_of`
115
+ * `any_of`
116
+ * At least one of the given fields must be present
117
+ * `one_of`
118
+ * Only one of the given fields can be present
119
+
120
+ ### Running as a helper
121
+
122
+ `rule` is available as a helper in routes, and will raise `Sinatra::ParamValidator::InvalidParameterError` if validation fails.
123
+
124
+ ```ruby
125
+ get '/page' do
126
+ rule :any_of, :a, :b
127
+ end
128
+ ```
129
+
92
130
  ## Custom Messages
93
131
 
94
132
  It is possible to return a custom error message when a validation fails:
@@ -113,6 +151,10 @@ It is possible to run code after a validation succeeds, by passing a block to `p
113
151
  param :number, Integer, required: true do
114
152
  # ...
115
153
  end
154
+
155
+ rule :any_of, :a, :b do
156
+ # ...
157
+ end
116
158
  ```
117
159
 
118
160
  If you wish to indicate a validation failure within a block, raise `Sinatra::ParameterValidator::InvalidParameterError`
@@ -126,20 +168,6 @@ param :number, Integer, required: true do |validator|
126
168
  end
127
169
  ```
128
170
 
129
- ## Rules
130
-
131
- Rules work on multiple parameters:
132
-
133
- ```ruby
134
- rule :all_or_none_of, :a, :b
135
- ```
136
-
137
- * `all_or_none_of`
138
- * `any_of`
139
- * At least one of the given fields must be present
140
- * `one_of`
141
- * Only one of the given fields can be present
142
-
143
171
  ## Validator Types
144
172
 
145
173
  The default validator will raise `Sinatra::ParamValidator::ValidationFailedError` when validation fails.
@@ -149,7 +177,7 @@ There are two other provided validators, that handle failure differently:
149
177
  * `url_param`
150
178
  * will `halt 403`
151
179
  * `form`
152
- * if [sinatra-flash](https://github.com/SFEley/sinatra-flash) is available, it will flash the errors and `redirect back`
180
+ * if [sinatra-flash](https://rubygems.org/gems/sinatra-flash) is available, it will flash the errors and `redirect back`
153
181
  * will provide a JSON object with errors to an XHR request
154
182
  * will `halt 400`
155
183
 
@@ -165,6 +193,55 @@ get '/user/:id', validate_url_param: :user_id do
165
193
  end
166
194
  ```
167
195
 
196
+ ### Form Helpers
197
+
198
+ There are some general helpers for handling values after validation.
199
+ These require the [sinatra-flash](https://rubygems.org/gems/sinatra-flash) gem.
200
+
201
+ * `form_values(hash)`
202
+ * Set the values that will be returned by `form_value`.
203
+ * These will be overridden by any values flashed to the session in the `:params` hash
204
+ * `form_value(field)`
205
+ * Get the form value for the given field
206
+ * `form_error?(field = nil)`
207
+ * With a field: returns if that field had any validation errors
208
+ * Without a field: returns if any field had validation errors
209
+ * `form_errors(field)`
210
+ * Get the list of validation errors for the given field
211
+ * `invalid_feedback(field, default = nil)`
212
+ * Get the invalid feedback (error message) for the given field, defaulting to the default parameter.
213
+
214
+ #### Usage
215
+
216
+ For a form to edit something, you can define the values that the form should use:
217
+
218
+ ```ruby
219
+ get '/edit/:thing' do
220
+ thing = #...
221
+ form_values thing
222
+ end
223
+ ```
224
+
225
+ This is an example form input with bootstrap styling:
226
+
227
+ ```html
228
+ <form method="post" <%== 'class="was-validated"' if form_error? %>>
229
+ <div class="mb-3">
230
+ <label class="form-label" for="name">Name</label>
231
+ <input type="text" name="name" id="name"
232
+ class="form-control <%= 'is-invalid' if form_error? :name %>"
233
+ value="<%= form_value :name %>">
234
+ <div class="invalid-feedback">
235
+ <%== invalid_feedback :name, 'Please provide a name.' %>
236
+ </div>
237
+ </div>
238
+ </form>
239
+ ```
240
+
241
+ When the form is submitted, validation may fail, and the page will redirect back to the edit form.
242
+ The redirect will flash the submitted parameters.
243
+ `form_value` will now prefer the flashed parameters over the original values for `thing`.
244
+
168
245
  ## Validators with parameters
169
246
 
170
247
  It is possible to define a validator with a parameter.
@@ -13,14 +13,15 @@ module Sinatra
13
13
  raise "Filter params failed: #{e}"
14
14
  end
15
15
 
16
- def param(key, type, default: nil, message: nil, **args, &block)
16
+ def param(key, type, default: nil, message: nil, transform: nil, **args, &block) # rubocop:disable Metrics/ParameterLists
17
17
  parameter = Parameter.new(params[key], type, **args)
18
- _update_params_hash key, parameter, default
19
18
  if parameter.valid?
19
+ _update_params_hash key, parameter, default, transform
20
20
  _run_block(key, block) if block
21
21
  else
22
22
  _handle_error key, message || parameter.errors
23
23
  end
24
+ params[key]
24
25
  rescue NameError
25
26
  raise 'Invalid parameter type'
26
27
  end
@@ -57,16 +58,18 @@ module Sinatra
57
58
  _handle_error key, e.message
58
59
  end
59
60
 
60
- def _update_params_hash(key, parameter, default)
61
- if params.key?(key) && !params[key].nil?
62
- _set_param_to_coerced key, parameter
63
- elsif !default.nil?
64
- _set_param_to_default key, default
61
+ def _update_params_hash(key, parameter, default, transform)
62
+ if !params.key?(key) || params[key].nil?
63
+ _set_param_to_default key, default unless default.nil?
64
+ else
65
+ _set_param_to_coerced key, parameter, transform
65
66
  end
66
67
  end
67
68
 
68
- def _set_param_to_coerced(key, parameter)
69
- params[key] = parameter.coerced unless parameter.coerced.nil?
69
+ def _set_param_to_coerced(key, parameter, transform)
70
+ return if parameter.coerced.nil?
71
+
72
+ params[key] = transform.nil? ? parameter.coerced : transform.to_proc.call(parameter.coerced)
70
73
  end
71
74
 
72
75
  def _set_param_to_default(key, default)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sinatra
4
4
  module ParamValidator
5
- VERSION = '0.16.1'
5
+ VERSION = '0.17.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-param-validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.1
4
+ version: 0.17.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Selby