sinatra-param-validator 0.12.0 → 0.15.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: b892596c037b0e00101f33a36c3fef30985e3514e3f0b74d706e55c27e03f921
4
- data.tar.gz: 8c6d082dc416b889ca799fb49de1fef7f11af81656c5be29d30f8c191529a4c1
3
+ metadata.gz: 0635a378cefa1851f127f94720dbcdb8a6abe12093af7fcdafcccb5066c3a10d
4
+ data.tar.gz: 2a8fe28e566133d4e72aa0188824f184a02984475dc1f79796d3d7a98e914957
5
5
  SHA512:
6
- metadata.gz: 85089ee9a10e79bd4cf56684e3e63a6487ea6015c093bebd17b720ea52851679790316f0c65bfc4b47bcfe399bc13d4a4c2062127c69c0c1743957663522cc14
7
- data.tar.gz: 608f712978969fe990aefa8e2d1c90daa99871d9c9588c86b82fe0ae30873a256f8b27d4c834ad445014c32895a53a626090b32a1993469d8e40dd50bb079191
6
+ metadata.gz: 4ff032b54cf35d989b81613ee6b965a40a8b18699d183fdeef32c08a5b0d8a520415ec43a270c6b84d288446bae8813fd17a8eb9f1d5c77fcee3ad8b8eb5bb63
7
+ data.tar.gz: adc4c502ff12a8730598c28c13871ecd52e4c8749bbe4c5ea7b9256727cd3997b9b31152e4d22c3e908ae39eca8c40b236a65ebf8c7e5ff1dfa41fd75b632b7d
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.15.0] - 2022-07-19
4
+
5
+ - Pass message to raised exception
6
+ - Allow rules to run blocks
7
+
8
+ ## [0.14.0] - 2022-07-18
9
+
10
+ - Fix parameter coercion if a parameter is validated multiple times
11
+ - Refactor `param` and `rule` functions into helpers
12
+ - Allow `param` and `rule` to be run independently
13
+
14
+ ## [0.13.0] - 2022-07-18
15
+
16
+ - Capture `InvalidParameterError`s raised when running the parser
17
+
3
18
  ## [0.12.0] - 2022-07-15
4
19
 
