sinatra-param-validator 0.3.0 → 0.4.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 +8 -0
- data/Gemfile.lock +1 -1
- data/README.md +46 -0
- data/lib/sinatra/param_validator/helpers.rb +4 -3
- data/lib/sinatra/param_validator/identifier.rb +15 -0
- data/lib/sinatra/param_validator/invalid_parameter_error.rb +9 -0
- data/lib/sinatra/param_validator/parser.rb +20 -5
- data/lib/sinatra/param_validator/validation_failed_error.rb +2 -2
- data/lib/sinatra/param_validator/validator.rb +2 -2
- data/lib/sinatra/param_validator/version.rb +1 -1
- data/lib/sinatra/param_validator.rb +5 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f474d7b6ff2e2ed3de6f58e3bd0ab0b328cc11886ac36701c5a1557a8713e5be
|
4
|
+
data.tar.gz: 378f0b0bb00df8adca7f951175133833d68ce457066f79423aadb52cab1ba6fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 045a26ac9992b92e1dab3c63527b6ebfd2b70c84b554d20a7f38abfa070f0402da47776d663619c0ae1ff95c681a52ccfb7da34fa2f5c10928c28626c77f75a9
|
7
|
+
data.tar.gz: cefd05d4f4babca139755e05d891802e375732bc944d278a8313e14759e87cd58b300c567e236ccf844897233072ce37e79d9c70f27385babd8b35435008a5c3
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,13 @@
|
|
1
1
|
## [Unreleased]
|
2
2
|
|
3
|
+
## [0.4.0] - 2022-06-09
|
4
|
+
|
5
|
+
- Allow custom error messages to be used when validation fails
|
6
|
+
- Ensure running multiple validations for a single parameter merges the errors correctly
|
7
|
+
- Allow validations to run code if successful
|
8
|
+
- Allow exceptions to be raised by the parameter block to indicate failure
|
9
|
+
- Allow parameters to be passed to validators
|
10
|
+
|
3
11
|
## [0.3.0] - 2022-06-08
|
4
12
|
|
5
13
|
- Don't create entries in `params` for parameters that are not passed
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -86,6 +86,35 @@ All parameters have the following validations available:
|
|
86
86
|
|
87
87
|
* `min` / `max`
|
88
88
|
|
89
|
+
## Custom Messages
|
90
|
+
|
91
|
+
It is possible to return a custom error message when a validation fails:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
param :number, Integer, required: true, message: 'The number is required'
|
95
|
+
```
|
96
|
+
|
97
|
+
It is also possible to run multiple validations against a single parameter.
|
98
|
+
This can be useful if different failures require different messages.
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
param :number, Integer, required: true, message: 'The number is required'
|
102
|
+
param :number, Integer, min: 100, message: 'The number is not large enough'
|
103
|
+
```
|
104
|
+
|
105
|
+
## Validation blocks
|
106
|
+
|
107
|
+
It is possible to run code after a validation succeeds, by passing a block to `param`:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
param :number, Integer, required: true do
|
111
|
+
# ...
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
If you wish to indicate a validation failure within a block, raise `Sinatra::ParameterValidator::InvalidParameterError`
|
116
|
+
with a message, and it will be passed through as an error for the parameter.
|
117
|
+
|
89
118
|
## Rules
|
90
119
|
|
91
120
|
Rules work on multiple parameters:
|
@@ -126,6 +155,23 @@ get '/user/:id', validate_url_param: :user_id do
|
|
126
155
|
end
|
127
156
|
```
|
128
157
|
|
158
|
+
## Validators with parameters
|
159
|
+
|
160
|
+
It is possible to define a validator with a parameter.
|
161
|
+
To call the validator, you can use the `vi` helper to wrap a validator identifier with arguments:
|
162
|
+
|
163
|
+
```ruby
|
164
|
+
validator :number do |min|
|
165
|
+
param :id, Integer, min: min
|
166
|
+
end
|
167
|
+
|
168
|
+
post '/number', validate: vi(:new_user, 10) do
|
169
|
+
# ...
|
170
|
+
end
|
171
|
+
```
|
172
|
+
|
173
|
+
|
174
|
+
|
129
175
|
## Development
|
130
176
|
|
131
177
|
After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests.
|
@@ -13,10 +13,11 @@ module Sinatra
|
|
13
13
|
raise "Filter params failed: #{e}"
|
14
14
|
end
|
15
15
|
|
16
|
-
def validate(klass, identifier
|
17
|
-
|
16
|
+
def validate(klass, identifier)
|
17
|
+
identifier = Identifier.new(identifier) if identifier.is_a? Symbol
|
18
|
+
definition = settings.validator_definitions.get(identifier.identifier)
|
18
19
|
validator = klass.new(&definition)
|
19
|
-
validator.run(self, *args)
|
20
|
+
validator.run(self, *identifier.args)
|
20
21
|
validator.handle_failure(self) unless validator.success?
|
21
22
|
end
|
22
23
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
module ParamValidator
|
5
|
+
# Class to hold a validator identifier plus arguments
|
6
|
+
class Identifier
|
7
|
+
attr_reader :identifier, :args
|
8
|
+
|
9
|
+
def initialize(identifier, *args)
|
10
|
+
@identifier = identifier
|
11
|
+
@args = args
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'delegate'
|
4
4
|
|
5
|
+
require_relative 'invalid_parameter_error'
|
5
6
|
require_relative 'parameter'
|
6
7
|
require_relative 'rule'
|
7
8
|
|
@@ -11,18 +12,26 @@ module Sinatra
|
|
11
12
|
class Parser < SimpleDelegator
|
12
13
|
attr_reader :errors
|
13
14
|
|
14
|
-
def initialize(definition, context)
|
15
|
+
def initialize(definition, context, *args)
|
15
16
|
super(context)
|
16
17
|
@context = context
|
17
18
|
@errors = {}
|
18
19
|
|
19
|
-
instance_exec(
|
20
|
+
instance_exec(*args, &definition)
|
20
21
|
end
|
21
22
|
|
22
|
-
def
|
23
|
+
def add_error(key, error)
|
24
|
+
@errors[key] = @errors.fetch(key, []).concat(Array(error))
|
25
|
+
end
|
26
|
+
|
27
|
+
def param(key, type, message: nil, **args, &block)
|
23
28
|
parameter = Parameter.new(@context.params[key], type, **args)
|
24
|
-
@context.params[key] = parameter.coerced if @context.params.key?
|
25
|
-
|
29
|
+
@context.params[key] = parameter.coerced if @context.params.key?(key) && parameter.coerced
|
30
|
+
if parameter.valid?
|
31
|
+
run_block(key, block) if block
|
32
|
+
else
|
33
|
+
add_error key, message || parameter.errors
|
34
|
+
end
|
26
35
|
rescue NameError
|
27
36
|
raise 'Invalid parameter type'
|
28
37
|
end
|
@@ -36,6 +45,12 @@ module Sinatra
|
|
36
45
|
rescue NameError
|
37
46
|
raise 'Invalid rule type'
|
38
47
|
end
|
48
|
+
|
49
|
+
def run_block(key, block)
|
50
|
+
@context.instance_exec(&block)
|
51
|
+
rescue InvalidParameterError => e
|
52
|
+
add_error key, e.message
|
53
|
+
end
|
39
54
|
end
|
40
55
|
end
|
41
56
|
end
|
@@ -3,6 +3,7 @@
|
|
3
3
|
require_relative 'param_validator/camelize'
|
4
4
|
require_relative 'param_validator/definitions'
|
5
5
|
require_relative 'param_validator/helpers'
|
6
|
+
require_relative 'param_validator/identifier'
|
6
7
|
require_relative 'param_validator/parser'
|
7
8
|
require_relative 'param_validator/snake_case'
|
8
9
|
require_relative 'param_validator/validator'
|
@@ -17,6 +18,10 @@ module Sinatra
|
|
17
18
|
settings.validator_definitions.add(identifier, definition)
|
18
19
|
end
|
19
20
|
|
21
|
+
def vi(identifier, *args)
|
22
|
+
Identifier.new(identifier, *args)
|
23
|
+
end
|
24
|
+
|
20
25
|
class << self
|
21
26
|
include SnakeCase
|
22
27
|
|
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.4.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-06-
|
11
|
+
date: 2022-06-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack-test
|
@@ -142,6 +142,8 @@ files:
|
|
142
142
|
- lib/sinatra/param_validator/camelize.rb
|
143
143
|
- lib/sinatra/param_validator/definitions.rb
|
144
144
|
- lib/sinatra/param_validator/helpers.rb
|
145
|
+
- lib/sinatra/param_validator/identifier.rb
|
146
|
+
- lib/sinatra/param_validator/invalid_parameter_error.rb
|
145
147
|
- lib/sinatra/param_validator/parameter.rb
|
146
148
|
- lib/sinatra/param_validator/parameter/array.rb
|
147
149
|
- lib/sinatra/param_validator/parameter/boolean.rb
|