sinatra-param-validator 0.15.0 → 0.17.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/CHANGELOG.md +13 -0
- data/Gemfile.lock +2 -2
- data/README.md +92 -15
- data/lib/sinatra/param_validator/helpers.rb +18 -7
- data/lib/sinatra/param_validator/version.rb +1 -1
- data/sinatra-param-validator.gemspec +3 -3
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc96ce9554cce1728ccfe8611b8a32683f59ecab0cb0c30f2048a4f36546cb85
|
4
|
+
data.tar.gz: 5a9a45fee8f03500085750f0af21c30e0d36c1d66dcd7c0799049435aae9cba4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87356c1bd08e3969b8e5d35709af6a18b4be740a38eedf9db3744741823fe4c00a7a494aaaece03cfe5495cd7acc149585f7eaa4036b4a36e91117063bf96fa8
|
7
|
+
data.tar.gz: d71e87ff1462b200e8a36a5f0d0fdd7321fa4b28356951baeba2704afa0246989c6bd95d517cb913b0bb66e2355cb332a5776c04108f7bbc1feb36d8e776047c
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,18 @@
|
|
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
|
+
|
8
|
+
## [0.16.1] - 2022-07-19
|
9
|
+
|
10
|
+
- Fix URIs in gemspec
|
11
|
+
|
12
|
+
## [0.16.0] - 2022-07-19
|
13
|
+
|
14
|
+
- Set default value for nil parameters
|
15
|
+
|
3
16
|
## [0.15.0] - 2022-07-19
|
4
17
|
|
5
18
|
- Pass message to raised exception
|
data/Gemfile.lock
CHANGED
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://
|
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,13 +58,23 @@ 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)
|
62
|
-
|
63
|
-
|
64
|
-
|
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
|
68
|
+
|
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)
|
73
|
+
end
|
74
|
+
|
75
|
+
def _set_param_to_default(key, default)
|
76
|
+
params[key] = default.respond_to?(:call) ? default.call : default
|
77
|
+
end
|
67
78
|
end
|
68
79
|
end
|
69
80
|
end
|
@@ -9,13 +9,13 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.email = ['rick@selby-family.co.uk']
|
10
10
|
|
11
11
|
spec.summary = 'Validation of parameters for Sinatra'
|
12
|
-
spec.homepage = 'https://github.com/rickselby/sinatra-
|
12
|
+
spec.homepage = 'https://github.com/rickselby/sinatra-param-validator'
|
13
13
|
spec.license = 'MIT'
|
14
14
|
spec.required_ruby_version = '>= 2.6.0'
|
15
15
|
|
16
|
-
spec.metadata['changelog_uri'] = 'https://github.com/rickselby/sinatra-
|
16
|
+
spec.metadata['changelog_uri'] = 'https://github.com/rickselby/sinatra-para-validator/CHANGELOG.md'
|
17
17
|
spec.metadata['homepage_uri'] = spec.homepage
|
18
|
-
spec.metadata['source_code_uri'] = 'https://github.com/rickselby/sinatra-
|
18
|
+
spec.metadata['source_code_uri'] = 'https://github.com/rickselby/sinatra-param-validator'
|
19
19
|
spec.metadata['rubygems_mfa_required'] = 'true'
|
20
20
|
|
21
21
|
# Specify which files should be added to the gem when it is released.
|
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.
|
4
|
+
version: 0.17.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rick Selby
|
@@ -166,13 +166,13 @@ files:
|
|
166
166
|
- lib/sinatra/param_validator/validator/url_param.rb
|
167
167
|
- lib/sinatra/param_validator/version.rb
|
168
168
|
- sinatra-param-validator.gemspec
|
169
|
-
homepage: https://github.com/rickselby/sinatra-
|
169
|
+
homepage: https://github.com/rickselby/sinatra-param-validator
|
170
170
|
licenses:
|
171
171
|
- MIT
|
172
172
|
metadata:
|
173
|
-
changelog_uri: https://github.com/rickselby/sinatra-
|
174
|
-
homepage_uri: https://github.com/rickselby/sinatra-
|
175
|
-
source_code_uri: https://github.com/rickselby/sinatra-
|
173
|
+
changelog_uri: https://github.com/rickselby/sinatra-para-validator/CHANGELOG.md
|
174
|
+
homepage_uri: https://github.com/rickselby/sinatra-param-validator
|
175
|
+
source_code_uri: https://github.com/rickselby/sinatra-param-validator
|
176
176
|
rubygems_mfa_required: 'true'
|
177
177
|
post_install_message:
|
178
178
|
rdoc_options: []
|