sinatra-param-validator 0.10.0 → 0.13.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 +12 -0
- data/Gemfile.lock +2 -2
- data/lib/sinatra/param_validator/parser.rb +36 -34
- data/lib/sinatra/param_validator/validator.rb +2 -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: 5cad00b1174bc3a3c95d063287d27226a96e06994238b4a350b1395316a708b1
|
4
|
+
data.tar.gz: 5133cf2b22163b3c3aff130f5151f3c249adae54c7c45f60bd02d6299bf6d50d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95c479b20400a9652a011ded28cd2d4d1c470e5acc531c44e1d9f9c2c643e55d0c5f9c09346c800615ab8fe9d46dbe69111077370a41fa154159a699dadc2f72
|
7
|
+
data.tar.gz: 676ad92c188cbe7d24ba9bbb596e28d79857242df12e572f5376a4b3a2f5c719b41107ab0640b38f6d27bfdf9ebaf4e2a5cea0c292d0a84e2ed0bdd244ac5759
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,17 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.13.0] - 2022-07-15
|
4
|
+
|
5
|
+
- Capture `InvalidParameterError`s raised when running the parser
|
6
|
+
|
7
|
+
## [0.12.0] - 2022-07-15
|
8
|
+
|
9
|
+
- Switch from a delegator to extending the context
|
10
|
+
|
11
|
+
## [0.11.0] - 2022-07-15
|
12
|
+
|
13
|
+
- Fix setting param values to false
|
14
|
+
|
3
15
|
## [0.10.0] - 2022-07-12
|
4
16
|
|
5
17
|
- Remove the `block` option from validators
|
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
|
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.13.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-07-
|
11
|
+
date: 2022-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack-test
|