5
20
  - Switch from a delegator to extending the context
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sinatra-param-validator (0.12.0)
4
+ sinatra-param-validator (0.15.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
@@ -13,6 +13,29 @@ module Sinatra
13
13
  raise "Filter params failed: #{e}"
14
14
  end
15
15
 
16
+ def param(key, type, default: nil, message: nil, **args, &block)
17
+ parameter = Parameter.new(params[key], type, **args)
18
+ _update_params_hash key, parameter, default
19
+ if parameter.valid?
20
+ _run_block(key, block) if block
21
+ else
22
+ _handle_error key, message || parameter.errors
23
+ end
24
+ rescue NameError
25
+ raise 'Invalid parameter type'
26
+ end
27
+
28
+ def rule(name, *args, **kwargs, &block)
29
+ rule = Rule.new(name, params, *args, **kwargs)
30
+ if rule.passes?
31
+ _run_block(:rules, block) if block
32
+ else
33
+ _handle_error :rules, rule.errors unless rule.passes?
34
+ end
35
+ rescue NameError
36
+ raise 'Invalid rule type'
37
+ end
38
+
16
39
  def validate(klass, identifier)
17
40
  identifier = Identifier.new(identifier) if identifier.is_a? Symbol
18
41
  definition = settings.validator_definitions.get(identifier.identifier)
@@ -20,6 +43,27 @@ module Sinatra
20
43
  validator.run(self, *identifier.args)
21
44
  validator.handle_failure(self) unless validator.success?
22
45
  end
46
+
47
+ def _handle_error(key, error)
48
+ raise InvalidParameterError, error unless defined? @_validator_errors
49
+
50
+ @_validator_errors[key] = @_validator_errors.fetch(key, []).concat(Array(error))
51
+ end
52
+
53
+ def _run_block(key, block)
54
+ args = block.arity == 1 ? [self] : []
55
+ instance_exec(*args, &block)
56
+ rescue InvalidParameterError => e
57
+ _handle_error key, e.message
58
+ end
59
+
60
+ def _update_params_hash(key, parameter, default)
61
+ if params.key?(key)
62
+ params[key] = parameter.coerced unless parameter.coerced.nil?
63
+ elsif !default.nil?
64
+ params[key] = default.respond_to?(:call) ? default.call : default
65
+ end
66
+ end
23
67
  end
24
68
  end
25
69
  end
@@ -15,6 +15,7 @@ module Sinatra
15
15
 
16
16
  def coerce(value)
17
17
  return nil if value.nil?
18
+ return value if value.is_a? ::Date
18
19
 
19
20
  ::Date.parse(value)
20
21
  end
@@ -14,6 +14,7 @@ module Sinatra
14
14
 
15
15
  def coerce(value)
16
16
  return nil if value.nil?
17
+ return value if value.is_a? ::Integer
17
18
 
18
19
  Integer(value, 10)
19
20
  end
@@ -15,6 +15,7 @@ module Sinatra
15
15
 
16
16
  def coerce(value)
17
17
  return nil if value.nil?
18
+ return value if value.is_a? ::Time
18
19
 
19
20
  ::Time.parse(value)
20
21
  end
@@ -1,29 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'common'
4
+
3
5
  module Sinatra
4
6
  module ParamValidator
5
7
  class Rule
6
8
  # Rule to enforce all given params, or none of them
7
9
  class AllOrNoneOf
8
- attr_reader :errors
9
-
10
- def initialize(params, *fields, **_kwargs)
11
- @errors = []
12
- @params = params
13
- @fields = fields
14
-
15
- validate(fields)
16
- end
17
-
18
- def passes?
19
- @errors.empty?
20
- end
10
+ include Common
21
11
 
22
- def validate(fields)
23
- count = fields.count { |f| @params.key? f }
24
- return if count.zero? || count == fields.count
12
+ def validate
13
+ return if count.zero? || count == @fields.count
25
14
 
26
- @errors.push "All or none of [#{fields.join ', '}] must be provided"
15
+ @errors.push "All or none of [#{@fields.join ', '}] must be provided"
27
16
  end
28
17
  end
29
18
  end
@@ -1,27 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'common'
4
+
3
5
  module Sinatra
4
6
  module ParamValidator
5
7
  class Rule
6
8
  # Rule to enforce at least one of the given params exists
7
9
  class AnyOf
8
- attr_reader :errors
9
-
10
- def initialize(params, *fields, **_kwargs)
11
- @errors = []
12
- @params = params
13
- @fields = fields
14
-
15
- validate(fields)
16
- end
17
-
18
- def passes?
19
- @errors.empty?
20
- end
10
+ include Common
21
11
 
22
- def validate(fields)
23
- count = fields.count { |f| @params.key? f }
24
- @errors.push "One of [#{fields.join ', '}] must be provided" if count < 1
12
+ def validate
13
+ @errors.push "One of [#{@fields.join ', '}] must be provided" if count < 1
25
14
  end
26
15
  end
27
16
  end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sinatra
4
+ module ParamValidator
5
+ class Rule
6
+ # Common validation methods shared between rules
7
+ module Common
8
+ attr_reader :errors
9
+
10
+ def initialize(params, *fields, **_kwargs)
11
+ @errors = []
12
+ @params = params
13
+ @fields = fields
14
+
15
+ validate
16
+ end
17
+
18
+ def count
19
+ @count ||= (@fields & @params.keys.map(&:to_sym)).count
20
+ end
21
+
22
+ def passes?
23
+ @errors.empty?
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,28 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'common'
4
+
3
5
  module Sinatra
4
6
  module ParamValidator
5
7
  class Rule
6
8
  # Rule to enforce only one of the given params has been given
7
9
  class OneOf
8
- attr_reader :errors
9
-
10
- def initialize(params, *fields, **_kwargs)
11
- @errors = []
12
- @params = params
13
- @fields = fields
14
-
15
- validate(fields)
16
- end
17
-
18
- def passes?
19
- @errors.empty?
20
- end
10
+ include Common
21
11
 
22
- def validate(fields)
23
- count = fields.count { |f| @params.key? f }
24
- @errors.push "Only one of [#{fields.join ', '}] is allowed" if count > 1
25
- @errors.push "One of [#{fields.join ', '}] must be provided" if count < 1
12
+ def validate
13
+ @errors.push "Only one of [#{@fields.join ', '}] is allowed" if count > 1
14
+ @errors.push "One of [#{@fields.join ', '}] must be provided" if count < 1
26
15
  end
27
16
  end
28
17
  end
@@ -16,7 +16,11 @@ module Sinatra
16
16
  end
17
17
 
18
18
  def run(context, *args)
19
- @errors = Parser.new(context).parse(@definition, *args).errors
19
+ context.instance_variable_set(:@_validator_errors, {})
20
+ context.instance_exec(*args, &@definition)
21
+ @errors = context.remove_instance_variable(:@_validator_errors)
22
+ rescue InvalidParameterError => e
23
+ @errors[:general] = [e.message]
20
24
  end
21
25
 
22
26
  def success?
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sinatra
4
4
  module ParamValidator
5
- VERSION = '0.12.0'
5
+ VERSION = '0.15.0'
6
6
  end
7
7
  end
@@ -4,7 +4,9 @@ require_relative 'param_validator/camelize'
4
4
  require_relative 'param_validator/definitions'
5
5
  require_relative 'param_validator/helpers'
6
6
  require_relative 'param_validator/identifier'
7
- require_relative 'param_validator/parser'
7
+ require_relative 'param_validator/invalid_parameter_error'
8
+ require_relative 'param_validator/parameter'
9
+ require_relative 'param_validator/rule'
8
10
  require_relative 'param_validator/snake_case'
9
11
  require_relative 'param_validator/validator'
10
12
  require_relative 'param_validator/version'
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.12.0
4
+ version: 0.15.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-15 00:00:00.000000000 Z
11
+ date: 2022-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack-test
@@ -154,10 +154,10 @@ files:
154
154
  - lib/sinatra/param_validator/parameter/integer.rb
155
155
  - lib/sinatra/param_validator/parameter/string.rb
156
156
  - lib/sinatra/param_validator/parameter/time.rb
157
- - lib/sinatra/param_validator/parser.rb
158
157
  - lib/sinatra/param_validator/rule.rb
159
158
  - lib/sinatra/param_validator/rule/all_or_none_of.rb
160
159
  - lib/sinatra/param_validator/rule/any_of.rb
160
+ - lib/sinatra/param_validator/rule/common.rb
161
161
  - lib/sinatra/param_validator/rule/one_of.rb
162
162
  - lib/sinatra/param_validator/snake_case.rb
163
163
  - lib/sinatra/param_validator/validation_failed_error.rb
@@ -1,73 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'delegate'
4
-
5
- require_relative 'invalid_parameter_error'
6
- require_relative 'parameter'
7
- require_relative 'rule'
8
-
9
- module Sinatra
10
- module ParamValidator
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))
15
- end
16
-
17
- def param(key, type, default: nil, message: nil, **args, &block)
18
- parameter = Parameter.new(params[key], type, **args)
19
- _update_params_hash key, parameter, default
20
- if parameter.valid?
21
- _run_block(key, block) if block
22
- else
23
- _add_error key, message || parameter.errors
24
- end
25
- rescue NameError
26
- raise 'Invalid parameter type'
27
- end
28
-
29
- def rule(name, *args, **kwargs)
30
- rule = Rule.new(name, params, *args, **kwargs)
31
- unless rule.passes?
32
- @__validator_errors[:rules] ||= []
33
- @__validator_errors[:rules].push(rule.errors)
34
- end
35
- rescue NameError
36
- raise 'Invalid rule type'
37
- end
38
-
39
- def _run_block(key, block)
40
- args = block.arity == 1 ? [self] : []
41
- instance_exec(*args, &block)
42
- rescue InvalidParameterError => e
43
- _add_error key, e.message
44
- end
45
-
46
- def _update_params_hash(key, parameter, default)
47
- if params.key?(key)
48
- params[key] = parameter.coerced unless parameter.coerced.nil?
49
- elsif !default.nil?
50
- params[key] = default.respond_to?(:call) ? default.call : default
51
- end
52
- end
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
72
- end
73
- end