sinatra-param-validator 0.14.0 → 0.15.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2f94e83b8a50e45743393eff54f46c692095fbad50942f68e047713b55397512
4
- data.tar.gz: 1394c62c964ad2e4bc51dbd44abf98313757babbbe13748c09f32dc2a2e8863d
3
+ metadata.gz: 0635a378cefa1851f127f94720dbcdb8a6abe12093af7fcdafcccb5066c3a10d
4
+ data.tar.gz: 2a8fe28e566133d4e72aa0188824f184a02984475dc1f79796d3d7a98e914957
5
5
  SHA512:
6
- metadata.gz: '09cd666c97ce8ffd0750361ca52cfa04b2687351bd4db39918ba6d393ce476917943a16311817cddd50507939978687a4820d8aeb2fbda130dfd64d01a12ac72'
7
- data.tar.gz: 2589470cf72e955854bc68ade46b6ccb1ddf2a5e9d1a7ad142e3fcfa5a2f714aa547b850090ee26caec29d33bf3d8c5b476809cb6ef392e3dad20222588bd6b9
6
+ metadata.gz: 4ff032b54cf35d989b81613ee6b965a40a8b18699d183fdeef32c08a5b0d8a520415ec43a270c6b84d288446bae8813fd17a8eb9f1d5c77fcee3ad8b8eb5bb63
7
+ data.tar.gz: adc4c502ff12a8730598c28c13871ecd52e4c8749bbe4c5ea7b9256727cd3997b9b31152e4d22c3e908ae39eca8c40b236a65ebf8c7e5ff1dfa41fd75b632b7d
data/CHANGELOG.md CHANGED
@@ -1,12 +1,17 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.14.0] - 2022-07-15
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
4
9
 
5
10
  - Fix parameter coercion if a parameter is validated multiple times
6
11
  - Refactor `param` and `rule` functions into helpers
7
12
  - Allow `param` and `rule` to be run independently
8
13
 
9
- ## [0.13.0] - 2022-07-15
14
+ ## [0.13.0] - 2022-07-18
10
15
 
11
16
  - Capture `InvalidParameterError`s raised when running the parser
12
17
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sinatra-param-validator (0.14.0)
4
+ sinatra-param-validator (0.15.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -25,9 +25,13 @@ module Sinatra
25
25
  raise 'Invalid parameter type'
26
26
  end
27
27
 
28
- def rule(name, *args, **kwargs)
28
+ def rule(name, *args, **kwargs, &block)
29
29
  rule = Rule.new(name, params, *args, **kwargs)
30
- _handle_error :rules, rule.errors unless rule.passes?
30
+ if rule.passes?
31
+ _run_block(:rules, block) if block
32
+ else
33
+ _handle_error :rules, rule.errors unless rule.passes?
34
+ end
31
35
  rescue NameError
32
36
  raise 'Invalid rule type'
33
37
  end
@@ -41,7 +45,7 @@ module Sinatra
41
45
  end
42
46
 
43
47
  def _handle_error(key, error)
44
- raise InvalidParameterError unless defined? @_validator_errors
48
+ raise InvalidParameterError, error unless defined? @_validator_errors
45
49
 
46
50
  @_validator_errors[key] = @_validator_errors.fetch(key, []).concat(Array(error))
47
51
  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
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sinatra
4
4
  module ParamValidator
5
- VERSION = '0.14.0'
5
+ VERSION = '0.15.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.14.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-18 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
@@ -157,6 +157,7 @@ files:
157
157
  - lib/sinatra/param_validator/rule.rb
158
158
  - lib/sinatra/param_validator/rule/all_or_none_of.rb
159
159
  - lib/sinatra/param_validator/rule/any_of.rb
160
+ - lib/sinatra/param_validator/rule/common.rb
160
161
  - lib/sinatra/param_validator/rule/one_of.rb
161
162
  - lib/sinatra/param_validator/snake_case.rb
162
163
  - lib/sinatra/param_validator/validation_failed_error.rb