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 +4 -4
- data/CHANGELOG.md +15 -0
- data/Gemfile.lock +2 -2
- data/lib/sinatra/param_validator/helpers.rb +44 -0
- data/lib/sinatra/param_validator/parameter/date.rb +1 -0
- data/lib/sinatra/param_validator/parameter/integer.rb +1 -0
- data/lib/sinatra/param_validator/parameter/time.rb +1 -0
- data/lib/sinatra/param_validator/rule/all_or_none_of.rb +6 -17
- data/lib/sinatra/param_validator/rule/any_of.rb +5 -16
- data/lib/sinatra/param_validator/rule/common.rb +28 -0
- data/lib/sinatra/param_validator/rule/one_of.rb +6 -17
- data/lib/sinatra/param_validator/validator.rb +5 -1
- data/lib/sinatra/param_validator/version.rb +1 -1
- data/lib/sinatra/param_validator.rb +3 -1
- metadata +3 -3
- data/lib/sinatra/param_validator/parser.rb +0 -73
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0635a378cefa1851f127f94720dbcdb8a6abe12093af7fcdafcccb5066c3a10d
|
4
|
+
data.tar.gz: 2a8fe28e566133d4e72aa0188824f184a02984475dc1f79796d3d7a98e914957
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
@@ -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
|
@@ -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
|
-
|
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
|
23
|
-
count
|
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
|
-
|
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
|
23
|
-
|
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
|
-
|
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
|
23
|
-
|
24
|
-
@errors.push "
|
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
|
-
|
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?
|
@@ -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/
|
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.
|
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-
|
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
|