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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f474d7b6ff2e2ed3de6f58e3bd0ab0b328cc11886ac36701c5a1557a8713e5be
4
- data.tar.gz: 378f0b0bb00df8adca7f951175133833d68ce457066f79423aadb52cab1ba6fb
3
+ metadata.gz: d0704f790d84c6f56cc3f0d68f55ed3bcd95b5fa76fb9d185bb3b5421bb62348
4
+ data.tar.gz: 352199c3b098d9adb0e1666ee538b373a72afdb13c1cbd62ea686943fc7e4954
5
5
  SHA512:
6
- metadata.gz: 045a26ac9992b92e1dab3c63527b6ebfd2b70c84b554d20a7f38abfa070f0402da47776d663619c0ae1ff95c681a52ccfb7da34fa2f5c10928c28626c77f75a9
7
- data.tar.gz: cefd05d4f4babca139755e05d891802e375732bc944d278a8313e14759e87cd58b300c567e236ccf844897233072ce37e79d9c70f27385babd8b35435008a5c3
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
@@ -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.7.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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 param(key, type, message: nil, **args, &block)
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
- @context.params[key] = parameter.coerced if @context.params.key?(key) && parameter.coerced
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
- @context.instance_exec(&block)
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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sinatra
4
4
  module ParamValidator
5
- VERSION = '0.4.0'
5
+ VERSION = '0.7.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.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rick Selby