sinatra-param-validator 0.14.0 → 0.16.1

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: 2f94e83b8a50e45743393eff54f46c692095fbad50942f68e047713b55397512
4
- data.tar.gz: 1394c62c964ad2e4bc51dbd44abf98313757babbbe13748c09f32dc2a2e8863d
3
+ metadata.gz: 78acd25fda0c630a67c5ce3e2b24a9300c70432b40621a1ebd2822e4d63a549b
4
+ data.tar.gz: 6ad51ab220feb9b3ec422532d1ae46bc2b812ec9969eec4e3b7a5b6b0387f4a6
5
5
  SHA512:
6
- metadata.gz: '09cd666c97ce8ffd0750361ca52cfa04b2687351bd4db39918ba6d393ce476917943a16311817cddd50507939978687a4820d8aeb2fbda130dfd64d01a12ac72'
7
- data.tar.gz: 2589470cf72e955854bc68ade46b6ccb1ddf2a5e9d1a7ad142e3fcfa5a2f714aa547b850090ee26caec29d33bf3d8c5b476809cb6ef392e3dad20222588bd6b9
6
+ metadata.gz: 224a086b11dc650444326980951756850ae049e4f09b17dac6ae73881327ae694ca2c38f3855887300ebeeed6a9fc36d61651d61884cbe237f057d964858e6e2
7
+ data.tar.gz: 1b80d184a2e078f22a265cfcc3d38aa083f4cb6ad0e0850d193b5c39ef2bbe190ea87c7edbd628c9b3121472b7704367d58e9e9a8c4b357338daf14c0e70d8cf
data/CHANGELOG.md CHANGED
@@ -1,12 +1,25 @@
1
1
  ## [Unreleased]
2
2
 
3
- ## [0.14.0] - 2022-07-15
3
+ ## [0.16.1] - 2022-07-19
4
+
5
+ - Fix URIs in gemspec
6
+
7
+ ## [0.16.0] - 2022-07-19
8
+
9
+ - Set default value for nil parameters
10
+
11
+ ## [0.15.0] - 2022-07-19
12
+
13
+ - Pass message to raised exception
14
+ - Allow rules to run blocks
15
+
16
+ ## [0.14.0] - 2022-07-18
4
17
 
5
18
  - Fix parameter coercion if a parameter is validated multiple times
6
19
  - Refactor `param` and `rule` functions into helpers
7
20
  - Allow `param` and `rule` to be run independently
8
21
 
9
- ## [0.13.0] - 2022-07-15
22
+ ## [0.13.0] - 2022-07-18
10
23
 
11
24
  - Capture `InvalidParameterError`s raised when running the parser
12
25
 
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.16.1)
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
@@ -54,12 +58,20 @@ module Sinatra
54
58
  end
55
59
 
56
60
  def _update_params_hash(key, parameter, default)
57
- if params.key?(key)
58
- params[key] = parameter.coerced unless parameter.coerced.nil?
61
+ if params.key?(key) && !params[key].nil?
62
+ _set_param_to_coerced key, parameter
59
63
  elsif !default.nil?
60
- params[key] = default.respond_to?(:call) ? default.call : default
64
+ _set_param_to_default key, default
61
65
  end
62
66
  end
67
+
68
+ def _set_param_to_coerced(key, parameter)
69
+ params[key] = parameter.coerced unless parameter.coerced.nil?
70
+ end
71
+
72
+ def _set_param_to_default(key, default)
73
+ params[key] = default.respond_to?(:call) ? default.call : default
74
+ end
63
75
  end
64
76
  end
65
77
  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.16.1'
6
6
  end
7
7
  end
@@ -9,13 +9,13 @@ Gem::Specification.new do |spec|
9
9
  spec.email = ['rick@selby-family.co.uk']
10
10
 
11
11
  spec.summary = 'Validation of parameters for Sinatra'
12
- spec.homepage = 'https://github.com/rickselby/sinatra-param_validator-validator'
12
+ spec.homepage = 'https://github.com/rickselby/sinatra-param-validator'
13
13
  spec.license = 'MIT'
14
14
  spec.required_ruby_version = '>= 2.6.0'
15
15
 
16
- spec.metadata['changelog_uri'] = 'https://github.com/rickselby/sinatra-param_validator-validator/CHANGELOG.md'
16
+ spec.metadata['changelog_uri'] = 'https://github.com/rickselby/sinatra-para-validator/CHANGELOG.md'
17
17
  spec.metadata['homepage_uri'] = spec.homepage
18
- spec.metadata['source_code_uri'] = 'https://github.com/rickselby/sinatra-param_validator-validator'
18
+ spec.metadata['source_code_uri'] = 'https://github.com/rickselby/sinatra-param-validator'
19
19
  spec.metadata['rubygems_mfa_required'] = 'true'
20
20
 
21
21
  # Specify which files should be added to the gem when it is released.
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.16.1
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
@@ -165,13 +166,13 @@ files:
165
166
  - lib/sinatra/param_validator/validator/url_param.rb
166
167
  - lib/sinatra/param_validator/version.rb
167
168
  - sinatra-param-validator.gemspec
168
- homepage: https://github.com/rickselby/sinatra-param_validator-validator
169
+ homepage: https://github.com/rickselby/sinatra-param-validator
169
170
  licenses:
170
171
  - MIT
171
172
  metadata:
172
- changelog_uri: https://github.com/rickselby/sinatra-param_validator-validator/CHANGELOG.md
173
- homepage_uri: https://github.com/rickselby/sinatra-param_validator-validator
174
- source_code_uri: https://github.com/rickselby/sinatra-param_validator-validator
173
+ changelog_uri: https://github.com/rickselby/sinatra-para-validator/CHANGELOG.md
174
+ homepage_uri: https://github.com/rickselby/sinatra-param-validator
175
+ source_code_uri: https://github.com/rickselby/sinatra-param-validator
175
176
  rubygems_mfa_required: 'true'
176
177
  post_install_message:
177
178
  rdoc_options: []