sinatra-param-validator 0.6.0 → 0.7.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 +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +3 -0
- data/lib/sinatra/param_validator/parser.rb +12 -2
- 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
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`
|
@@ -28,9 +28,9 @@ module Sinatra
|
|
28
28
|
run_block :block, block
|
29
29
|
end
|
30
30
|
|
31
|
-
def param(key, type, message: nil, **args, &block)
|
31
|
+
def param(key, type, default: nil, message: nil, **args, &block)
|
32
32
|
parameter = Parameter.new(@context.params[key], type, **args)
|
33
|
-
|
33
|
+
update_params_hash key, parameter, default
|
34
34
|
if parameter.valid?
|
35
35
|
run_block(key, block) if block
|
36
36
|
else
|
@@ -50,12 +50,22 @@ module Sinatra
|
|
50
50
|
raise 'Invalid rule type'
|
51
51
|
end
|
52
52
|
|
53
|
+
private
|
54
|
+
|
53
55
|
def run_block(key, block)
|
54
56
|
args = block.arity == 1 ? [self] : []
|
55
57
|
@context.instance_exec(*args, &block)
|
56
58
|
rescue InvalidParameterError => e
|
57
59
|
add_error key, e.message
|
58
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
|
59
69
|
end
|
60
70
|
end
|
61
71
|
end
|