rails_param 0.10.0 → 0.11.2

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
- SHA1:
3
- metadata.gz: 8cbf1f2bda303ead2066ddf49597a4af8b276ced
4
- data.tar.gz: c403d8ad4dafd5a1e014e8e8a0b10417e69badcc
2
+ SHA256:
3
+ metadata.gz: '08178a96aa23b8bdd933adeed68df920615f3dfd23c3419be3e5a3862daf9110'
4
+ data.tar.gz: 8440cb471e4bde464eb3587031c785fff70a1c0072a219d284bb9a35d75359f1
5
5
  SHA512:
6
- metadata.gz: 9c509598a1dda1c1fe0a1276fe957c3259862ac9430b3b6302926ab4aecf26636eb6de0d07f3901e8f7ca6aa8ab1c3b1ea61c75af341a4a495bf6c6554b63505
7
- data.tar.gz: 7b2c56eff184db82d4497a46b203155c077bfb625178de27b6b38a5a8bc9947a088d29468d889d0ec0fccbeda28ebdfc853f73b4f4023d7170a0eb34dba68e86
6
+ metadata.gz: eac1db59508ef0c4bf89a59b95ba8685f47c2d55529ce4253581540f7ac0398921651187a203e0c44f9b5416eebf07914a9756918737e344ff499cf728287707
7
+ data.tar.gz: b91642011d2d51e82c997cecd2cf8e4d9d72ca01cd1ed0b13d6cfa1e3df5f3acd611325984d516389df85169a2f9bea4ffe9c897b3b106d472c3fe027af25514
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
- # rails-param
1
+ # rails_param
2
2
  _Parameter Validation & Type Coercion for Rails_
3
3
 
