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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 902453e928aa07fd3a259794b029cc20213aa73555f7cc7348bfb202239d7c29
4
- data.tar.gz: 8e890aeaedfa9ad25dfb0a8b85181c70f817ec4371554d09a05dbfe409b23571
3
+ metadata.gz: 5cad00b1174bc3a3c95d063287d27226a96e06994238b4a350b1395316a708b1
4
+ data.tar.gz: 5133cf2b22163b3c3aff130f5151f3c249adae54c7c45f60bd02d6299bf6d50d
5
5
  SHA512:
6
- metadata.gz: b61d1563c52d501972584a02a2a0c74373675108fab55ad6272958fe5b28938e59c20e3b3f3f06c734a9adc94e140e89bc41a7756a6e68281c567dd09b9e82f3
7
- data.tar.gz: 2d0c36672894b4616092e63cd0771292f4c41f024c63bb01a5c6006e0a80a4824c6d0d8df2f2afa34c9854d89c0f871b85c13abac387c6219f85406775948261
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
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sinatra-param-validator (0.10.0)
4
+ sinatra-param-validator (0.13.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -85,4 +85,4 @@ DEPENDENCIES
85
85
  sinatra-param-validator!
86
86
 
87
87
  BUNDLED WITH
88
- 2.3.16
88
+ 2.3.17
@@ -8,64 +8,66 @@ require_relative 'rule'
8
8
 
9
9
  module Sinatra
10
10
  module ParamValidator
11
- # Run the definition in the given scope
12
- class Parser < SimpleDelegator
13
- attr_reader :errors
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(@context.params[key], type, **args)
33
- update_params_hash key, parameter, default
18
+ parameter = Parameter.new(params[key], type, **args)
19
+ _update_params_hash key, parameter, default
34
20
  if parameter.valid?
35
- run_block(key, block) if block
21
+ _run_block(key, block) if block
36
22
  else
37
- add_error key, message || parameter.errors
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, @context.params, *args, **kwargs)
30
+ rule = Rule.new(name, params, *args, **kwargs)
45
31
  unless rule.passes?
46
- @errors[:rules] ||= []
47
- @errors[:rules].push(rule.errors)
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
- private
54
-
55
- def run_block(key, block)
39
+ def _run_block(key, block)
56
40
  args = block.arity == 1 ? [self] : []
57
- @context.instance_exec(*args, &block)
41
+ instance_exec(*args, &block)
58
42
  rescue InvalidParameterError => e
59
- add_error key, e.message
43
+ _add_error key, e.message
60
44
  end
61
45
 
62
- def update_params_hash(key, parameter, default)
63
- if @context.params.key?(key)
64
- @context.params[key] = parameter.coerced if parameter.coerced
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
- @context.params[key] = default.respond_to?(:call) ? default.call : default
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
@@ -17,6 +17,8 @@ module Sinatra
17
17
 
18
18
  def run(context, *args)
19
19
  @errors = Parser.new(context).parse(@definition, *args).errors
20
+ rescue InvalidParameterError => e
21
+ @errors[:general] = [e.message]
20
22
  end
21
23
 
22
24
  def success?
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sinatra
4
4
  module ParamValidator
5
- VERSION = '0.10.0'
5
+ VERSION = '0.13.0'
6
6
  end
7
7
  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.10.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-12 00:00:00.000000000 Z
11
+ date: 2022-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack-test