sinatra-param-validator 0.9.0 → 0.12.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 +12 -0
- data/Gemfile.lock +2 -2
- data/README.md +0 -9
- data/lib/sinatra/param_validator/parser.rb +36 -38
- 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: 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
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.12.0] - 2022-07-15
|
|
4
|
+
|
|
5
|
+
- Switch from a delegator to extending the context
|
|
6
|
+
|
|
7
|
+
## [0.11.0] - 2022-07-15
|
|
8
|
+
|
|
9
|
+
- Fix setting param values to false
|
|
10
|
+
|
|
11
|
+
## [0.10.0] - 2022-07-12
|
|
12
|
+
|
|
13
|
+
- Remove the `block` option from validators
|
|
14
|
+
|
|
3
15
|
## [0.9.0] - 2022-07-09
|
|
4
16
|
|
|
5
17
|
- Change parser to parse the definition outside of the initialize block
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -126,15 +126,6 @@ param :number, Integer, required: true do |validator|
|
|
|
126
126
|
end
|
|
127
127
|
```
|
|
128
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
|
-
|
|
138
129
|
## Rules
|
|
139
130
|
|
|
140
131
|
Rules work on multiple parameters:
|
|
@@ -8,68 +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))
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def block(&block)
|
|
32
|
-
run_block :block, block
|
|
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))
|
|
33
15
|
end
|
|
34
16
|
|
|
35
17
|
def param(key, type, default: nil, message: nil, **args, &block)
|
|
36
|
-
parameter = Parameter.new(
|
|
37
|
-
|
|
18
|
+
parameter = Parameter.new(params[key], type, **args)
|
|
19
|
+
_update_params_hash key, parameter, default
|
|
38
20
|
if parameter.valid?
|
|
39
|
-
|
|
21
|
+
_run_block(key, block) if block
|
|
40
22
|
else
|
|
41
|
-
|
|
23
|
+
_add_error key, message || parameter.errors
|
|
42
24
|
end
|
|
43
25
|
rescue NameError
|
|
44
26
|
raise 'Invalid parameter type'
|
|
45
27
|
end
|
|
46
28
|
|
|
47
29
|
def rule(name, *args, **kwargs)
|
|
48
|
-
rule = Rule.new(name,
|
|
30
|
+
rule = Rule.new(name, params, *args, **kwargs)
|
|
49
31
|
unless rule.passes?
|
|
50
|
-
@
|
|
51
|
-
@
|
|
32
|
+
@__validator_errors[:rules] ||= []
|
|
33
|
+
@__validator_errors[:rules].push(rule.errors)
|
|
52
34
|
end
|
|
53
35
|
rescue NameError
|
|
54
36
|
raise 'Invalid rule type'
|
|
55
37
|
end
|
|
56
38
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
def run_block(key, block)
|
|
39
|
+
def _run_block(key, block)
|
|
60
40
|
args = block.arity == 1 ? [self] : []
|
|
61
|
-
|
|
41
|
+
instance_exec(*args, &block)
|
|
62
42
|
rescue InvalidParameterError => e
|
|
63
|
-
|
|
43
|
+
_add_error key, e.message
|
|
64
44
|
end
|
|
65
45
|
|
|
66
|
-
def
|
|
67
|
-
if
|
|
68
|
-
|
|
46
|
+
def _update_params_hash(key, parameter, default)
|
|
47
|
+
if params.key?(key)
|
|
48
|
+
params[key] = parameter.coerced unless parameter.coerced.nil?
|
|
69
49
|
elsif !default.nil?
|
|
70
|
-
|
|
50
|
+
params[key] = default.respond_to?(:call) ? default.call : default
|
|
71
51
|
end
|
|
72
52
|
end
|
|
73
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
|
|
74
72
|
end
|
|
75
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.12.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-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rack-test
|