moca_rlibs 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +21 -2
- data/lib/moca_rlibs/active_model_validators/ip.rb +28 -0
- data/lib/moca_rlibs/grape_validators/ip_address.rb +36 -0
- data/lib/moca_rlibs/version.rb +1 -1
- data/lib/moca_rlibs.rb +2 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b147078e15c0cb13012214d313ec93af268b85de8100dcfebae040faaa6cd99
|
4
|
+
data.tar.gz: d29bd7c7bef4f739e0db863f593091e47589ae3863644e285a3d8b65b59442c5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a71797fc5bf75331985f97c18a1cb9f62761289ce279434412144d92599d4263f53626857afe7eb1aa1a00d9b2db94bd909e005b8b24f56fe452193b1cfa5e0c
|
7
|
+
data.tar.gz: 11f571d96e4a6faeaa0d3a4923ea30db99c970fe35d98f4154d06aab3c10c898defc283819383c72a125e6aa8cbfa0d68a517375f0407985b3bcf8ad3271c215
|
data/README.md
CHANGED
@@ -66,8 +66,18 @@ class UserModel
|
|
66
66
|
include ActiveModel::Validations
|
67
67
|
|
68
68
|
validates :user_email, email: true
|
69
|
-
validates :user_email, email: {mode: :strict, require_fqdn: true}
|
70
|
-
validates :user_email, email: {domain: 'example.com'}
|
69
|
+
validates :user_email, email: { mode: :strict, require_fqdn: true }
|
70
|
+
validates :user_email, email: { domain: 'example.com' }
|
71
|
+
end
|
72
|
+
```
|
73
|
+
IPアドレスのフォーマットをチェックする
|
74
|
+
```ruby
|
75
|
+
class UserModel
|
76
|
+
include ActiveModel::Validations
|
77
|
+
|
78
|
+
validates :last_login_ip, ip: true
|
79
|
+
validates :last_login_ip, ip: { version: :v4 }
|
80
|
+
validates :last_login_ip, ip: { version: :v6 }
|
71
81
|
end
|
72
82
|
```
|
73
83
|
|
@@ -171,6 +181,15 @@ params do
|
|
171
181
|
requires :text, type: String, only_printable: true
|
172
182
|
end
|
173
183
|
```
|
184
|
+
IPアドレスのフォーマットをチェックする
|
185
|
+
```ruby
|
186
|
+
params do
|
187
|
+
optional :ip, type: String, ip_address: true
|
188
|
+
optional :ipv4, type: String, ip_address: :v4
|
189
|
+
optional :ipv6, type: String, ip_address: :v6
|
190
|
+
end
|
191
|
+
```
|
192
|
+
|
174
193
|
## Slack通知送信用クラス
|
175
194
|
```ruby
|
176
195
|
# インスタンス化
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_model'
|
4
|
+
require 'active_support/all'
|
5
|
+
require 'resolv'
|
6
|
+
|
7
|
+
module ActiveModel::Validations
|
8
|
+
# IPアドレスのフォーマットをチェックする
|
9
|
+
class IpValidator < ::ActiveModel::EachValidator
|
10
|
+
PERMITTED_VERSION = %i[v4 v6].freeze
|
11
|
+
|
12
|
+
def initialize(options)
|
13
|
+
options[:version] ||= %i[v4 v6]
|
14
|
+
options[:version] = Array(options[:version])
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
def validate_each(record, attribute, value)
|
19
|
+
return if value.blank?
|
20
|
+
|
21
|
+
value = value.presence.to_s
|
22
|
+
return if options[:version].include?(:v4) && ::Resolv::IPv4::Regex.match?(value)
|
23
|
+
return if options[:version].include?(:v6) && ::Resolv::IPv6::Regex.match?(value)
|
24
|
+
|
25
|
+
record.errors[attribute] << 'invalid ip format'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/all'
|
4
|
+
require 'grape'
|
5
|
+
require 'resolv'
|
6
|
+
|
7
|
+
module MocaRlibs::GrapeValidators
|
8
|
+
# 数値の最小値をチェック
|
9
|
+
class IpAddress < Grape::Validations::Base
|
10
|
+
def validate_param!(attr_name, params)
|
11
|
+
return if !@required && params[attr_name].blank?
|
12
|
+
|
13
|
+
version = @option.instance_of?(Proc) ? @option.call(params) : @option
|
14
|
+
value = params[attr_name].presence.to_s
|
15
|
+
return if validate_ip(version, value)
|
16
|
+
|
17
|
+
message = "invalid ip format: #{value}"
|
18
|
+
raise Grape::Exceptions::Validation.new(params: [@scope.full_name(attr_name)], message: message)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def validate_ip(version, value)
|
24
|
+
case version
|
25
|
+
when :v4
|
26
|
+
return true if ::Resolv::IPv4::Regex.match?(value)
|
27
|
+
when :v6
|
28
|
+
return true if ::Resolv::IPv6::Regex.match?(value)
|
29
|
+
else
|
30
|
+
return true if ::Resolv::IPv4::Regex.match?(value)
|
31
|
+
return true if ::Resolv::IPv6::Regex.match?(value)
|
32
|
+
end
|
33
|
+
false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/moca_rlibs/version.rb
CHANGED
data/lib/moca_rlibs.rb
CHANGED
@@ -19,6 +19,7 @@ require_relative 'moca_rlibs/grape_validators/only_numbers'
|
|
19
19
|
require_relative 'moca_rlibs/grape_validators/only_alpha'
|
20
20
|
require_relative 'moca_rlibs/grape_validators/only_alpha_numeric'
|
21
21
|
require_relative 'moca_rlibs/grape_validators/only_printable'
|
22
|
+
require_relative 'moca_rlibs/grape_validators/ip_address'
|
22
23
|
require_relative 'moca_rlibs/grape_errors/bad_gateway'
|
23
24
|
require_relative 'moca_rlibs/grape_errors/bad_request'
|
24
25
|
require_relative 'moca_rlibs/grape_errors/conflict'
|
@@ -36,6 +37,7 @@ require_relative 'moca_rlibs/active_model_validators/only_alpha_numeric'
|
|
36
37
|
require_relative 'moca_rlibs/active_model_validators/only_ascii'
|
37
38
|
require_relative 'moca_rlibs/active_model_validators/only_numbers'
|
38
39
|
require_relative 'moca_rlibs/active_model_validators/only_printable'
|
40
|
+
require_relative 'moca_rlibs/active_model_validators/ip'
|
39
41
|
|
40
42
|
require 'active_model'
|
41
43
|
require 'email_validator'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: moca_rlibs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- mocaberos
|
@@ -277,6 +277,7 @@ files:
|
|
277
277
|
- buildspec.yml
|
278
278
|
- docker/scripts/test.sh
|
279
279
|
- lib/moca_rlibs.rb
|
280
|
+
- lib/moca_rlibs/active_model_validators/ip.rb
|
280
281
|
- lib/moca_rlibs/active_model_validators/only_alpha.rb
|
281
282
|
- lib/moca_rlibs/active_model_validators/only_alpha_numeric.rb
|
282
283
|
- lib/moca_rlibs/active_model_validators/only_ascii.rb
|
@@ -300,6 +301,7 @@ files:
|
|
300
301
|
- lib/moca_rlibs/grape_validators/email_rfc.rb
|
301
302
|
- lib/moca_rlibs/grape_validators/email_strict.rb
|
302
303
|
- lib/moca_rlibs/grape_validators/ends_with.rb
|
304
|
+
- lib/moca_rlibs/grape_validators/ip_address.rb
|
303
305
|
- lib/moca_rlibs/grape_validators/length.rb
|
304
306
|
- lib/moca_rlibs/grape_validators/max.rb
|
305
307
|
- lib/moca_rlibs/grape_validators/min.rb
|