sinatra-param-validator 0.11.0 → 0.12.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 +4 -0
- data/Gemfile.lock +1 -1
- data/lib/sinatra/param_validator/parser.rb +36 -34
- 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: b892596c037b0e00101f33a36c3fef30985e3514e3f0b74d706e55c27e03f921
|
4
|
+
data.tar.gz: 8c6d082dc416b889ca799fb49de1fef7f11af81656c5be29d30f8c191529a4c1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 85089ee9a10e79bd4cf56684e3e63a6487ea6015c093bebd17b720ea52851679790316f0c65bfc4b47bcfe399bc13d4a4c2062127c69c0c1743957663522cc14
|
7
|
+
data.tar.gz: 608f712978969fe990aefa8e2d1c90daa99871d9c9588c86b82fe0ae30873a256f8b27d4c834ad445014c32895a53a626090b32a1993469d8e40dd50bb079191
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -8,64 +8,66 @@ require_relative 'rule'
|
|
8
8
|
|
9
9
|
module Sinatra
|
10
10
|
module ParamValidator
|
11
|
-
#
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
def initialize(context)
|
16
|
-
super(context)
|
17
|
-
@context = context
|
18
|
-
@errors = {}
|
19
|
-
end
|
20
|
-
|
21
|
-
def parse(definition, *args)
|
22
|
-
instance_exec(*args, &definition)
|
23
|
-
|
24
|
-
self
|
25
|
-
end
|
26
|
-
|
27
|
-
def add_error(key, error)
|
28
|
-
@errors[key] = @errors.fetch(key, []).concat(Array(error))
|
11
|
+
# Methods to add to the context for handling parameters
|
12
|
+
module ValidatorMethods
|
13
|
+
def _add_error(key, error)
|
14
|
+
@__validator_errors[key] = @__validator_errors.fetch(key, []).concat(Array(error))
|
29
15
|
end
|
30
16
|
|
31
17
|
def param(key, type, default: nil, message: nil, **args, &block)
|
32
|
-
parameter = Parameter.new(
|
33
|
-
|
18
|
+
parameter = Parameter.new(params[key], type, **args)
|
19
|
+
_update_params_hash key, parameter, default
|
34
20
|
if parameter.valid?
|
35
|
-
|
21
|
+
_run_block(key, block) if block
|
36
22
|
else
|
37
|
-
|
23
|
+
_add_error key, message || parameter.errors
|
38
24
|
end
|
39
25
|
rescue NameError
|
40
26
|
raise 'Invalid parameter type'
|
41
27
|
end
|
42
28
|
|
43
29
|
def rule(name, *args, **kwargs)
|
44
|
-
rule = Rule.new(name,
|
30
|
+
rule = Rule.new(name, params, *args, **kwargs)
|
45
31
|
unless rule.passes?
|
46
|
-
@
|
47
|
-
@
|
32
|
+
@__validator_errors[:rules] ||= []
|
33
|
+
@__validator_errors[:rules].push(rule.errors)
|
48
34
|
end
|
49
35
|
rescue NameError
|
50
36
|
raise 'Invalid rule type'
|
51
37
|
end
|
52
38
|
|
53
|
-
|
54
|
-
|
55
|
-
def run_block(key, block)
|
39
|
+
def _run_block(key, block)
|
56
40
|
args = block.arity == 1 ? [self] : []
|
57
|
-
|
41
|
+
instance_exec(*args, &block)
|
58
42
|
rescue InvalidParameterError => e
|
59
|
-
|
43
|
+
_add_error key, e.message
|
60
44
|
end
|
61
45
|
|
62
|
-
def
|
63
|
-
if
|
64
|
-
|
46
|
+
def _update_params_hash(key, parameter, default)
|
47
|
+
if params.key?(key)
|
48
|
+
params[key] = parameter.coerced unless parameter.coerced.nil?
|
65
49
|
elsif !default.nil?
|
66
|
-
|
50
|
+
params[key] = default.respond_to?(:call) ? default.call : default
|
67
51
|
end
|
68
52
|
end
|
69
53
|
end
|
54
|
+
|
55
|
+
# Run the definition in the given scope
|
56
|
+
class Parser
|
57
|
+
attr_reader :errors
|
58
|
+
|
59
|
+
def initialize(context)
|
60
|
+
@context = context
|
61
|
+
end
|
62
|
+
|
63
|
+
def parse(definition, *args)
|
64
|
+
@context.extend(ValidatorMethods)
|
65
|
+
@context.instance_variable_set(:@__validator_errors, {})
|
66
|
+
@context.instance_exec(*args, &definition)
|
67
|
+
@errors = @context.instance_variable_get(:@__validator_errors)
|
68
|
+
|
69
|
+
self
|
70
|
+
end
|
71
|
+
end
|
70
72
|
end
|
71
73
|
end
|