lucky_param 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rubocop.yml +154 -0
- data/.travis.yml +10 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +125 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/lucky_param/checker.rb +49 -0
- data/lib/lucky_param/version.rb +5 -0
- data/lib/lucky_param.rb +41 -0
- data/lucky_param.gemspec +28 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a017ce0d189d7f1c397675300a770e553ef058769f5b8250b451e0c5411bdc10
|
4
|
+
data.tar.gz: 7cab2988621b49fd9c0b0d5075d8301ff554b2c72e403e0e110906b84117fcd5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2926ebff40cdf3171069d567d287995bfbb41254903b2ea40266e398ce08645e7d6da9c8a54482c24abb6551eac6a938359f9032437f8d4ef3ccacef83594818
|
7
|
+
data.tar.gz: 8185c5c344ae28cf992a575dce343a29c73748220712176710ff04eea4c1a35ca318c1e82b0466d23d052cadcfe3bda70d9ac9f5b96d49c9c78523486c766964
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
# RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
|
4
|
+
# to ignore them, so only the ones explicitly set in this file are enabled.
|
5
|
+
DisabledByDefault: true
|
6
|
+
|
7
|
+
# Prefer &&/|| over and/or.
|
8
|
+
Style/AndOr:
|
9
|
+
Enabled: true
|
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
|
+
# Align `when` with `case`.
|
18
|
+
Layout/CaseIndentation:
|
19
|
+
Enabled: true
|
20
|
+
|
21
|
+
# Align comments with method definitions.
|
22
|
+
Layout/CommentIndentation:
|
23
|
+
Enabled: true
|
24
|
+
|
25
|
+
Layout/ElseAlignment:
|
26
|
+
Enabled: true
|
27
|
+
|
28
|
+
Layout/EmptyLineAfterMagicComment:
|
29
|
+
Enabled: true
|
30
|
+
|
31
|
+
# In a regular class definition, no empty lines around the body.
|
32
|
+
Layout/EmptyLinesAroundClassBody:
|
33
|
+
Enabled: true
|
34
|
+
|
35
|
+
# In a regular method definition, no empty lines around the body.
|
36
|
+
Layout/EmptyLinesAroundMethodBody:
|
37
|
+
Enabled: true
|
38
|
+
|
39
|
+
# In a regular module definition, no empty lines around the body.
|
40
|
+
Layout/EmptyLinesAroundModuleBody:
|
41
|
+
Enabled: true
|
42
|
+
|
43
|
+
# Align `end` with the matching keyword or starting expression except for
|
44
|
+
# assignments, where it should be aligned with the LHS.
|
45
|
+
Layout/EndAlignment:
|
46
|
+
Enabled: true
|
47
|
+
EnforcedStyleAlignWith: variable
|
48
|
+
AutoCorrect: true
|
49
|
+
|
50
|
+
Layout/FirstParameterIndentation:
|
51
|
+
Enabled: true
|
52
|
+
|
53
|
+
# Use Ruby >= 1.9 syntax for hashes. Prefer { a: :b } over { :a => :b }.
|
54
|
+
Style/HashSyntax:
|
55
|
+
Enabled: true
|
56
|
+
|
57
|
+
# Method definitions after `private` or `protected` isolated calls need one
|
58
|
+
# extra level of indentation.
|
59
|
+
Layout/IndentationConsistency:
|
60
|
+
Enabled: true
|
61
|
+
EnforcedStyle: rails
|
62
|
+
|
63
|
+
# Two spaces, no tabs (for indentation).
|
64
|
+
Layout/IndentationWidth:
|
65
|
+
Enabled: true
|
66
|
+
|
67
|
+
Layout/LeadingCommentSpace:
|
68
|
+
Enabled: true
|
69
|
+
|
70
|
+
Layout/SpaceAfterColon:
|
71
|
+
Enabled: true
|
72
|
+
|
73
|
+
Layout/SpaceAfterComma:
|
74
|
+
Enabled: true
|
75
|
+
|
76
|
+
Layout/SpaceAroundEqualsInParameterDefault:
|
77
|
+
Enabled: true
|
78
|
+
|
79
|
+
Layout/SpaceAroundKeyword:
|
80
|
+
Enabled: true
|
81
|
+
|
82
|
+
Layout/SpaceAroundOperators:
|
83
|
+
Enabled: true
|
84
|
+
|
85
|
+
Layout/SpaceBeforeComma:
|
86
|
+
Enabled: true
|
87
|
+
|
88
|
+
Layout/SpaceBeforeFirstArg:
|
89
|
+
Enabled: true
|
90
|
+
|
91
|
+
Style/DefWithParentheses:
|
92
|
+
Enabled: true
|
93
|
+
|
94
|
+
# Defining a method with parameters needs parentheses.
|
95
|
+
Style/MethodDefParentheses:
|
96
|
+
Enabled: true
|
97
|
+
|
98
|
+
Style/FrozenStringLiteralComment:
|
99
|
+
Enabled: true
|
100
|
+
EnforcedStyle: always
|
101
|
+
Exclude:
|
102
|
+
- 'actionview/test/**/*.builder'
|
103
|
+
- 'actionview/test/**/*.ruby'
|
104
|
+
- 'actionpack/test/**/*.builder'
|
105
|
+
- 'actionpack/test/**/*.ruby'
|
106
|
+
- 'activestorage/db/migrate/**/*.rb'
|
107
|
+
|
108
|
+
# Use `foo {}` not `foo{}`.
|
109
|
+
Layout/SpaceBeforeBlockBraces:
|
110
|
+
Enabled: true
|
111
|
+
|
112
|
+
# Use `foo { bar }` not `foo {bar}`.
|
113
|
+
Layout/SpaceInsideBlockBraces:
|
114
|
+
Enabled: true
|
115
|
+
|
116
|
+
# Use `{ a: 1 }` not `{a:1}`.
|
117
|
+
Layout/SpaceInsideHashLiteralBraces:
|
118
|
+
Enabled: true
|
119
|
+
|
120
|
+
Layout/SpaceInsideParens:
|
121
|
+
Enabled: true
|
122
|
+
|
123
|
+
# Check quotes usage according to lint rule below.
|
124
|
+
Style/StringLiterals:
|
125
|
+
Enabled: true
|
126
|
+
EnforcedStyle: double_quotes
|
127
|
+
|
128
|
+
# Detect hard tabs, no hard tabs.
|
129
|
+
Layout/Tab:
|
130
|
+
Enabled: true
|
131
|
+
|
132
|
+
# Blank lines should not have any spaces.
|
133
|
+
Layout/TrailingBlankLines:
|
134
|
+
Enabled: true
|
135
|
+
|
136
|
+
# No trailing whitespace.
|
137
|
+
Layout/TrailingWhitespace:
|
138
|
+
Enabled: true
|
139
|
+
|
140
|
+
# Use quotes for string literals when they are enough.
|
141
|
+
Style/UnneededPercentQ:
|
142
|
+
Enabled: true
|
143
|
+
|
144
|
+
# Use my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
|
145
|
+
Lint/RequireParentheses:
|
146
|
+
Enabled: true
|
147
|
+
|
148
|
+
Style/RedundantReturn:
|
149
|
+
Enabled: true
|
150
|
+
AllowMultipleReturnValues: true
|
151
|
+
|
152
|
+
Style/Semicolon:
|
153
|
+
Enabled: true
|
154
|
+
AllowAsExpressionSeparator: true
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at xiefei@yolanda.hk. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
lucky_param (0.1.0)
|
5
|
+
rails (>= 4.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actioncable (5.2.1)
|
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)
|
47
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
48
|
+
i18n (>= 0.7, < 2)
|
49
|
+
minitest (~> 5.1)
|
50
|
+
tzinfo (~> 1.1)
|
51
|
+
arel (9.0.0)
|
52
|
+
builder (3.2.3)
|
53
|
+
concurrent-ruby (1.0.5)
|
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)
|
59
|
+
concurrent-ruby (~> 1.0)
|
60
|
+
loofah (2.2.2)
|
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 (10.5.0)
|
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)
|
111
|
+
thread_safe (0.3.6)
|
112
|
+
tzinfo (1.2.5)
|
113
|
+
thread_safe (~> 0.1)
|
114
|
+
websocket-driver (0.7.0)
|
115
|
+
websocket-extensions (>= 0.1.0)
|
116
|
+
websocket-extensions (0.1.3)
|
117
|
+
|
118
|
+
PLATFORMS
|
119
|
+
ruby
|
120
|
+
|
121
|
+
DEPENDENCIES
|
122
|
+
lucky_param!
|
123
|
+
|
124
|
+
BUNDLED WITH
|
125
|
+
1.16.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 谢非
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# LuckyParam
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/shootingfly/lucky_param.svg?branch=master)](https://travis-ci.org/shootingfly/lucky_param)
|
4
|
+
|
5
|
+
A simple parameter validation in controller for Rails.
|
6
|
+
|
7
|
+
Compare to `apipie`, `grape`, it is neccessary to check param in controller.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
```rb
|
12
|
+
gem "lucky_param"
|
13
|
+
```
|
14
|
+
|
15
|
+
### Define any validator for your app:
|
16
|
+
app/controllers/application_controller.rb
|
17
|
+
|
18
|
+
```rb
|
19
|
+
class SessionControllers < ApplicationController
|
20
|
+
include LuckyParam
|
21
|
+
|
22
|
+
LuckyParam::CUSTOM_CHECKER = {
|
23
|
+
Email: [
|
24
|
+
->(obj) { obj =~ /([0-9a-zA-Z]){6,30}/ },
|
25
|
+
"must be valid email"
|
26
|
+
],
|
27
|
+
Password: [
|
28
|
+
->(obj) { obj =~ /([0-9a-zA-Z]){6,30}/ },
|
29
|
+
"must be valid password"
|
30
|
+
],
|
31
|
+
}.freeze
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
app/controllers/sessions_controller.rb
|
36
|
+
|
37
|
+
```rb
|
38
|
+
class SessionControllers < ApplicationController
|
39
|
+
def create
|
40
|
+
required :email, :Email
|
41
|
+
required :password, :Password
|
42
|
+
render json: 'ok'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
## License
|
48
|
+
|
49
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "lucky_param"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LuckyParam
|
4
|
+
CHECKER = {
|
5
|
+
NONE: [
|
6
|
+
->(_obj) { true }
|
7
|
+
],
|
8
|
+
String: [
|
9
|
+
->(obj) { obj =~ /\w/ },
|
10
|
+
"must be valid String"
|
11
|
+
],
|
12
|
+
Integer: [
|
13
|
+
->(obj) { obj =~ /\d+/ },
|
14
|
+
"must be valid Integer"
|
15
|
+
],
|
16
|
+
Email: [
|
17
|
+
->(obj) { obj =~ /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/ },
|
18
|
+
"must be valid Email"
|
19
|
+
],
|
20
|
+
Float: [
|
21
|
+
->(obj) { obj =~ /^(-?\d+)(\.\d+)?$/ },
|
22
|
+
"must be valid Float"
|
23
|
+
],
|
24
|
+
Timestamp: [
|
25
|
+
->(obj) { obj =~ /^(\+|\-)?\d+$/ },
|
26
|
+
"must be valid Timestamp"
|
27
|
+
],
|
28
|
+
ArrayJSON: [
|
29
|
+
lambda { |obj|
|
30
|
+
begin
|
31
|
+
JSON.parse(obj).is_a?(Array)
|
32
|
+
rescue StandardError
|
33
|
+
false
|
34
|
+
end
|
35
|
+
},
|
36
|
+
"must be valid Array JSON"
|
37
|
+
],
|
38
|
+
HashJSON: [
|
39
|
+
lambda { |obj|
|
40
|
+
begin
|
41
|
+
JSON.parse(obj).is_a?(Hash)
|
42
|
+
rescue StandardError
|
43
|
+
false
|
44
|
+
end
|
45
|
+
},
|
46
|
+
"must be valid Hash JSON"
|
47
|
+
]
|
48
|
+
}.freeze
|
49
|
+
end
|
data/lib/lucky_param.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "./lucky_param/version"
|
4
|
+
require_relative "./lucky_param/checker"
|
5
|
+
|
6
|
+
module LuckyParam
|
7
|
+
class ParamMissError < StandardError
|
8
|
+
def initialize(column)
|
9
|
+
super "Missing Params: #{column}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class ParamFormatError < StandardError
|
14
|
+
def initialize(message)
|
15
|
+
super "Wrong Params Format: #{message}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def required(column, checker_type = :NONE)
|
20
|
+
raise ParamMissError, column unless params.key?(column)
|
21
|
+
|
22
|
+
message = _checker_message(column, checker_type)
|
23
|
+
raise ParamFormatError, message if message
|
24
|
+
end
|
25
|
+
|
26
|
+
def optional(column, checker_type = :NONE)
|
27
|
+
return unless params.key?(column)
|
28
|
+
|
29
|
+
message = _checker_message(column, checker_type)
|
30
|
+
raise ParamFormatError, message if message
|
31
|
+
end
|
32
|
+
|
33
|
+
def _checker_message(column, checker_type)
|
34
|
+
checker = CUSTOM_CHECKER[checker_type] if LuckyParam.const_defined?(:CUSTOM_CHECKER)
|
35
|
+
checker ||= CHECKER.fetch(checker_type) {
|
36
|
+
raise "Unknown checker `#{checker_type}`, try to define checker with const `LuckyParam::CUSTOM_CHECKER`"
|
37
|
+
}
|
38
|
+
result = checker[0].call(params[column])
|
39
|
+
result ? nil : checker[1]
|
40
|
+
end
|
41
|
+
end
|
data/lucky_param.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path("lib", __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require "lucky_param/version"
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "lucky_param"
|
9
|
+
spec.version = LuckyParam::VERSION
|
10
|
+
spec.authors = ["Shooting Fly"]
|
11
|
+
spec.email = ["rocky13670250811@gmail.com"]
|
12
|
+
|
13
|
+
spec.summary = "Helper of parameter validation in controller"
|
14
|
+
spec.description = "An easy and extendable controller validator helper for Rails."
|
15
|
+
spec.homepage = "https://github.com/shootingfly/lucky_param"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
spec.add_dependency "rails", ">= 4.2"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lucky_param
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Shooting Fly
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.2'
|
27
|
+
description: An easy and extendable controller validator helper for Rails.
|
28
|
+
email:
|
29
|
+
- rocky13670250811@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rubocop.yml"
|
36
|
+
- ".travis.yml"
|
37
|
+
- CODE_OF_CONDUCT.md
|
38
|
+
- Gemfile
|
39
|
+
- Gemfile.lock
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- bin/console
|
44
|
+
- bin/setup
|
45
|
+
- lib/lucky_param.rb
|
46
|
+
- lib/lucky_param/checker.rb
|
47
|
+
- lib/lucky_param/version.rb
|
48
|
+
- lucky_param.gemspec
|
49
|
+
homepage: https://github.com/shootingfly/lucky_param
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata: {}
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 2.7.6
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: Helper of parameter validation in controller
|
73
|
+
test_files: []
|