sinatra-param-validator 0.4.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +13 -0
- data/Gemfile.lock +1 -1
- data/README.md +20 -3
- data/lib/sinatra/param_validator/parameter/common.rb +7 -3
- data/lib/sinatra/param_validator/parser.rb +18 -3
- data/lib/sinatra/param_validator/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0704f790d84c6f56cc3f0d68f55ed3bcd95b5fa76fb9d185bb3b5421bb62348
|
4
|
+
data.tar.gz: 352199c3b098d9adb0e1666ee538b373a72afdb13c1cbd62ea686943fc7e4954
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8df45995e8e88d7ead9edfdd8985f2b43a5904e08005f205a815286f2fa0fb36acb98494538f5bb30ee7733db8f8faf061cfc09ce48763f26176538881c9135e
|
7
|
+
data.tar.gz: 03caf360ce521261397eca9558f5dfe8f74df3bd6a3c1932cd5fda802f257f3025199cbd0c8f29098b231ccf6fea982947ff52fc5b19ea5309e746a8e5384293
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.7.0] - 2022-06-09
|
4
|
+
|
5
|
+
- Add `default` option for parameters
|
6
|
+
- Allow `default` option to be a proc or a lambda
|
7
|
+
|
8
|
+
## [0.6.0] - 2022-06-09
|
9
|
+
|
10
|
+
- Add `block` to run blocks of code in the route context
|
11
|
+
|
12
|
+
## [0.5.0] - 2022-06-09
|
13
|
+
|
14
|
+
- Allow the validator to be passed to the block for a valid parameter
|
15
|
+
|
3
16
|
## [0.4.0] - 2022-06-09
|
4
17
|
|
5
18
|
- Allow custom error messages to be used when validation fails
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -69,6 +69,9 @@ param :number, Integer, required: true, in: 0..100
|
|
69
69
|
|
70
70
|
All parameters have the following validations available:
|
71
71
|
|
72
|
+
* `default`
|
73
|
+
* Set a default value if this parameter was not provided
|
74
|
+
* This can be a lambda or a proc if required
|
72
75
|
* `nillable`
|
73
76
|
* If this is set, all other validations are skipped if the value is nil
|
74
77
|
* `required`
|
@@ -115,6 +118,23 @@ end
|
|
115
118
|
If you wish to indicate a validation failure within a block, raise `Sinatra::ParameterValidator::InvalidParameterError`
|
116
119
|
with a message, and it will be passed through as an error for the parameter.
|
117
120
|
|
121
|
+
If you need to do further validation with your parameter, the validator can be passed to the block:
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
param :number, Integer, required: true do |validator|
|
125
|
+
validator.param :digit, Integer, min: params[:number]
|
126
|
+
end
|
127
|
+
```
|
128
|
+
|
129
|
+
If you need to run some code in the route context, you can just use the `block` keyword:
|
130
|
+
|
131
|
+
```ruby
|
132
|
+
block do |validator|
|
133
|
+
#...
|
134
|
+
validator.param :val, Integer
|
135
|
+
end
|
136
|
+
```
|
137
|
+
|
118
138
|
## Rules
|
119
139
|
|
120
140
|
Rules work on multiple parameters:
|
@@ -149,7 +169,6 @@ post '/new-user', validate_form: :new_user do
|
|
149
169
|
# ...
|
150
170
|
end
|
151
171
|
|
152
|
-
|
153
172
|
get '/user/:id', validate_url_param: :user_id do
|
154
173
|
# ...
|
155
174
|
end
|
@@ -170,8 +189,6 @@ post '/number', validate: vi(:new_user, 10) do
|
|
170
189
|
end
|
171
190
|
```
|
172
191
|
|
173
|
-
|
174
|
-
|
175
192
|
## Development
|
176
193
|
|
177
194
|
After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests.
|
@@ -9,13 +9,17 @@ module Sinatra
|
|
9
9
|
|
10
10
|
def initialize(value, **options)
|
11
11
|
@errors = []
|
12
|
-
@coerced = coerce value
|
13
12
|
@options = options
|
14
13
|
|
14
|
+
begin
|
15
|
+
@coerced = coerce value
|
16
|
+
rescue ArgumentError
|
17
|
+
@errors.push "'#{value}' is not a valid #{self.class}"
|
18
|
+
return
|
19
|
+
end
|
20
|
+
|
15
21
|
validate_options
|
16
22
|
validate unless nil_and_ok?
|
17
|
-
rescue ArgumentError
|
18
|
-
@errors.push "'#{value}' is not a valid #{self.class}"
|
19
23
|
end
|
20
24
|
|
21
25
|
def valid?
|
@@ -24,9 +24,13 @@ module Sinatra
|
|
24
24
|
@errors[key] = @errors.fetch(key, []).concat(Array(error))
|
25
25
|
end
|
26
26
|
|
27
|
-
def
|
27
|
+
def block(&block)
|
28
|
+
run_block :block, block
|
29
|
+
end
|
30
|
+
|
31
|
+
def param(key, type, default: nil, message: nil, **args, &block)
|
28
32
|
parameter = Parameter.new(@context.params[key], type, **args)
|
29
|
-
|
33
|
+
update_params_hash key, parameter, default
|
30
34
|
if parameter.valid?
|
31
35
|
run_block(key, block) if block
|
32
36
|
else
|
@@ -46,11 +50,22 @@ module Sinatra
|
|
46
50
|
raise 'Invalid rule type'
|
47
51
|
end
|
48
52
|
|
53
|
+
private
|
54
|
+
|
49
55
|
def run_block(key, block)
|
50
|
-
|
56
|
+
args = block.arity == 1 ? [self] : []
|
57
|
+
@context.instance_exec(*args, &block)
|
51
58
|
rescue InvalidParameterError => e
|
52
59
|
add_error key, e.message
|
53
60
|
end
|
61
|
+
|
62
|
+
def update_params_hash(key, parameter, default)
|
63
|
+
if @context.params.key?(key)
|
64
|
+
@context.params[key] = parameter.coerced if parameter.coerced
|
65
|
+
elsif !default.nil?
|
66
|
+
@context.params[key] = default.respond_to?(:call) ? default.call : default
|
67
|
+
end
|
68
|
+
end
|
54
69
|
end
|
55
70
|
end
|
56
71
|
end
|