4
+ [![Gem Version](https://badge.fury.io/rb/rails_param.svg)](https://rubygems.org/gems/rails_param)
4
5
  [![Build Status](https://travis-ci.org/nicolasblanco/rails_param.svg?branch=master)](https://travis-ci.org/nicolasblanco/rails_param)
5
6
 
6
7
  ## Introduction
@@ -74,8 +75,15 @@ You may use the [rescue_from](http://api.rubyonrails.org/classes/ActiveSupport/R
74
75
  - `is`
75
76
  - `in`, `within`, `range`
76
77
  - `min` / `max`
78
+ - `min_length` / `max_length`
77
79
  - `format`
78
80
 
81
+ Customize exception message with option `:message`
82
+
83
+ ```ruby
84
+ param! :q, String, required: true, message: "Query not specified"
85
+ ```
86
+
79
87
  ### Defaults and Transformations
80
88
 
81
89
  Passing a `default` option will provide a default value for a parameter if none is passed. A `default` can defined as either a default or as a `Proc`:
@@ -147,4 +155,4 @@ Nicolas Blanco
147
155
 
148
156
  ## License
149
157
 
150
- rails-param is available under the MIT license. See the LICENSE file for more info.
158
+ rails_param is available under the MIT license. See the LICENSE file for more info.
@@ -2,9 +2,16 @@ module RailsParam
2
2
  module Param
3
3
 
4
4
  DEFAULT_PRECISION = 14
5
+ TIME_TYPES = [Date, DateTime, Time].freeze
6
+ STRING_OR_TIME_TYPES = ([String] + TIME_TYPES).freeze
5
7
 
6
8
  class InvalidParameterError < StandardError
7
9
  attr_accessor :param, :options
10
+
11
+ def message
12
+ return options[:message] if options.is_a?(Hash) && options.key?(:message)
13
+ super
14
+ end
8
15
  end
9
16
 
10
17
  class MockController
@@ -21,14 +28,17 @@ module RailsParam
21
28
  params[name] = coerce(params[name], type, options)
22
29
 
23
30
  # set default
24
- if options[:default].respond_to?(:call)
25
- params[name] = options[:default].call
26
- elsif params[name].nil? && check_param_presence?(options[:default])
27
- params[name] = options[:default]
31
+ if params[name].nil? && check_param_presence?(options[:default])
32
+ params[name] = options[:default].respond_to?(:call) ? options[:default].call : options[:default]
28
33
  end
29
34
 
30
- # apply tranformation
31
- if params[name] && options[:transform]
35
+ # validate presence
36
+ if params[name].nil? && options[:required]
37
+ raise InvalidParameterError, "Parameter #{name} is required"
38
+ end
39
+
40
+ # apply transformation
41
+ if params.include?(name) && options[:transform]
32
42
  params[name] = options[:transform].to_proc.call(params[name])
33
43
  end
34
44
 
@@ -92,22 +102,35 @@ module RailsParam
92
102
  begin
93
103
  return nil if param.nil?
94
104
  return param if (param.is_a?(type) rescue false)
105
+ if (param.is_a?(Array) && type != Array) || ((param.is_a?(Hash) || param.is_a?(ActionController::Parameters)) && type != Hash)
106
+ raise ArgumentError
107
+ end
95
108
  return param if (param.is_a?(ActionController::Parameters) && type == Hash rescue false)
96
109
  return Integer(param) if type == Integer
97
110
  return Float(param) if type == Float
98
111
  return String(param) if type == String
99
- return Date.parse(param) if type == Date
100
- return Time.parse(param) if type == Time
101
- return DateTime.parse(param) if type == DateTime
112
+ if TIME_TYPES.include? type
113
+ if options[:format].present?
114
+ return type.strptime(param, options[:format])
115
+ else
116
+ return type.parse(param)
117
+ end
118
+ end
119
+ raise ArgumentError if (type == Array || type == Hash) && !param.respond_to?(:split)
102
120
  return Array(param.split(options[:delimiter] || ",")) if type == Array
103
121
  return Hash[param.split(options[:delimiter] || ",").map { |c| c.split(options[:separator] || ":") }] if type == Hash
104
- return (/^(false|f|no|n|0)$/i === param.to_s ? false : (/^(true|t|yes|y|1)$/i === param.to_s ? true : (raise ArgumentError))) if type == TrueClass || type == FalseClass || type == :boolean
122
+ if type == TrueClass || type == FalseClass || type == :boolean
123
+ return false if /^(false|f|no|n|0)$/i === param.to_s
124
+ return true if /^(true|t|yes|y|1)$/i === param.to_s
125
+
126
+ raise ArgumentError
127
+ end
105
128
  if type == BigDecimal
106
129
  param = param.delete('$,').strip.to_f if param.is_a?(String)
107
- return BigDecimal.new(param, (options[:precision] || DEFAULT_PRECISION))
130
+ return BigDecimal(param, (options[:precision] || DEFAULT_PRECISION))
108
131
  end
109
132
  return nil
110
- rescue ArgumentError
133
+ rescue ArgumentError, TypeError
111
134
  raise InvalidParameterError, "'#{param}' is not a valid #{type}"
112
135
  end
113
136
  end
@@ -115,20 +138,18 @@ module RailsParam
115
138
  def validate!(param, param_name, options)
116
139
  options.each do |key, value|
117
140
  case key
118
- when :required
119
- raise InvalidParameterError, "Parameter #{param_name} is required" if value && param.nil?
120
141
  when :blank
121
142
  raise InvalidParameterError, "Parameter #{param_name} cannot be blank" if !value && case param
122
143
  when String
123
144
  !(/\S/ === param)
124
- when Array, Hash
145
+ when Array, Hash, ActionController::Parameters
125
146
  param.empty?
126
147
  else
127
148
  param.nil?
128
149
  end
129
150
  when :format
130
- raise InvalidParameterError, "Parameter #{param_name} must be a string if using the format validation" unless param.kind_of?(String)
131
- raise InvalidParameterError, "Parameter #{param_name} must match format #{value}" unless param =~ value
151
+ raise InvalidParameterError, "Parameter #{param_name} must be a string if using the format validation" unless STRING_OR_TIME_TYPES.any? { |cls| param.kind_of? cls }
152
+ raise InvalidParameterError, "Parameter #{param_name} must match format #{value}" if param.kind_of?(String) && param !~ value
132
153
  when :is
133
154
  raise InvalidParameterError, "Parameter #{param_name} must be #{value}" unless param === value
134
155
  when :in, :within, :range
@@ -146,6 +167,8 @@ module RailsParam
146
167
  raise InvalidParameterError, "Parameter #{param_name} cannot have length less than #{value}" unless param.nil? || value <= param.length
147
168
  when :max_length
148
169
  raise InvalidParameterError, "Parameter #{param_name} cannot have length greater than #{value}" unless param.nil? || value >= param.length
170
+ when :custom
171
+ value.call(param)
149
172
  end
150
173
  end
151
174
  end
@@ -1,3 +1,3 @@
1
1
  module RailsParam #:nodoc
2
- VERSION = "0.10.0"
2
+ VERSION = "0.11.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_param
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nicolas Blanco
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-12 00:00:00.000000000 Z
11
+ date: 2021-05-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: 3.2.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 3.2.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.2.0
69
83
  description: "\n Parameter Validation and Type Coercion for Rails\n "
70
84
  email: nicolas@nicolasblanco.fr
71
85
  executables: []
@@ -96,8 +110,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
110
  - !ruby/object:Gem::Version
97
111
  version: 1.3.6
98
112
  requirements: []
99
- rubyforge_project:
100
- rubygems_version: 2.5.1
113
+ rubygems_version: 3.1.6
101
114
  signing_key:
102
115
  specification_version: 4
103
116
  summary: Parameter Validation and Type Coercion for Rails