weak_parameters 0.4.2 → 0.4.3
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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +4 -0
- data/lib/weak_parameters/date_validator.rb +40 -0
- data/lib/weak_parameters/time_validator.rb +18 -0
- data/lib/weak_parameters/validator.rb +8 -0
- data/lib/weak_parameters/version.rb +1 -1
- data/lib/weak_parameters.rb +2 -0
- data/spec/dummy/app/controllers/concerns/with_recipe.rb +4 -0
- data/spec/dummy/app/controllers/concerns/with_strong.rb +4 -0
- data/spec/requests/api/recipes_spec.rb +28 -0
- data/spec/requests/api/strong_spec.rb +10 -0
- data/spec/requests/recipes_spec.rb +28 -0
- data/spec/requests/strong_spec.rb +10 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '09e8c53c2c8454671bcdc7b74e641d830a4198bc'
|
4
|
+
data.tar.gz: a70b4dc7bbdbea50696e8eda91a0101c230b9b49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 89b575f9765feb386807dd60ffdcdaa34d8741a09ab5bb1137f3ccbe20358456dad70f2a7eb2178e64966a05cd7eee4cb453701a01af82c2a1f4dec2428e0de7
|
7
|
+
data.tar.gz: 6f3bdc9e43d62048c604155551187e9547531d9e4a5c9529db29993b24085611be8185735fbaaed5adf95a20c318aa6408be68ee88cdfbd415d333ab0712efc7
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -26,6 +26,8 @@ class RecipesController < ApplicationController
|
|
26
26
|
string :quantity do |value|
|
27
27
|
value =~ /\A\d+(?:\.\d+)g\z/
|
28
28
|
end
|
29
|
+
date :date, only: ["%Y/%m/%d", "%Y.%m.%d"]
|
30
|
+
time :time, only: "%Y/%m/%d %H:%M:%S"
|
29
31
|
end
|
30
32
|
|
31
33
|
def create
|
@@ -59,6 +61,8 @@ irb(main):005:0> app.post "/recipes", name: "alice", type: "bob"
|
|
59
61
|
* file
|
60
62
|
* object
|
61
63
|
* list
|
64
|
+
* date (= default %Y-%m-%d)
|
65
|
+
* time (= default %Y-%m-%d %H:%M:%S)
|
62
66
|
|
63
67
|
### Available options
|
64
68
|
* required
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module WeakParameters
|
2
|
+
class DateValidator < WeakParameters::BaseValidator
|
3
|
+
private
|
4
|
+
|
5
|
+
def parser_class
|
6
|
+
::Date
|
7
|
+
end
|
8
|
+
|
9
|
+
def valid_type?
|
10
|
+
if options[:only]
|
11
|
+
Array(options[:only]).any? do |format|
|
12
|
+
begin
|
13
|
+
parser_class.strptime(value, format)
|
14
|
+
return false unless strictly?(format)
|
15
|
+
true
|
16
|
+
rescue ::ArgumentError
|
17
|
+
false
|
18
|
+
end
|
19
|
+
end
|
20
|
+
else
|
21
|
+
begin
|
22
|
+
parser_class.parse(value)
|
23
|
+
return false unless strictly?
|
24
|
+
true
|
25
|
+
rescue ::ArgumentError
|
26
|
+
false
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def strictly?(format = nil)
|
32
|
+
result = format ? ::Date._strptime(value, format) : ::Date._strptime(value)
|
33
|
+
result && !result.key?(:leftover)
|
34
|
+
end
|
35
|
+
|
36
|
+
def error_message
|
37
|
+
"params[#{key.inspect}] must be a valid Date"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module WeakParameters
|
2
|
+
class TimeValidator < WeakParameters::DateValidator
|
3
|
+
private
|
4
|
+
|
5
|
+
def parser_class
|
6
|
+
::Time
|
7
|
+
end
|
8
|
+
|
9
|
+
# Time is always true! (no custom validation)
|
10
|
+
def strictly?(format = nil)
|
11
|
+
true
|
12
|
+
end
|
13
|
+
|
14
|
+
def error_message
|
15
|
+
"params[#{key.inspect}] must be a valid Time"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -65,6 +65,14 @@ module WeakParameters
|
|
65
65
|
validators << WeakParameters::FloatValidator.new(controller, key, options, &block)
|
66
66
|
end
|
67
67
|
|
68
|
+
def date(key, options = {}, &block)
|
69
|
+
validators << WeakParameters::DateValidator.new(controller, key, options, &block)
|
70
|
+
end
|
71
|
+
|
72
|
+
def time(key, options = {}, &block)
|
73
|
+
validators << WeakParameters::TimeValidator.new(controller, key, options, &block)
|
74
|
+
end
|
75
|
+
|
68
76
|
def file(key, options = {}, &block)
|
69
77
|
validators << WeakParameters::FileValidator.new(controller, key, options, &block)
|
70
78
|
end
|
data/lib/weak_parameters.rb
CHANGED
@@ -6,6 +6,8 @@ require "weak_parameters/any_validator"
|
|
6
6
|
require "weak_parameters/array_validator"
|
7
7
|
require "weak_parameters/boolean_validator"
|
8
8
|
require "weak_parameters/file_validator"
|
9
|
+
require "weak_parameters/date_validator"
|
10
|
+
require "weak_parameters/time_validator"
|
9
11
|
require "weak_parameters/float_validator"
|
10
12
|
require "weak_parameters/hash_validator"
|
11
13
|
require "weak_parameters/integer_validator"
|
@@ -11,6 +11,10 @@ module WithRecipe
|
|
11
11
|
hash :config
|
12
12
|
array :tags
|
13
13
|
float :rate
|
14
|
+
date :date
|
15
|
+
date :custom_date, only: %w[%Y/%m/%d %Y.%m.%d]
|
16
|
+
time :time
|
17
|
+
time :custom_time, only: '%Y/%m/%d %H:%M:%S'
|
14
18
|
file :attachment
|
15
19
|
integer :custom, only: 0..1, handler: :render_error
|
16
20
|
string :zip_code do |value|
|
@@ -19,6 +19,10 @@ module WithStrong
|
|
19
19
|
array :strong_tags, strong: true
|
20
20
|
float :rate
|
21
21
|
float :strong_rate, strong: true
|
22
|
+
date :date
|
23
|
+
date :strong_date, strong: true
|
24
|
+
time :time
|
25
|
+
time :strong_time, strong: true
|
22
26
|
file :attachment
|
23
27
|
file :strong_attachment, strong: true
|
24
28
|
integer :custom, only: 0..1, handler: :render_error
|
@@ -106,6 +106,34 @@ describe "Recipes with rails-api", type: :request do
|
|
106
106
|
include_examples "400"
|
107
107
|
end
|
108
108
|
|
109
|
+
context "with wrong date param" do
|
110
|
+
before do
|
111
|
+
params[:date] = '2017-01-32'
|
112
|
+
end
|
113
|
+
include_examples "400"
|
114
|
+
end
|
115
|
+
|
116
|
+
context "with wrong custom_date param" do
|
117
|
+
before do
|
118
|
+
params[:custom_date] = Date.current.strftime('%Y-%m-%d')
|
119
|
+
end
|
120
|
+
include_examples "400"
|
121
|
+
end
|
122
|
+
|
123
|
+
context "with wrong time param" do
|
124
|
+
before do
|
125
|
+
params[:time] = '2017-01-31 24:00:01'
|
126
|
+
end
|
127
|
+
include_examples "400"
|
128
|
+
end
|
129
|
+
|
130
|
+
context "with wrong custom_time param" do
|
131
|
+
before do
|
132
|
+
params[:custom_time] = Time.current.strftime('%Y-%m-%d %H:%M:%S')
|
133
|
+
end
|
134
|
+
include_examples "400"
|
135
|
+
end
|
136
|
+
|
109
137
|
context "with wrong file param" do
|
110
138
|
before do
|
111
139
|
params[:attachment] = "x"
|
@@ -17,6 +17,10 @@ describe "Strong with rails-api", type: :request do
|
|
17
17
|
strong_config: { a: 1, b: { c: 2 } },
|
18
18
|
tags: [1],
|
19
19
|
strong_tags: [1],
|
20
|
+
date: Date.current.strftime('%Y-%m-%d'),
|
21
|
+
strong_date: Date.current.strftime('%Y-%m-%d'),
|
22
|
+
time: Time.current.strftime('%Y-%m-%d %H:%M:%S'),
|
23
|
+
strong_time: Time.current.strftime('%Y-%m-%d %H:%M:%S'),
|
20
24
|
attachment: Rack::Test::UploadedFile.new(__FILE__),
|
21
25
|
strong_attachment: Rack::Test::UploadedFile.new(__FILE__),
|
22
26
|
zip_code: "123-4567",
|
@@ -73,6 +77,12 @@ describe "Strong with rails-api", type: :request do
|
|
73
77
|
expect(controller.permitted_params).not_to have_key "strong_rate"
|
74
78
|
expect(controller.permitted_params).not_to have_key "rate"
|
75
79
|
|
80
|
+
expect(controller.permitted_params).to have_key "strong_date"
|
81
|
+
expect(controller.permitted_params).not_to have_key "date"
|
82
|
+
|
83
|
+
expect(controller.permitted_params).to have_key "strong_time"
|
84
|
+
expect(controller.permitted_params).not_to have_key "time"
|
85
|
+
|
76
86
|
expect(controller.permitted_params).to have_key "strong_attachment"
|
77
87
|
expect(controller.permitted_params).not_to have_key "attachment"
|
78
88
|
|
@@ -106,6 +106,34 @@ describe "Recipes", type: :request do
|
|
106
106
|
include_examples "400"
|
107
107
|
end
|
108
108
|
|
109
|
+
context "with wrong date param" do
|
110
|
+
before do
|
111
|
+
params[:date] = '2017-01-32'
|
112
|
+
end
|
113
|
+
include_examples "400"
|
114
|
+
end
|
115
|
+
|
116
|
+
context "with wrong custom_date param" do
|
117
|
+
before do
|
118
|
+
params[:custom_date] = Date.current.strftime('%Y-%m-%d')
|
119
|
+
end
|
120
|
+
include_examples "400"
|
121
|
+
end
|
122
|
+
|
123
|
+
context "with wrong time param" do
|
124
|
+
before do
|
125
|
+
params[:time] = '2017-01-31 24:00:01'
|
126
|
+
end
|
127
|
+
include_examples "400"
|
128
|
+
end
|
129
|
+
|
130
|
+
context "with wrong custom_time param" do
|
131
|
+
before do
|
132
|
+
params[:custom_time] = Time.current.strftime('%Y-%m-%d %H:%M:%S')
|
133
|
+
end
|
134
|
+
include_examples "400"
|
135
|
+
end
|
136
|
+
|
109
137
|
context "with wrong file param" do
|
110
138
|
before do
|
111
139
|
params[:attachment] = "x"
|
@@ -17,6 +17,10 @@ describe "Strong", type: :request do
|
|
17
17
|
strong_config: { a: 1, b: { c: 2 } },
|
18
18
|
tags: [1],
|
19
19
|
strong_tags: [1],
|
20
|
+
date: Date.current.strftime('%Y-%m-%d'),
|
21
|
+
strong_date: Date.current.strftime('%Y-%m-%d'),
|
22
|
+
time: Time.current.strftime('%Y-%m-%d %H:%M:%S'),
|
23
|
+
strong_time: Time.current.strftime('%Y-%m-%d %H:%M:%S'),
|
20
24
|
attachment: Rack::Test::UploadedFile.new(__FILE__),
|
21
25
|
strong_attachment: Rack::Test::UploadedFile.new(__FILE__),
|
22
26
|
zip_code: "123-4567",
|
@@ -73,6 +77,12 @@ describe "Strong", type: :request do
|
|
73
77
|
expect(controller.permitted_params).not_to have_key "strong_rate"
|
74
78
|
expect(controller.permitted_params).not_to have_key "rate"
|
75
79
|
|
80
|
+
expect(controller.permitted_params).to have_key "strong_date"
|
81
|
+
expect(controller.permitted_params).not_to have_key "date"
|
82
|
+
|
83
|
+
expect(controller.permitted_params).to have_key "strong_time"
|
84
|
+
expect(controller.permitted_params).not_to have_key "time"
|
85
|
+
|
76
86
|
expect(controller.permitted_params).to have_key "strong_attachment"
|
77
87
|
expect(controller.permitted_params).not_to have_key "attachment"
|
78
88
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: weak_parameters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryo Nakamura
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -142,6 +142,7 @@ files:
|
|
142
142
|
- lib/weak_parameters/base_validator.rb
|
143
143
|
- lib/weak_parameters/boolean_validator.rb
|
144
144
|
- lib/weak_parameters/controller.rb
|
145
|
+
- lib/weak_parameters/date_validator.rb
|
145
146
|
- lib/weak_parameters/file_validator.rb
|
146
147
|
- lib/weak_parameters/float_validator.rb
|
147
148
|
- lib/weak_parameters/hash_validator.rb
|
@@ -149,6 +150,7 @@ files:
|
|
149
150
|
- lib/weak_parameters/list_validator.rb
|
150
151
|
- lib/weak_parameters/object_validator.rb
|
151
152
|
- lib/weak_parameters/string_validator.rb
|
153
|
+
- lib/weak_parameters/time_validator.rb
|
152
154
|
- lib/weak_parameters/validation_error.rb
|
153
155
|
- lib/weak_parameters/validator.rb
|
154
156
|
- lib/weak_parameters/version.rb
|
@@ -219,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
219
221
|
version: '0'
|
220
222
|
requirements: []
|
221
223
|
rubyforge_project:
|
222
|
-
rubygems_version: 2.
|
224
|
+
rubygems_version: 2.6.11
|
223
225
|
signing_key:
|
224
226
|
specification_version: 4
|
225
227
|
summary: Add a validation filter to your controller.
|