stronger_parameters 1.0.2 → 1.1.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
  SHA1:
3
- metadata.gz: 93d2fdbe9f0ef6a466cd4bf9b767378b372db899
4
- data.tar.gz: a49cd1dbb65c4f3f2f889c99ebdf1920f0d7b4e8
3
+ metadata.gz: 008fefe30dfbd3ec1f72923ca934d2e13bbf2d16
4
+ data.tar.gz: 217fcf6bcf4d7a9e5267578340aadb8c1e5f5c25
5
5
  SHA512:
6
- metadata.gz: 1c55ade72ce711a2a3414c794bb573446f6beb5459b1d3cf9938b49305d611d44ee5cc05618b378dab28767fd978af51ec387c1295807c52b1ebf6783630c0f8
7
- data.tar.gz: fc4e1f91e3ecf5a151277cab998377dae87838e2d7f6ea0b5d3e1abf53dcd04305127e91df44987a91eaacfa329000a0a23ecabec128fbd9e2a9c4d31f4d6887
6
+ metadata.gz: 77063ef7951b0164cae2a152294078aa6c8de1d0e14ef4274cdf2a5fc75a2ff59e1e3f499903c3bf2c91d453478e659cf4c17a5567d0ea545bb4c2f2c3314cf2
7
+ data.tar.gz: 89a19943e2d3155b69801d5ce08bfb3decf81e3bc94ab63db815a06b2879586068543eb7b71a56d5803500780bb1fce526e00625e5d56620c09ee1600899f21c
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # stronger_parameters
2
+ [![Build Status](https://travis-ci.org/zendesk/stronger_parameters.svg?branch=master)](https://travis-ci.org/zendesk/stronger_parameters)
2
3
 
3
4
  This is an extension of `strong_parameters` with added type checking.
4
5
 
@@ -5,11 +5,12 @@ module StrongerParameters
5
5
  attr_reader :constraints
6
6
 
7
7
  def initialize(constraints)
8
- @constraints = constraints.with_indifferent_access
8
+ @constraints = constraints.with_indifferent_access unless constraints.nil?
9
9
  end
10
10
 
11
11
  def value(v)
12
12
  if v.is_a?(Hash)
13
+ return ActionController::Parameters.new(v).permit! if constraints.nil?
13
14
  return ActionController::Parameters.new(v).permit(constraints)
14
15
  end
15
16
 
@@ -1,9 +1,9 @@
1
1
  require 'stronger_parameters/constraints'
2
2
 
3
3
  module StrongerParameters
4
- class FixnumConstraint < Constraint
4
+ class IntegerConstraint < Constraint
5
5
  def value(v)
6
- if v.is_a?(Fixnum)
6
+ if v.is_a?(Fixnum) || v.is_a?(Bignum)
7
7
  return v
8
8
  elsif v.is_a?(String) && v =~ /\A-?\d+\Z/
9
9
  return v.to_i
@@ -76,7 +76,7 @@ module StrongerParameters
76
76
  end
77
77
 
78
78
  require 'stronger_parameters/constraints/string_constraint'
79
- require 'stronger_parameters/constraints/fixnum_constraint'
79
+ require 'stronger_parameters/constraints/integer_constraint'
80
80
  require 'stronger_parameters/constraints/boolean_constraint'
81
81
  require 'stronger_parameters/constraints/array_constraint'
82
82
  require 'stronger_parameters/constraints/hash_constraint'
@@ -28,7 +28,7 @@ module StrongerParameters
28
28
  end
29
29
 
30
30
  def integer
31
- @integer ||= FixnumConstraint.new
31
+ @integer ||= IntegerConstraint.new
32
32
  end
33
33
 
34
34
  def lt(limit)
@@ -76,7 +76,7 @@ module StrongerParameters
76
76
  ArrayConstraint.new(item_constraint)
77
77
  end
78
78
 
79
- def map(constraints)
79
+ def map(constraints = nil)
80
80
  HashConstraint.new(constraints)
81
81
  end
82
82
  end
@@ -1,3 +1,3 @@
1
1
  module StrongerParameters
2
- VERSION = '1.0.2'
2
+ VERSION = '1.1.0'
3
3
  end
metadata CHANGED
@@ -1,127 +1,127 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stronger_parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mick Staugaard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-08 00:00:00.000000000 Z
11
+ date: 2015-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 4.7.5
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 4.7.5
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: guard
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: guard-minitest
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: minitest-rails
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  - !ruby/object:Gem::Dependency
98
- name: appraisal
98
+ name: wwtd
99
99
  requirement: !ruby/object:Gem::Requirement
100
100
  requirements:
101
- - - '>='
101
+ - - ">="
102
102
  - !ruby/object:Gem::Version
103
103
  version: '0'
104
104
  type: :development
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
- - - '>='
108
+ - - ">="
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: actionpack
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
- - - '>'
115
+ - - ">"
116
116
  - !ruby/object:Gem::Version
117
- version: '2'
117
+ version: '3'
118
118
  type: :runtime
119
119
  prerelease: false
120
120
  version_requirements: !ruby/object:Gem::Requirement
121
121
  requirements:
122
- - - '>'
122
+ - - ">"
123
123
  - !ruby/object:Gem::Version
124
- version: '2'
124
+ version: '3'
125
125
  description:
126
126
  email:
127
127
  - mick@zendesk.com
@@ -129,30 +129,20 @@ executables: []
129
129
  extensions: []
130
130
  extra_rdoc_files: []
131
131
  files:
132
+ - README.md
133
+ - lib/stronger_parameters.rb
134
+ - lib/stronger_parameters/constraints.rb
132
135
  - lib/stronger_parameters/constraints/array_constraint.rb
133
136
  - lib/stronger_parameters/constraints/boolean_constraint.rb
134
137
  - lib/stronger_parameters/constraints/comparison_constraints.rb
135
138
  - lib/stronger_parameters/constraints/enumeration_constraint.rb
136
- - lib/stronger_parameters/constraints/fixnum_constraint.rb
137
139
  - lib/stronger_parameters/constraints/hash_constraint.rb
140
+ - lib/stronger_parameters/constraints/integer_constraint.rb
138
141
  - lib/stronger_parameters/constraints/string_constraint.rb
139
- - lib/stronger_parameters/constraints.rb
140
142
  - lib/stronger_parameters/errors.rb
141
143
  - lib/stronger_parameters/parameters.rb
142
144
  - lib/stronger_parameters/railtie.rb
143
145
  - lib/stronger_parameters/version.rb
144
- - lib/stronger_parameters.rb
145
- - test/array_contraints_test.rb
146
- - test/boolean_constraints_test.rb
147
- - test/comparison_constraints_test.rb
148
- - test/controller_test.rb
149
- - test/enum_constraints_test.rb
150
- - test/fixnum_constraints_test.rb
151
- - test/hash_constraint_test.rb
152
- - test/operator_constraints_test.rb
153
- - test/string_constraints_test.rb
154
- - test/test_helper.rb
155
- - README.md
156
146
  homepage: https://github.com/zendesk/stronger_parameters
157
147
  licenses:
158
148
  - Apache License Version 2.0
@@ -163,28 +153,18 @@ require_paths:
163
153
  - lib
164
154
  required_ruby_version: !ruby/object:Gem::Requirement
165
155
  requirements:
166
- - - '>='
156
+ - - ">="
167
157
  - !ruby/object:Gem::Version
168
158
  version: '0'
169
159
  required_rubygems_version: !ruby/object:Gem::Requirement
170
160
  requirements:
171
- - - '>='
161
+ - - ">="
172
162
  - !ruby/object:Gem::Version
173
163
  version: '0'
174
164
  requirements: []
175
165
  rubyforge_project:
176
- rubygems_version: 2.0.14
166
+ rubygems_version: 2.2.2
177
167
  signing_key:
178
168
  specification_version: 4
179
169
  summary: Type checking and type casting of parameters for Action Pack
180
- test_files:
181
- - test/array_contraints_test.rb
182
- - test/boolean_constraints_test.rb
183
- - test/comparison_constraints_test.rb
184
- - test/controller_test.rb
185
- - test/enum_constraints_test.rb
186
- - test/fixnum_constraints_test.rb
187
- - test/hash_constraint_test.rb
188
- - test/operator_constraints_test.rb
189
- - test/string_constraints_test.rb
190
- - test/test_helper.rb
170
+ test_files: []
@@ -1,12 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe 'array parameter constraints' do
4
- subject { ActionController::Parameters.array(ActionController::Parameters.string) }
5
-
6
- permits ['a', 'b']
7
-
8
- rejects 'abc'
9
- rejects 123
10
- rejects [123, 456]
11
- rejects ['abc', 123]
12
- end
@@ -1,17 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe 'boolean parameter constraints' do
4
- subject { ActionController::Parameters.boolean }
5
-
6
- permits true, :as => true
7
- permits 'true', :as => true
8
- permits 1, :as => true
9
- permits '1', :as => true
10
-
11
- permits false, :as => false
12
- permits 'false', :as => false
13
- permits 0, :as => false
14
- permits '0', :as => false
15
-
16
- rejects 'foo'
17
- end
@@ -1,35 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe 'comparison parameter constraints' do
4
- describe 'less-than types' do
5
- subject { ActionController::Parameters.lt(2) }
6
-
7
- permits 1
8
- rejects 2
9
- rejects 3
10
- end
11
-
12
- describe 'less-than-or-equal types' do
13
- subject { ActionController::Parameters.lte(2) }
14
-
15
- permits 1
16
- permits 2
17
- rejects 3
18
- end
19
-
20
- describe 'greater-than types' do
21
- subject { ActionController::Parameters.gt(2) }
22
-
23
- rejects 1
24
- rejects 2
25
- permits 3
26
- end
27
-
28
- describe 'greater-than-or-equal types' do
29
- subject { ActionController::Parameters.gte(2) }
30
-
31
- rejects 1
32
- permits 2
33
- permits 3
34
- end
35
- end
@@ -1,26 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class BooksController < ActionController::Base
4
- def create
5
- params.require(:book).permit(:id => Parameters.integer)
6
-
7
- head :ok
8
- end
9
- end
10
-
11
- describe BooksController do
12
- it 'rejects invalid params' do
13
- post :create, { :magazine => { :name => 'Mjallo!' } }
14
- assert_response :bad_request
15
- response.body.must_equal 'Required parameter missing: book'
16
-
17
- post :create, { :book => { :id => 'Mjallo!' } }
18
- assert_response :bad_request
19
- response.body.must_equal 'Invalid parameter: id must be an integer'
20
- end
21
-
22
- it 'permits valid params' do
23
- post :create, { :book => { :id => '123' } }
24
- assert_response :ok
25
- end
26
- end
@@ -1,12 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe 'enum parameter constraints' do
4
- subject { ActionController::Parameters.enumeration('abc', 123) }
5
-
6
- permits 'abc'
7
- permits 123
8
-
9
- rejects 'abcd'
10
- rejects '123'
11
- rejects 1234
12
- end
@@ -1,14 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe 'integer parameter constraints' do
4
- subject { ActionController::Parameters.integer }
5
-
6
- permits 123
7
- permits '123', :as => 123
8
- permits '-123', :as => -123
9
-
10
- rejects 'abc'
11
- rejects Date.today
12
- rejects Time.now
13
-
14
- end
@@ -1,28 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe 'array parameter constraints' do
4
- subject do
5
- ActionController::Parameters.map(
6
- :id => ActionController::Parameters.integer,
7
- :name => ActionController::Parameters.string
8
- )
9
- end
10
-
11
- def self.permits(value, options = {})
12
- options[:as] ||= value
13
- options[:as] = options[:as].with_indifferent_access
14
-
15
- super(value, options)
16
- end
17
-
18
- permits(:id => 1, :name => 'Mick')
19
- permits({:id => '1', :name => 'Mick'}, :as => {:id => 1, :name => 'Mick'})
20
- permits(:id => 1)
21
- permits({:id => '1'}, :as => {:id => 1})
22
- permits(:name => 'Mick')
23
-
24
- rejects(:id => 1, :name => 123)
25
- rejects(:id => 'Mick', :name => 'Mick')
26
- rejects(123)
27
- rejects('abc')
28
- end
@@ -1,22 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe 'operator parameter constraints' do
4
- describe 'OR types' do
5
- subject { ActionController::Parameters.integer | ActionController::Parameters.string }
6
-
7
- permits 'abc'
8
- permits '123', :as => 123
9
-
10
- rejects Date.today
11
- rejects Time.now
12
- end
13
-
14
- describe 'AND types' do
15
- subject { ActionController::Parameters.string & ActionController::Parameters.integer }
16
-
17
- permits '123', :as => 123
18
-
19
- rejects 123
20
- rejects 'abc'
21
- end
22
- end
@@ -1,15 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe 'string parameter constraints' do
4
- subject { ActionController::Parameters.string }
5
-
6
- permits 'abc'
7
-
8
- rejects 123
9
- rejects Date.today
10
- rejects Time.now
11
-
12
- it 'rejects strings that are too long' do
13
- assert_rejects(:value) { params(:value => '123').permit(:value => ActionController::Parameters.string(:max_length => 2)) }
14
- end
15
- end
data/test/test_helper.rb DELETED
@@ -1,74 +0,0 @@
1
- ENV["RAILS_ENV"] = "test"
2
-
3
- require 'test/unit'
4
- require 'rails'
5
- require 'action_controller'
6
- require 'rails/test_help'
7
-
8
- class FakeApplication < Rails::Application; end
9
-
10
- Rails.application = FakeApplication
11
- Rails.configuration.action_controller = ActiveSupport::OrderedOptions.new
12
- Rails.configuration.secret_key_base = 'secret_key_base'
13
-
14
- require 'action_pack'
15
- require 'strong_parameters' if ActionPack::VERSION::MAJOR == 3
16
-
17
- module ActionController
18
- SharedTestRoutes = ActionDispatch::Routing::RouteSet.new
19
- SharedTestRoutes.draw do
20
- get ':controller(/:action)'
21
- post ':controller(/:action)'
22
- put ':controller(/:action)'
23
- delete ':controller(/:action)'
24
- end
25
-
26
- class Base
27
- include ActionController::Testing
28
- include SharedTestRoutes.url_helpers
29
-
30
- rescue_from(ActionController::ParameterMissing) do |e|
31
- render :text => "Required parameter missing: #{e.param}", :status => :bad_request
32
- end
33
- end
34
-
35
- class ActionController::TestCase
36
- setup do
37
- @routes = SharedTestRoutes
38
- end
39
- end
40
- end
41
-
42
- require 'stronger_parameters'
43
- require 'minitest/rails'
44
- require 'minitest/autorun'
45
-
46
- ActionController::Parameters.action_on_invalid_parameters = :raise
47
-
48
- class MiniTest::Spec
49
- def params(hash)
50
- ActionController::Parameters.new(hash)
51
- end
52
-
53
- def assert_rejects(key, &block)
54
- err = block.must_raise StrongerParameters::InvalidParameter
55
- err.key.must_equal key.to_s
56
- end
57
-
58
- def self.permits(value, options = {})
59
- type_casted = options.fetch(:as, value)
60
-
61
- it "permits #{value.inspect} as #{type_casted.inspect}" do
62
- permitted = params(:value => value).permit(:value => subject)
63
- permitted[:value].must_equal type_casted
64
- end
65
- end
66
-
67
- def self.rejects(value, options = {})
68
- key = options.fetch(:key, :value)
69
-
70
- it "rejects #{value.inspect}" do
71
- assert_rejects(key) { params(:value => value).permit(:value => subject) }
72
- end
73
- end
74
- end