validate_japanese 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +72 -22
- data/lib/validate_japanese/japanese_validator.rb +5 -3
- data/lib/validate_japanese/version.rb +1 -1
- 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: 612abb78a67e99c387a4005dda1c80a3235ba9d21a05f55e4d6344631153b802
|
4
|
+
data.tar.gz: df1f5f73ef2a5840d92753cd2882f683918f66e492705f1c37251ba4d44a3c24
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc3bc136ec6a20c7dea4110733a9d0610333ff669ff6d8e647e4187eea87387cfecd014e2f25c52d09142088df9fe5c07cf2b20782501d3391c291c3f9025a9c
|
7
|
+
data.tar.gz: 349189e1e7710d4b3c71ca39b59c472a7e26d6b193ccb6df6cdc68d31055431eea5a934df1c712c3f5e96809646f426b80ad4dbedc150cd18a2d6c073662ec2e
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -13,43 +13,93 @@ Add this line to your application’s Gemfile:
|
|
13
13
|
gem 'validate_japanese'
|
14
14
|
```
|
15
15
|
|
16
|
-
Add a validation rule to your model
|
16
|
+
Add a validation rule to your model:
|
17
17
|
|
18
18
|
```ruby
|
19
|
-
|
19
|
+
class User < ApplicationRecord
|
20
|
+
validates :name, japanese: true
|
21
|
+
end
|
22
|
+
```
|
23
|
+
|
24
|
+
I18n error message:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
User.new(name: 'invalid name').tap(&:valid?)
|
28
|
+
# => Name に使える文字はひらがなカタカナ半角カナ漢字数字全角数字です。
|
20
29
|
```
|
21
30
|
|
22
31
|
## Usage
|
23
32
|
|
24
33
|
```ruby
|
25
|
-
|
26
|
-
|
27
|
-
validates :name, japanese: true
|
34
|
+
# hiragana katakana hankaku_kana kanji suji zenkaku_suji "ー-" + "-"
|
35
|
+
validates :name, japanese: true
|
28
36
|
|
29
|
-
|
30
|
-
|
37
|
+
user.errors.full_messages
|
38
|
+
# => Name に使える文字はひらがなカタカナ半角カナ漢字数字全角数字です。
|
39
|
+
```
|
31
40
|
|
32
|
-
|
33
|
-
|
41
|
+
```ruby
|
42
|
+
# hiragana "ぁ-ん" + "ー-"
|
43
|
+
validates :name, japanese: {hiragana: true}
|
34
44
|
|
35
|
-
|
36
|
-
|
45
|
+
user.errors.full_messages
|
46
|
+
# => Name に使える文字はひらがなです。
|
47
|
+
```
|
37
48
|
|
38
|
-
|
39
|
-
|
49
|
+
```ruby
|
50
|
+
# katakana "ァ-ン" + "ー-"
|
51
|
+
validates :name, japanese: {katakana: true}
|
40
52
|
|
41
|
-
|
42
|
-
|
53
|
+
user.errors.full_messages
|
54
|
+
# => Name に使える文字はカタカナです。
|
55
|
+
```
|
43
56
|
|
44
|
-
|
45
|
-
|
57
|
+
```ruby
|
58
|
+
# hankaku_kana "ァ-ン゙゚" + "-"
|
59
|
+
validates :name, japanese: {hankaku_kana: true}
|
46
60
|
|
47
|
-
|
48
|
-
|
61
|
+
user.errors.full_messages
|
62
|
+
# => Name に使える文字は半角カナです。
|
63
|
+
```
|
49
64
|
|
50
|
-
|
51
|
-
|
52
|
-
|
65
|
+
```ruby
|
66
|
+
# kanji "一-龠々"
|
67
|
+
validates :name, japanese: {kanji: true}
|
68
|
+
|
69
|
+
user.errors.full_messages
|
70
|
+
# => Name に使える文字は漢字です。
|
71
|
+
```
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
# suji "0-9"
|
75
|
+
validates :name, japanese: {suji: true}
|
76
|
+
|
77
|
+
user.errors.full_messages
|
78
|
+
# => Name に使える文字は数字です。
|
79
|
+
```
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
# zenkaku_suji "0-9"
|
83
|
+
validates :name, japanese: {zenkaku_suji: true}
|
84
|
+
|
85
|
+
user.errors.full_messages
|
86
|
+
# => Name に使える文字は全角数字です。
|
87
|
+
```
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
# hiragana katakana "ぁ-ん" + "ァ-ン" + "ー-"
|
91
|
+
validates :name, japanese: {only: %i(hiragana katakana)}
|
92
|
+
|
93
|
+
user.errors.full_messages
|
94
|
+
# => Name に使える文字はひらがなカタカナです。
|
95
|
+
```
|
96
|
+
|
97
|
+
```ruby
|
98
|
+
# japanese "、。"
|
99
|
+
validates :name, japanese: {concat: '、。'}
|
100
|
+
|
101
|
+
user.errors.full_messages
|
102
|
+
# => Name に使える文字はひらがなカタカナ半角カナ漢字数字全角数字、。です。
|
53
103
|
```
|
54
104
|
|
55
105
|
## Implementation
|
@@ -23,7 +23,7 @@ module ActiveModel
|
|
23
23
|
if options[:message]
|
24
24
|
record.errors.add(attribute, options[:message])
|
25
25
|
else
|
26
|
-
record.errors.add(attribute, :invalid_japanese, kind: available_kind(keys))
|
26
|
+
record.errors.add(attribute, :invalid_japanese, kind: available_kind(keys, concat: options[:concat]))
|
27
27
|
end
|
28
28
|
end
|
29
29
|
end
|
@@ -65,8 +65,10 @@ module ActiveModel
|
|
65
65
|
self.class.const_get(name.upcase)
|
66
66
|
end
|
67
67
|
|
68
|
-
def available_kind(keys, sep
|
69
|
-
keys.reject {|k| %i(choonpu hankaku_choonpu).include?(k)}.map {|k| I18n.t("validate_japanese.#{k}")}.join(sep)
|
68
|
+
def available_kind(keys, concat: nil, sep: '')
|
69
|
+
kind = keys.reject {|k| %i(choonpu hankaku_choonpu).include?(k)}.map {|k| I18n.t("validate_japanese.#{k}")}.join(sep)
|
70
|
+
kind += concat if concat
|
71
|
+
kind
|
70
72
|
end
|
71
73
|
end
|
72
74
|
end
|