sinatra-param-validator 0.5.0 → 0.8.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 +2 -2
- data/README.md +12 -1
- data/lib/sinatra/param_validator/parser.rb +16 -2
- data/lib/sinatra/param_validator/validator/form.rb +27 -0
- data/lib/sinatra/param_validator/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e29508460ddab60a223a9a68607cf1463c71b4bec8b4aa8fc74596d25174a296
|
4
|
+
data.tar.gz: 2e25b479bfe8de0cde6eae7ccc2bb060ea66dee9d547120d5daca616d1c87f42
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b373671727a80340866970d2f1ef3b327735d622bcccad5bd27fcdf622351790cf017d0f516280ed741956ab1c0d249c6d546bad3b2a07f08c5943ca324f0437
|
7
|
+
data.tar.gz: 57c9eeb497333f36fb2eb2167edf4bd47df88139bcd29e6201402893c325ec3bb4888254278a7ab46a2f6d185079a5e7173478ba510f3b621af47b39fb68fe7e
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,18 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.8.0] - 2022-07-09
|
4
|
+
|
5
|
+
- Add form helpers for form validator
|
6
|
+
|
7
|
+
## [0.7.0] - 2022-06-09
|
8
|
+
|
9
|
+
- Add `default` option for parameters
|
10
|
+
- Allow `default` option to be a proc or a lambda
|
11
|
+
|
12
|
+
## [0.6.0] - 2022-06-09
|
13
|
+
|
14
|
+
- Add `block` to run blocks of code in the route context
|
15
|
+
|
3
16
|
## [0.5.0] - 2022-06-09
|
4
17
|
|
5
18
|
- Allow the validator to be passed to the block for a valid parameter
|
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`
|
@@ -118,12 +121,20 @@ with a message, and it will be passed through as an error for the parameter.
|
|
118
121
|
If you need to do further validation with your parameter, the validator can be passed to the block:
|
119
122
|
|
120
123
|
```ruby
|
121
|
-
|
122
124
|
param :number, Integer, required: true do |validator|
|
123
125
|
validator.param :digit, Integer, min: params[:number]
|
124
126
|
end
|
125
127
|
```
|
126
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
|
+
|
127
138
|
## Rules
|
128
139
|
|
129
140
|
Rules work on multiple parameters:
|
@@ -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,12 +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] : []
|
51
57
|
@context.instance_exec(*args, &block)
|
52
58
|
rescue InvalidParameterError => e
|
53
59
|
add_error key, e.message
|
54
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
|
55
69
|
end
|
56
70
|
end
|
57
71
|
end
|
@@ -5,6 +5,33 @@ module Sinatra
|
|
5
5
|
class Validator
|
6
6
|
# A form validator
|
7
7
|
class Form < Validator
|
8
|
+
# Helpers for Sinatra templates
|
9
|
+
module Helpers
|
10
|
+
def form_values(hash)
|
11
|
+
hash = IndifferentHash[hash]
|
12
|
+
flash.now[:params] = flash.now.key?(:params) ? hash.merge(flash.now[:params]) : hash
|
13
|
+
end
|
14
|
+
|
15
|
+
def form_value(field)
|
16
|
+
flash[:params]&.fetch(field, nil)
|
17
|
+
end
|
18
|
+
|
19
|
+
def form_error?(field = nil)
|
20
|
+
return !flash[:form_errors].nil? && !flash[:form_errors]&.empty? if field.nil?
|
21
|
+
|
22
|
+
(flash[:form_errors] || {}).key?(field)
|
23
|
+
end
|
24
|
+
|
25
|
+
def form_errors(field)
|
26
|
+
(flash[:form_errors] || {}).fetch(field, [])
|
27
|
+
end
|
28
|
+
|
29
|
+
def invalid_feedback(field, default = nil)
|
30
|
+
fields = Array(field)
|
31
|
+
fields.any? { |f| form_error? f } ? fields.map { |f| form_errors f }.flatten.join('<br />') : default
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
8
35
|
def handle_failure(context)
|
9
36
|
case context.request.preferred_type.to_s
|
10
37
|
when 'application/json' then return json_failure(context)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rick Selby
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-07-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack-test
|