sinatra-param-validator 0.13.0 → 0.16.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 +16 -1
- data/Gemfile.lock +1 -1
- 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 +3 -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: c6ca19a9842af1724e306e23e2f35e7c68a7a3f51033a511ab0a373804782d02
|
4
|
+
data.tar.gz: c7d4e25ed4955248102c235bcf34b6cb8372b34315d0fc79d2e5cffb7588efa8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e39d7290c4df32d5eb7a93c7c7098e129b3c7e818de68c6e78af95c0a7ca3c15bcce15d447751b1c87962ec53fc7a9f4d002e3bea07764fc5e03bb2e5e0cbf34
|
7
|
+
data.tar.gz: dba6a7b0a516f7bb182628c57e8e88c2385ffa630552b81fb443cb0cb20e7e3c1f3866e6dfe802bdfc91b8d24be965c868ce8a5eb712ff37459fba1892562b8b
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,21 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
-
## [0.
|
3
|
+
## [0.16.0] - 2022-07-19
|
4
|
+
|
5
|
+
- Set default value for nil parameters
|
6
|
+
|
7
|
+
## [0.15.0] - 2022-07-19
|
8
|
+
|
9
|
+
- Pass message to raised exception
|
10
|
+
- Allow rules to run blocks
|
11
|
+
|
12
|
+
## [0.14.0] - 2022-07-18
|
13
|
+
|
14
|
+
- Fix parameter coercion if a parameter is validated multiple times
|
15
|
+
- Refactor `param` and `rule` functions into helpers
|
16
|
+
- Allow `param` and `rule` to be run independently
|
17
|
+
|
18
|
+
## [0.13.0] - 2022-07-18
|
4
19
|
|
5
20
|
- Capture `InvalidParameterError`s raised when running the parser
|
6
21
|
|
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) && !params[key].nil?
|
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,9 @@ 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)
|
20
22
|
rescue InvalidParameterError => e
|
21
23
|
@errors[:general] = [e.message]
|
22
24
|
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/
|
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.16.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
|