sinatra-param-validator 0.4.0 → 0.5.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: f474d7b6ff2e2ed3de6f58e3bd0ab0b328cc11886ac36701c5a1557a8713e5be
4
- data.tar.gz: 378f0b0bb00df8adca7f951175133833d68ce457066f79423aadb52cab1ba6fb
3
+ metadata.gz: 1312fa49d8b256875f1c6de21269a7be31ab1c3f2ca0a5efdb9902c31b5406d4
4
+ data.tar.gz: 4b483ce648a24026e26964b0cdd358a4f1256ffe7dc810770e8dbf2b98204306
5
5
  SHA512:
6
- metadata.gz: 045a26ac9992b92e1dab3c63527b6ebfd2b70c84b554d20a7f38abfa070f0402da47776d663619c0ae1ff95c681a52ccfb7da34fa2f5c10928c28626c77f75a9
7
- data.tar.gz: cefd05d4f4babca139755e05d891802e375732bc944d278a8313e14759e87cd58b300c567e236ccf844897233072ce37e79d9c70f27385babd8b35435008a5c3
6
+ metadata.gz: 2714270e73581d5beb88eb136516fc1d5275d05ebe2a3c5efca08d54b64bb2bcda467cb45cb4ab3fa7fee063606171356f9fe869cbedc0950afc7b92cb5dc8eb
7
+ data.tar.gz: 692691bde5e786c23897080967004fc5fb5b695d49942fd671db69ef6ec91c00771a0afbfc5e69954b5e22f8e585829c8e08a04b6a954cf14ec46b1c19dfdf8c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.5.0] - 2022-06-09
4
+
5
+ - Allow the validator to be passed to the block for a valid parameter
6
+
3
7
  ## [0.4.0] - 2022-06-09
4
8
 
5
9
  - Allow custom error messages to be used when validation fails
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sinatra-param-validator (0.4.0)
4
+ sinatra-param-validator (0.5.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -115,6 +115,15 @@ end
115
115
  If you wish to indicate a validation failure within a block, raise `Sinatra::ParameterValidator::InvalidParameterError`
116
116
  with a message, and it will be passed through as an error for the parameter.
117
117
 
118
+ If you need to do further validation with your parameter, the validator can be passed to the block:
119
+
120
+ ```ruby
121
+
122
+ param :number, Integer, required: true do |validator|
123
+ validator.param :digit, Integer, min: params[:number]
124
+ end
125
+ ```
126
+
118
127
  ## Rules
119
128
 
120
129
  Rules work on multiple parameters:
@@ -149,7 +158,6 @@ post '/new-user', validate_form: :new_user do
149
158
  # ...
150
159
  end
151
160
 
152
-
153
161
  get '/user/:id', validate_url_param: :user_id do
154
162
  # ...
155
163
  end
@@ -170,8 +178,6 @@ post '/number', validate: vi(:new_user, 10) do
170
178
  end
171
179
  ```
172
180
 
173
-
174
-
175
181
  ## Development
176
182
 
177
183
  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?
@@ -47,7 +47,8 @@ module Sinatra
47
47
  end
48
48
 
49
49
  def run_block(key, block)
50
- @context.instance_exec(&block)
50
+ args = block.arity == 1 ? [self] : []
51
+ @context.instance_exec(*args, &block)
51
52
  rescue InvalidParameterError => e
52
53
  add_error key, e.message
53
54
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sinatra
4
4
  module ParamValidator
5
- VERSION = '0.4.0'
5
+ VERSION = '0.5.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.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Selby