lucky_param 0.1.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +3 -13
- data/.travis.yml +1 -1
- data/Gemfile.lock +10 -103
- data/README.md +1 -1
- data/lib/lucky_param.rb +11 -10
- data/lib/lucky_param/checker.rb +22 -14
- data/lib/lucky_param/version.rb +1 -1
- data/lucky_param.gemspec +1 -1
- metadata +4 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 64d261e249956a9e8999bf426edf5cea2124db4b52bf53663d1344b691adaa8b
|
4
|
+
data.tar.gz: 7cafcbab4a0a503ff8046a0793dff7daee279491d8e33984be0b0dbbee917cd6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a61dc099c8033054be240c3b67b869149b49e72abbdf832f38244ec484d0d6dd3937d4fca6c2f204ec05d628aac82faa9a02b16bd104182badddeb723fc62943
|
7
|
+
data.tar.gz: 43285f26a67eae20d0486a4e6c257366d7a7eccef49e223d10334dd65d005b56f54d44858dd39fc8688cb7114f360f0b5361362aa5e887f0f2c9cf8da1da2c9a
|
data/.rubocop.yml
CHANGED
@@ -8,12 +8,6 @@ AllCops:
|
|
8
8
|
Style/AndOr:
|
9
9
|
Enabled: true
|
10
10
|
|
11
|
-
# Do not use braces for hash literals when they are the last argument of a
|
12
|
-
# method call.
|
13
|
-
Style/BracesAroundHashParameters:
|
14
|
-
Enabled: true
|
15
|
-
EnforcedStyle: context_dependent
|
16
|
-
|
17
11
|
# Align `when` with `case`.
|
18
12
|
Layout/CaseIndentation:
|
19
13
|
Enabled: true
|
@@ -58,7 +52,7 @@ Style/HashSyntax:
|
|
58
52
|
# extra level of indentation.
|
59
53
|
Layout/IndentationConsistency:
|
60
54
|
Enabled: true
|
61
|
-
EnforcedStyle:
|
55
|
+
EnforcedStyle: indented_internal_methods
|
62
56
|
|
63
57
|
# Two spaces, no tabs (for indentation).
|
64
58
|
Layout/IndentationWidth:
|
@@ -125,12 +119,8 @@ Style/StringLiterals:
|
|
125
119
|
Enabled: true
|
126
120
|
EnforcedStyle: double_quotes
|
127
121
|
|
128
|
-
# Detect hard tabs, no hard tabs.
|
129
|
-
Layout/Tab:
|
130
|
-
Enabled: true
|
131
|
-
|
132
122
|
# Blank lines should not have any spaces.
|
133
|
-
Layout/
|
123
|
+
Layout/TrailingEmptyLines:
|
134
124
|
Enabled: true
|
135
125
|
|
136
126
|
# No trailing whitespace.
|
@@ -138,7 +128,7 @@ Layout/TrailingWhitespace:
|
|
138
128
|
Enabled: true
|
139
129
|
|
140
130
|
# Use quotes for string literals when they are enough.
|
141
|
-
Style/
|
131
|
+
Style/RedundantPercentQ:
|
142
132
|
Enabled: true
|
143
133
|
|
144
134
|
# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
|
data/.travis.yml
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,119 +1,26 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
lucky_param (0.
|
5
|
-
|
4
|
+
lucky_param (1.0.0)
|
5
|
+
activesupport (>= 4.2)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
-
|
11
|
-
actionpack (= 5.2.1)
|
12
|
-
nio4r (~> 2.0)
|
13
|
-
websocket-driver (>= 0.6.1)
|
14
|
-
actionmailer (5.2.1)
|
15
|
-
actionpack (= 5.2.1)
|
16
|
-
actionview (= 5.2.1)
|
17
|
-
activejob (= 5.2.1)
|
18
|
-
mail (~> 2.5, >= 2.5.4)
|
19
|
-
rails-dom-testing (~> 2.0)
|
20
|
-
actionpack (5.2.1)
|
21
|
-
actionview (= 5.2.1)
|
22
|
-
activesupport (= 5.2.1)
|
23
|
-
rack (~> 2.0)
|
24
|
-
rack-test (>= 0.6.3)
|
25
|
-
rails-dom-testing (~> 2.0)
|
26
|
-
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
27
|
-
actionview (5.2.1)
|
28
|
-
activesupport (= 5.2.1)
|
29
|
-
builder (~> 3.1)
|
30
|
-
erubi (~> 1.4)
|
31
|
-
rails-dom-testing (~> 2.0)
|
32
|
-
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
33
|
-
activejob (5.2.1)
|
34
|
-
activesupport (= 5.2.1)
|
35
|
-
globalid (>= 0.3.6)
|
36
|
-
activemodel (5.2.1)
|
37
|
-
activesupport (= 5.2.1)
|
38
|
-
activerecord (5.2.1)
|
39
|
-
activemodel (= 5.2.1)
|
40
|
-
activesupport (= 5.2.1)
|
41
|
-
arel (>= 9.0)
|
42
|
-
activestorage (5.2.1)
|
43
|
-
actionpack (= 5.2.1)
|
44
|
-
activerecord (= 5.2.1)
|
45
|
-
marcel (~> 0.3.1)
|
46
|
-
activesupport (5.2.1)
|
10
|
+
activesupport (6.0.3.2)
|
47
11
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
48
12
|
i18n (>= 0.7, < 2)
|
49
13
|
minitest (~> 5.1)
|
50
14
|
tzinfo (~> 1.1)
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
crass (1.0.4)
|
55
|
-
erubi (1.7.1)
|
56
|
-
globalid (0.4.1)
|
57
|
-
activesupport (>= 4.2.0)
|
58
|
-
i18n (1.1.1)
|
15
|
+
zeitwerk (~> 2.2, >= 2.2.2)
|
16
|
+
concurrent-ruby (1.1.6)
|
17
|
+
i18n (1.8.3)
|
59
18
|
concurrent-ruby (~> 1.0)
|
60
|
-
|
61
|
-
crass (~> 1.0.2)
|
62
|
-
nokogiri (>= 1.5.9)
|
63
|
-
mail (2.7.1)
|
64
|
-
mini_mime (>= 0.1.1)
|
65
|
-
marcel (0.3.3)
|
66
|
-
mimemagic (~> 0.3.2)
|
67
|
-
method_source (0.9.0)
|
68
|
-
mimemagic (0.3.2)
|
69
|
-
mini_mime (1.0.1)
|
70
|
-
mini_portile2 (2.3.0)
|
71
|
-
minitest (5.11.3)
|
72
|
-
nio4r (2.3.1)
|
73
|
-
nokogiri (1.8.5)
|
74
|
-
mini_portile2 (~> 2.3.0)
|
75
|
-
rack (2.0.5)
|
76
|
-
rack-test (1.1.0)
|
77
|
-
rack (>= 1.0, < 3)
|
78
|
-
rails (5.2.1)
|
79
|
-
actioncable (= 5.2.1)
|
80
|
-
actionmailer (= 5.2.1)
|
81
|
-
actionpack (= 5.2.1)
|
82
|
-
actionview (= 5.2.1)
|
83
|
-
activejob (= 5.2.1)
|
84
|
-
activemodel (= 5.2.1)
|
85
|
-
activerecord (= 5.2.1)
|
86
|
-
activestorage (= 5.2.1)
|
87
|
-
activesupport (= 5.2.1)
|
88
|
-
bundler (>= 1.3.0)
|
89
|
-
railties (= 5.2.1)
|
90
|
-
sprockets-rails (>= 2.0.0)
|
91
|
-
rails-dom-testing (2.0.3)
|
92
|
-
activesupport (>= 4.2.0)
|
93
|
-
nokogiri (>= 1.6)
|
94
|
-
rails-html-sanitizer (1.0.4)
|
95
|
-
loofah (~> 2.2, >= 2.2.2)
|
96
|
-
railties (5.2.1)
|
97
|
-
actionpack (= 5.2.1)
|
98
|
-
activesupport (= 5.2.1)
|
99
|
-
method_source
|
100
|
-
rake (>= 0.8.7)
|
101
|
-
thor (>= 0.19.0, < 2.0)
|
102
|
-
rake (12.3.1)
|
103
|
-
sprockets (3.7.2)
|
104
|
-
concurrent-ruby (~> 1.0)
|
105
|
-
rack (> 1, < 3)
|
106
|
-
sprockets-rails (3.2.1)
|
107
|
-
actionpack (>= 4.0)
|
108
|
-
activesupport (>= 4.0)
|
109
|
-
sprockets (>= 3.0.0)
|
110
|
-
thor (0.20.0)
|
19
|
+
minitest (5.14.1)
|
111
20
|
thread_safe (0.3.6)
|
112
|
-
tzinfo (1.2.
|
21
|
+
tzinfo (1.2.7)
|
113
22
|
thread_safe (~> 0.1)
|
114
|
-
|
115
|
-
websocket-extensions (>= 0.1.0)
|
116
|
-
websocket-extensions (0.1.3)
|
23
|
+
zeitwerk (2.3.1)
|
117
24
|
|
118
25
|
PLATFORMS
|
119
26
|
ruby
|
@@ -122,4 +29,4 @@ DEPENDENCIES
|
|
122
29
|
lucky_param!
|
123
30
|
|
124
31
|
BUNDLED WITH
|
125
|
-
1.
|
32
|
+
2.1.4
|
data/README.md
CHANGED
@@ -22,7 +22,7 @@ class ApplicationController
|
|
22
22
|
# If necessary, you can overide or define your own parameter checkers.
|
23
23
|
LuckyParam::CUSTOM_CHECKER = {
|
24
24
|
Password: [
|
25
|
-
->(
|
25
|
+
->(params, column) { params[column] =~ /([0-9a-zA-Z]){6,30}/ },
|
26
26
|
"must be valid password"
|
27
27
|
],
|
28
28
|
}.freeze
|
data/lib/lucky_param.rb
CHANGED
@@ -5,6 +5,7 @@ require "active_support/core_ext/object/blank"
|
|
5
5
|
require_relative "./lucky_param/version"
|
6
6
|
require_relative "./lucky_param/checker"
|
7
7
|
|
8
|
+
# An easy and extendable controller validator helper for Rails.
|
8
9
|
module LuckyParam
|
9
10
|
class ParamMissError < StandardError; end
|
10
11
|
class ParamFormatError < StandardError; end
|
@@ -12,24 +13,24 @@ module LuckyParam
|
|
12
13
|
|
13
14
|
def required(column, checker_type)
|
14
15
|
raise ParamMissError.new("Missing Params: #{column}") if params[column].blank?
|
15
|
-
|
16
|
-
|
16
|
+
|
17
|
+
check_param_format(column, checker_type)
|
17
18
|
end
|
18
19
|
|
19
20
|
def optional(column, checker_type)
|
20
21
|
return if params[column].blank?
|
21
|
-
|
22
|
-
|
22
|
+
|
23
|
+
check_param_format(column, checker_type)
|
23
24
|
end
|
24
25
|
|
25
26
|
private
|
26
27
|
|
27
|
-
def
|
28
|
+
def check_param_format(column, checker_type)
|
28
29
|
checker = CUSTOM_CHECKER[checker_type] if LuckyParam.const_defined?(:CUSTOM_CHECKER)
|
29
|
-
checker ||= CHECKER.fetch(checker_type)
|
30
|
-
raise UnknownCheckerError
|
31
|
-
|
32
|
-
result = checker[0].call(params
|
33
|
-
|
30
|
+
checker ||= CHECKER.fetch(checker_type) do
|
31
|
+
raise UnknownCheckerError.new("Unknown checker `#{checker_type}`, try to define checker with const `LuckyParam::CUSTOM_CHECKER`")
|
32
|
+
end
|
33
|
+
result = checker[0].call(params, column)
|
34
|
+
raise ParamFormatError.new("Wrong Params Format: #{column} #{checker[1]}") unless result
|
34
35
|
end
|
35
36
|
end
|
data/lib/lucky_param/checker.rb
CHANGED
@@ -3,53 +3,61 @@
|
|
3
3
|
module LuckyParam
|
4
4
|
CHECKER = {
|
5
5
|
NONE: [
|
6
|
-
->(
|
6
|
+
->(params, column) { true }
|
7
7
|
],
|
8
8
|
String: [
|
9
|
-
->(
|
9
|
+
->(params, column) { params[column].is_a?(String) },
|
10
10
|
"must be valid String"
|
11
11
|
],
|
12
12
|
Boolean: [
|
13
|
-
->(
|
13
|
+
->(params, column) { %w[true false 1 0].include?(params[column].to_s) },
|
14
14
|
"must be one of [true false 1 0]"
|
15
15
|
],
|
16
16
|
Integer: [
|
17
|
-
->(
|
17
|
+
->(params, column) { params[column].to_s =~ /\A(0|[1-9]\d*)\Z$/ },
|
18
18
|
"must be valid Integer"
|
19
19
|
],
|
20
20
|
Float: [
|
21
|
-
->(
|
21
|
+
->(params, column) { params[column].to_s =~ /\A^[-+]?[0-9]+([,.][0-9]+)?\Z$/ },
|
22
22
|
"must be valid Float"
|
23
23
|
],
|
24
24
|
Number: [
|
25
|
-
|
25
|
+
lambda do |params, column|
|
26
|
+
begin
|
27
|
+
Float(params[column])
|
28
|
+
rescue StandardError
|
29
|
+
false
|
30
|
+
end
|
31
|
+
end
|
26
32
|
],
|
27
33
|
Email: [
|
28
|
-
->(
|
34
|
+
->(params, column) { params[column].to_s =~ /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/ },
|
29
35
|
"must be valid Email"
|
30
36
|
],
|
31
37
|
Timestamp: [
|
32
|
-
->(
|
38
|
+
->(params, column) { params[column].to_s =~ /^(\+|\-)?\d+$/ },
|
33
39
|
"must be valid Timestamp"
|
34
40
|
],
|
35
41
|
ArrayJSON: [
|
36
|
-
lambda
|
42
|
+
lambda do |params, column|
|
37
43
|
begin
|
38
|
-
JSON.parse(
|
44
|
+
params[column] = JSON.parse(params[column]) if params[column].is_a?(String)
|
45
|
+
params[column].is_a?(Array)
|
39
46
|
rescue StandardError
|
40
47
|
false
|
41
48
|
end
|
42
|
-
|
49
|
+
end,
|
43
50
|
"must be valid Array JSON"
|
44
51
|
],
|
45
52
|
HashJSON: [
|
46
|
-
lambda
|
53
|
+
lambda do |params, column|
|
47
54
|
begin
|
48
|
-
JSON.parse(
|
55
|
+
params[column] = JSON.parse(params[column]) if params[column].is_a?(String)
|
56
|
+
params[column].respond_to?(:to_hash)
|
49
57
|
rescue StandardError
|
50
58
|
false
|
51
59
|
end
|
52
|
-
|
60
|
+
end,
|
53
61
|
"must be valid Hash JSON"
|
54
62
|
]
|
55
63
|
}.freeze
|
data/lib/lucky_param/version.rb
CHANGED
data/lucky_param.gemspec
CHANGED
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lucky_param
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Shooting Fly
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -65,8 +65,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
65
65
|
- !ruby/object:Gem::Version
|
66
66
|
version: '0'
|
67
67
|
requirements: []
|
68
|
-
|
69
|
-
rubygems_version: 2.7.6
|
68
|
+
rubygems_version: 3.0.8
|
70
69
|
signing_key:
|
71
70
|
specification_version: 4
|
72
71
|
summary: Helper of parameter validation in controller
|