sinatra-param-validator 0.6.0 → 0.9.0

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: 2f824395ffa6b3e38d61c1e4cd81ee2237987c47b04c51abb4cd43d2fdedd2ec
4
- data.tar.gz: 9294cc7618a05d263df6f7e7415fdab88b0f6e846b05fce4cac2763d8a79b740
3
+ metadata.gz: f17b821ad13e5656480453ed3067bc2cb724cfc51dc71b89673ebbd29bf06cff
4
+ data.tar.gz: b4a333cc414c4fdb0ba9ba9cbc5da317037bbc0a08879f8f120c70996ce41073
5
5
  SHA512:
6
- metadata.gz: 17ed2207a700e74b5114ac4b10fb9f8a3f6089d6661df044017a5b36ea558ffec2452e62d7ec0814a5ce30536ab039569cfa45f031960e791caaf57ae253afac
7
- data.tar.gz: 8dd242fdf3f379612962ca5897e23e86782b061b16373bfc3dbf4b6e3e22b95e39787027e1499cca36872c9d88b7246871bbb882f6472caadbd5004de7763be4
6
+ metadata.gz: fd06efb152939cecc5ea1169426c4cded4177ad2a71b678a217899e246c4def5826457339b8a9f5a2afedc3044af53e4a41728f04ae6180cfdfc2efc5048e6f6
7
+ data.tar.gz: 90133f0e8bda179eafc1e193bcb4caa2466ece28efc20c17e3521e30a18a505d58d26f0ba6e0ff79fb88119bdcbeb9f2b4932ca7b6882e98c351eed0d1509a0d
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.9.0] - 2022-07-09
4
+
5
+ - Change parser to parse the definition outside of the initialize block
6
+
7
+ ## [0.8.0] - 2022-07-09
8
+
9
+ - Add form helpers for form validator
10
+
11
+ ## [0.7.0] - 2022-06-09
12
+
13
+ - Add `default` option for parameters
14
+ - Allow `default` option to be a proc or a lambda
15
+
3
16
  ## [0.6.0] - 2022-06-09
4
17
 
5
18
  - Add `block` to run blocks of code in the route context
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sinatra-param-validator (0.6.0)
4
+ sinatra-param-validator (0.9.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -85,4 +85,4 @@ DEPENDENCIES
85
85
  sinatra-param-validator!
86
86
 
87
87
  BUNDLED WITH
88
- 2.3.13
88
+ 2.3.17
data/README.md CHANGED
@@ -69,6 +69,9 @@ param :number, Integer, required: true, in: 0..100
69
69
 
70
70
  All parameters have the following validations available:
71
71
 
72
+ * `default`
73
+ * Set a default value if this parameter was not provided
74
+ * This can be a lambda or a proc if required
72
75
  * `nillable`
73
76
  * If this is set, all other validations are skipped if the value is nil
74
77
  * `required`
@@ -12,12 +12,16 @@ module Sinatra
12
12
  class Parser < SimpleDelegator
13
13
  attr_reader :errors
14
14
 
15
- def initialize(definition, context, *args)
15
+ def initialize(context)
16
16
  super(context)
17
17
  @context = context
18
18
  @errors = {}
19
+ end
19
20
 
21
+ def parse(definition, *args)
20
22
  instance_exec(*args, &definition)
23
+
24
+ self
21
25
  end
22
26
 
23
27
  def add_error(key, error)
@@ -28,9 +32,9 @@ module Sinatra
28
32
  run_block :block, block
29
33
  end
30
34
 
31
- def param(key, type, message: nil, **args, &block)
35
+ def param(key, type, default: nil, message: nil, **args, &block)
32
36
  parameter = Parameter.new(@context.params[key], type, **args)
33
- @context.params[key] = parameter.coerced if @context.params.key?(key) && parameter.coerced
37
+ update_params_hash key, parameter, default
34
38
  if parameter.valid?
35
39
  run_block(key, block) if block
36
40
  else
@@ -50,12 +54,22 @@ module Sinatra
50
54
  raise 'Invalid rule type'
51
55
  end
52
56
 
57
+ private
58
+
53
59
  def run_block(key, block)
54
60
  args = block.arity == 1 ? [self] : []
55
61
  @context.instance_exec(*args, &block)
56
62
  rescue InvalidParameterError => e
57
63
  add_error key, e.message
58
64
  end
65
+
66
+ def update_params_hash(key, parameter, default)
67
+ if @context.params.key?(key)
68
+ @context.params[key] = parameter.coerced if parameter.coerced
69
+ elsif !default.nil?
70
+ @context.params[key] = default.respond_to?(:call) ? default.call : default
71
+ end
72
+ end
59
73
  end
60
74
  end
61
75
  end
@@ -5,6 +5,33 @@ module Sinatra
5
5
  class Validator
6
6
  # A form validator
7
7
  class Form < Validator
8
+ # Helpers for Sinatra templates
9
+ module Helpers
10
+ def form_values(hash)
11
+ hash = IndifferentHash[hash]
12
+ flash.now[:params] = flash.now.key?(:params) ? hash.merge(flash.now[:params]) : hash
13
+ end
14
+
15
+ def form_value(field)
16
+ flash[:params]&.fetch(field, nil)
17
+ end
18
+
19
+ def form_error?(field = nil)
20
+ return !flash[:form_errors].nil? && !flash[:form_errors]&.empty? if field.nil?
21
+
22
+ (flash[:form_errors] || {}).key?(field)
23
+ end
24
+
25
+ def form_errors(field)
26
+ (flash[:form_errors] || {}).fetch(field, [])
27
+ end
28
+
29
+ def invalid_feedback(field, default = nil)
30
+ fields = Array(field)
31
+ fields.any? { |f| form_error? f } ? fields.map { |f| form_errors f }.flatten.join('<br />') : default
32
+ end
33
+ end
34
+
8
35
  def handle_failure(context)
9
36
  case context.request.preferred_type.to_s
10
37
  when 'application/json' then return json_failure(context)
@@ -16,7 +16,7 @@ module Sinatra
16
16
  end
17
17
 
18
18
  def run(context, *args)
19
- @errors = Parser.new(@definition, context, *args).errors
19
+ @errors = Parser.new(context).parse(@definition, *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.6.0'
5
+ VERSION = '0.9.0'
6
6
  end
7
7
  end
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.6.0
4
+ version: 0.9.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-09 00:00:00.000000000 Z
11
+ date: 2022-07-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack-test