lucky_param 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +2 -2
- data/README.md +21 -9
- data/lib/lucky_param/checker.rb +7 -7
- data/lib/lucky_param/version.rb +1 -1
- data/lib/lucky_param.rb +27 -23
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ccec36d0fcd976004cc8464dde95eaa07c28addad21077dde9081585c53d38ef
|
4
|
+
data.tar.gz: 98a62a979a11ef8f2c35c2963cace3a0e580baf834b22a3ba83049eb3e7f34e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f5ae2f511f00e5198f52832e8c9d4fb0e1095ad9fd5334924a3c11c4e208acf7a88bd780eb1843ad4d26478b6c69b755eec0b68472c47b87578704c15e39e0c0
|
7
|
+
data.tar.gz: 6c6c7d91ae8c226b796329d659b1d28dc0a9970087c7fc58b0b4f2a3ace1a6bad8f9654ead328ca0df665ae7d0a2b177d8cb1b61796689c76241a498bf794361
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
lucky_param (0.1.
|
4
|
+
lucky_param (0.1.1)
|
5
5
|
rails (>= 4.2)
|
6
6
|
|
7
7
|
GEM
|
@@ -99,7 +99,7 @@ GEM
|
|
99
99
|
method_source
|
100
100
|
rake (>= 0.8.7)
|
101
101
|
thor (>= 0.19.0, < 2.0)
|
102
|
-
rake (
|
102
|
+
rake (12.3.1)
|
103
103
|
sprockets (3.7.2)
|
104
104
|
concurrent-ruby (~> 1.0)
|
105
105
|
rack (> 1, < 3)
|
data/README.md
CHANGED
@@ -2,28 +2,25 @@
|
|
2
2
|
|
3
3
|
[![Build Status](https://travis-ci.org/shootingfly/lucky_param.svg?branch=master)](https://travis-ci.org/shootingfly/lucky_param)
|
4
4
|
|
5
|
-
A simple parameter
|
5
|
+
A simple parameter checker in controller for Rails Api.
|
6
6
|
|
7
|
-
|
7
|
+
### Do Check, Fail Fast
|
8
8
|
|
9
9
|
## Usage
|
10
10
|
|
11
|
+
Gemfile
|
11
12
|
```rb
|
12
13
|
gem "lucky_param"
|
13
14
|
```
|
14
15
|
|
15
|
-
### Define any validator for your app:
|
16
16
|
app/controllers/application_controller.rb
|
17
17
|
|
18
18
|
```rb
|
19
|
-
class
|
19
|
+
class ApplicationController
|
20
20
|
include LuckyParam
|
21
21
|
|
22
|
+
# If necessary, you can overide or define your own parameter checkers.
|
22
23
|
LuckyParam::CUSTOM_CHECKER = {
|
23
|
-
Email: [
|
24
|
-
->(obj) { obj =~ /([0-9a-zA-Z]){6,30}/ },
|
25
|
-
"must be valid email"
|
26
|
-
],
|
27
24
|
Password: [
|
28
25
|
->(obj) { obj =~ /([0-9a-zA-Z]){6,30}/ },
|
29
26
|
"must be valid password"
|
@@ -35,15 +32,30 @@ end
|
|
35
32
|
app/controllers/sessions_controller.rb
|
36
33
|
|
37
34
|
```rb
|
38
|
-
class
|
35
|
+
class SessionsController < ApplicationController
|
39
36
|
def create
|
40
37
|
required :email, :Email
|
41
38
|
required :password, :Password
|
39
|
+
optional :nick_name, :String
|
42
40
|
render json: 'ok'
|
43
41
|
end
|
44
42
|
end
|
45
43
|
```
|
46
44
|
|
45
|
+
## Internal Checkers
|
46
|
+
|
47
|
+
[Checkers](https://github.com/shootingfly/lucky_param/blob/master/lib/lucky_param/checker.rb)
|
48
|
+
|
49
|
+
## Contributing
|
50
|
+
|
51
|
+
1. Fork it ( https://github.com/shootingfly/lucky_param/fork )
|
52
|
+
2. Create your feature branch (git checkout -b my-new-feature)
|
53
|
+
3. Make your changes
|
54
|
+
4. Run `ruby test/lucky_param_test.rb` to run the tests
|
55
|
+
5. Commit your changes (git commit -am 'Add some feature')
|
56
|
+
6. Push to the branch (git push origin my-new-feature)
|
57
|
+
7. Create a new Pull Request
|
58
|
+
|
47
59
|
## License
|
48
60
|
|
49
61
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/lucky_param/checker.rb
CHANGED
@@ -6,21 +6,21 @@ module LuckyParam
|
|
6
6
|
->(_obj) { true }
|
7
7
|
],
|
8
8
|
String: [
|
9
|
-
->(obj) { obj
|
9
|
+
->(obj) { !obj.empty? },
|
10
10
|
"must be valid String"
|
11
11
|
],
|
12
12
|
Integer: [
|
13
|
-
->(obj) { obj
|
13
|
+
->(obj) { obj.to_i.to_s == obj },
|
14
14
|
"must be valid Integer"
|
15
15
|
],
|
16
|
-
Email: [
|
17
|
-
->(obj) { obj =~ /\A[^@\s]+@([^@\s]+\.)+[^@\s]+\z/ },
|
18
|
-
"must be valid Email"
|
19
|
-
],
|
20
16
|
Float: [
|
21
|
-
->(obj) { obj
|
17
|
+
->(obj) { obj.to_f.to_s == obj },
|
22
18
|
"must be valid Float"
|
23
19
|
],
|
20
|
+
Email: [
|
21
|
+
->(obj) { obj =~ /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/ },
|
22
|
+
"must be valid Email"
|
23
|
+
],
|
24
24
|
Timestamp: [
|
25
25
|
->(obj) { obj =~ /^(\+|\-)?\d+$/ },
|
26
26
|
"must be valid Timestamp"
|
data/lib/lucky_param/version.rb
CHANGED
data/lib/lucky_param.rb
CHANGED
@@ -5,37 +5,41 @@ require_relative "./lucky_param/checker"
|
|
5
5
|
|
6
6
|
module LuckyParam
|
7
7
|
class ParamMissError < StandardError
|
8
|
-
def initialize(column)
|
9
|
-
super "Missing Params: #{column}"
|
10
|
-
end
|
11
8
|
end
|
12
9
|
|
13
10
|
class ParamFormatError < StandardError
|
14
|
-
def initialize(message)
|
15
|
-
super "Wrong Params Format: #{message}"
|
16
|
-
end
|
17
11
|
end
|
18
12
|
|
19
|
-
|
20
|
-
raise ParamMissError, column unless params.key?(column)
|
21
|
-
|
22
|
-
message = _checker_message(column, checker_type)
|
23
|
-
raise ParamFormatError, message if message
|
13
|
+
class UnknownCheckerError < StandardError
|
24
14
|
end
|
25
15
|
|
26
|
-
def
|
27
|
-
|
28
|
-
|
29
|
-
message = _checker_message(column, checker_type)
|
30
|
-
raise ParamFormatError, message if message
|
16
|
+
def required(column, checker_type)
|
17
|
+
required_optional(:required, column, checker_type)
|
31
18
|
end
|
32
19
|
|
33
|
-
def
|
34
|
-
|
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]
|
20
|
+
def optional(column, checker_type)
|
21
|
+
required_optional(:optional, column, checker_type)
|
40
22
|
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def required_optional(type, column, checker_type)
|
27
|
+
unless params[column]
|
28
|
+
return if type == :optional
|
29
|
+
raise ParamMissError, "Missing Params: #{column}"
|
30
|
+
end
|
31
|
+
message = checker_message(column, checker_type)
|
32
|
+
if message
|
33
|
+
raise ParamFormatError, "Wrong Params Format: #{message}"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def checker_message(column, checker_type)
|
38
|
+
checker = CUSTOM_CHECKER[checker_type] if LuckyParam.const_defined?(:CUSTOM_CHECKER)
|
39
|
+
checker ||= CHECKER.fetch(checker_type) {
|
40
|
+
raise UnknownCheckerError, "Unknown checker `#{checker_type}`, try to define checker with const `LuckyParam::CUSTOM_CHECKER`"
|
41
|
+
}
|
42
|
+
result = checker[0].call(params[column])
|
43
|
+
result ? nil : "#{column} #{checker[1]}"
|
44
|
+
end
|
41
45
|
end
|