sinatra-param-validator 0.3.0 → 0.6.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: 54d6ed44b4046ecc9fd330aef4e719cbe10d4a953a8d07a80226822f59ed66d7
4
- data.tar.gz: e70164b07f23972aeffa920719c6de35baff4576eed46260944daf3a1b5e404b
3
+ metadata.gz: 2f824395ffa6b3e38d61c1e4cd81ee2237987c47b04c51abb4cd43d2fdedd2ec
4
+ data.tar.gz: 9294cc7618a05d263df6f7e7415fdab88b0f6e846b05fce4cac2763d8a79b740
5
5
  SHA512:
6
- metadata.gz: d4d54fc8f4d76d1499d9c15adec02e35ff2f7c36bf99f5606d15b60a579e45da1c0905624c12be5e4a01b645670a5f23603191cf87fd6be0fd3541c1f94f6afb
7
- data.tar.gz: c1c40e3b72dcb644adaf9437b0b7b67cfec8e33efad17ded176b74f3d6cf942fd76d3e4b7bf47f11442ebbb279d08907678e7600813b0b8da37c13736e0fdef4
6
+ metadata.gz: 17ed2207a700e74b5114ac4b10fb9f8a3f6089d6661df044017a5b36ea558ffec2452e62d7ec0814a5ce30536ab039569cfa45f031960e791caaf57ae253afac
7
+ data.tar.gz: 8dd242fdf3f379612962ca5897e23e86782b061b16373bfc3dbf4b6e3e22b95e39787027e1499cca36872c9d88b7246871bbb882f6472caadbd5004de7763be4
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.6.0] - 2022-06-09
4
+
5
+ - Add `block` to run blocks of code in the route context
6
+
7
+ ## [0.5.0] - 2022-06-09
8
+
9
+ - Allow the validator to be passed to the block for a valid parameter
10
+
11
+ ## [0.4.0] - 2022-06-09
12
+
13
+ - Allow custom error messages to be used when validation fails
14
+ - Ensure running multiple validations for a single parameter merges the errors correctly
15
+ - Allow validations to run code if successful
16
+ - Allow exceptions to be raised by the parameter block to indicate failure
17
+ - Allow parameters to be passed to validators
18
+
3
19
  ## [0.3.0] - 2022-06-08
4
20
 
5
21
  - Don't create entries in `params` for parameters that are not passed
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sinatra-param-validator (0.3.0)
4
+ sinatra-param-validator (0.6.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -86,6 +86,52 @@ 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
+
118
+ If you need to do further validation with your parameter, the validator can be passed to the block:
119
+
120
+ ```ruby
121
+ param :number, Integer, required: true do |validator|
122
+ validator.param :digit, Integer, min: params[:number]
123
+ end
124
+ ```
125
+
126
+ If you need to run some code in the route context, you can just use the `block` keyword:
127
+
128
+ ```ruby
129
+ block do |validator|
130
+ #...
131
+ validator.param :val, Integer
132
+ end
133
+ ```
134
+
89
135
  ## Rules
90
136
 
91
137
  Rules work on multiple parameters:
@@ -120,12 +166,26 @@ post '/new-user', validate_form: :new_user do
120
166
  # ...
121
167
  end
122
168
 
123
-
124
169
  get '/user/:id', validate_url_param: :user_id do
125
170
  # ...
126
171
  end
127
172
  ```
128
173
 
174
+ ## Validators with parameters
175
+
176
+ It is possible to define a validator with a parameter.
177
+ To call the validator, you can use the `vi` helper to wrap a validator identifier with arguments:
178
+
179
+ ```ruby
180
+ validator :number do |min|
181
+ param :id, Integer, min: min
182
+ end
183
+
184
+ post '/number', validate: vi(:new_user, 10) do
185
+ # ...
186
+ end
187
+ ```
188
+
129
189
  ## Development
130
190
 
131
191
  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, args = {})
17
- definition = settings.validator_definitions.get(identifier)
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
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sinatra
4
+ module ParamValidator
5
+ # Error raised when validation fails
6
+ class InvalidParameterError < StandardError
7
+ end
8
+ end
9
+ end
@@ -9,13 +9,17 @@ module Sinatra
9
9
 
10
10
  def initialize(value, **options)
11
11
  @errors = []
12
- @coerced = coerce value
13
12
  @options = options
14
13
 
14
+ begin
15
+ @coerced = coerce value
16
+ rescue ArgumentError
17
+ @errors.push "'#{value}' is not a valid #{self.class}"
18
+ return
19
+ end
20
+
15
21
  validate_options
16
22
  validate unless nil_and_ok?
17
- rescue ArgumentError
18
- @errors.push "'#{value}' is not a valid #{self.class}"
19
23
  end
20
24
 
21
25
  def valid?
@@ -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,30 @@ 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({}, &definition)
20
+ instance_exec(*args, &definition)
20
21
  end
21
22
 
22
- def param(key, type, **args)
23
+ def add_error(key, error)
24
+ @errors[key] = @errors.fetch(key, []).concat(Array(error))
25
+ end
26
+
27
+ def block(&block)
28
+ run_block :block, block
29
+ end
30
+
31
+ def param(key, type, message: nil, **args, &block)
23
32
  parameter = Parameter.new(@context.params[key], type, **args)
24
- @context.params[key] = parameter.coerced if @context.params.key? key
25
- @errors[key] = parameter.errors unless parameter.valid?
33
+ @context.params[key] = parameter.coerced if @context.params.key?(key) && parameter.coerced
34
+ if parameter.valid?
35
+ run_block(key, block) if block
36
+ else
37
+ add_error key, message || parameter.errors
38
+ end
26
39
  rescue NameError
27
40
  raise 'Invalid parameter type'
28
41
  end
@@ -36,6 +49,13 @@ module Sinatra
36
49
  rescue NameError
37
50
  raise 'Invalid rule type'
38
51
  end
52
+
53
+ def run_block(key, block)
54
+ args = block.arity == 1 ? [self] : []
55
+ @context.instance_exec(*args, &block)
56
+ rescue InvalidParameterError => e
57
+ add_error key, e.message
58
+ end
39
59
  end
40
60
  end
41
61
  end
@@ -6,9 +6,9 @@ module Sinatra
6
6
  class ValidationFailedError < StandardError
7
7
  attr_reader :errors
8
8
 
9
- def initialize(errors, msg = nil)
9
+ def initialize(errors)
10
10
  @errors = errors
11
- super(msg)
11
+ super("Validation failed: #{errors}")
12
12
  end
13
13
  end
14
14
  end
@@ -15,8 +15,8 @@ module Sinatra
15
15
  raise ValidationFailedError, @errors
16
16
  end
17
17
 
18
- def run(context)
19
- @errors = Parser.new(@definition, context).errors
18
+ def run(context, *args)
19
+ @errors = Parser.new(@definition, context, *args).errors
20
20
  end
21
21
 
22
22
  def success?
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Sinatra
4
4
  module ParamValidator
5
- VERSION = '0.3.0'
5
+ VERSION = '0.6.0'
6
6
  end
7
7
  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.3.0
4
+ version: 0.6.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-08 00:00:00.000000000 Z
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