uuid_validator 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,12 +1,25 @@
|
|
1
|
+
require 'active_model'
|
1
2
|
require 'active_model/validator'
|
2
3
|
|
3
4
|
module ActiveModel
|
4
5
|
module Validations
|
5
6
|
class UuidValidator < ActiveModel::EachValidator
|
6
|
-
|
7
|
+
RE_STRING = "[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}"
|
8
|
+
|
9
|
+
def initialize(options)
|
10
|
+
super
|
11
|
+
@re = case
|
12
|
+
when options[:upper_only]
|
13
|
+
/\A#{RE_STRING.upcase}\z/
|
14
|
+
when options[:lower_only]
|
15
|
+
/\A#{RE_STRING.downcase}\z/
|
16
|
+
else
|
17
|
+
/\A#{RE_STRING}\z/i
|
18
|
+
end
|
19
|
+
end
|
7
20
|
|
8
21
|
def validate_each(record, attribute, value)
|
9
|
-
record.errors.add(attribute, :not_an_uuid) unless value =~
|
22
|
+
record.errors.add(attribute, :not_an_uuid) unless value =~ @re
|
10
23
|
end
|
11
24
|
|
12
25
|
end
|
data/test/uuid_validator_test.rb
CHANGED
@@ -9,6 +9,17 @@ describe ActiveModel::Validations::UuidValidator do
|
|
9
9
|
I18n.locale = 'en'
|
10
10
|
end
|
11
11
|
|
12
|
+
def must_be_valid(key)
|
13
|
+
TestModel.new(key: key).valid?.must_equal true
|
14
|
+
end
|
15
|
+
|
16
|
+
def must_be_error(key, error = 'is not an uuid')
|
17
|
+
model = TestModel.new(key: key)
|
18
|
+
model.valid?.must_equal false
|
19
|
+
model.errors[:key].must_equal [error]
|
20
|
+
end
|
21
|
+
|
22
|
+
|
12
23
|
describe "validates :key, :uuid => true" do
|
13
24
|
before do
|
14
25
|
TestModel.validates :key, :uuid => true
|
@@ -16,69 +27,111 @@ describe ActiveModel::Validations::UuidValidator do
|
|
16
27
|
|
17
28
|
describe "passes for wikipedia sample" do
|
18
29
|
it "is valid" do
|
19
|
-
|
20
|
-
model = TestModel.new(key: key)
|
21
|
-
model.valid?.must_equal true
|
30
|
+
must_be_valid "550e8400-e29b-41d4-a716-446655440000"
|
22
31
|
end
|
23
32
|
end
|
24
33
|
|
25
34
|
describe "test for SecureRandom.uuid" do
|
26
35
|
it "is valid" do
|
27
|
-
|
28
|
-
model.valid?.must_equal true
|
36
|
+
must_be_valid SecureRandom.uuid
|
29
37
|
end
|
30
38
|
end
|
31
39
|
|
32
40
|
describe "test for UUIDTools" do
|
33
41
|
it "is valid for md5_create" do
|
34
|
-
|
35
|
-
model = TestModel.new(key: key)
|
36
|
-
model.valid?.must_equal true
|
42
|
+
must_be_valid UUIDTools::UUID.md5_create(UUIDTools::UUID_DNS_NAMESPACE, "www.example.com").to_s
|
37
43
|
end
|
38
44
|
|
39
45
|
it "is valid for sha1_create" do
|
40
|
-
|
41
|
-
model = TestModel.new(key: key)
|
42
|
-
model.valid?.must_equal true
|
46
|
+
must_be_valid UUIDTools::UUID.sha1_create(UUIDTools::UUID_DNS_NAMESPACE, "www.example.com").to_s
|
43
47
|
end
|
44
48
|
|
45
49
|
it "is valid for timestamp_create" do
|
46
|
-
|
47
|
-
model = TestModel.new(key: key)
|
48
|
-
model.valid?.must_equal true
|
50
|
+
must_be_valid UUIDTools::UUID.timestamp_create.to_s
|
49
51
|
end
|
50
52
|
|
51
53
|
it "is valid for random_create" do
|
52
|
-
|
53
|
-
model = TestModel.new(key: key)
|
54
|
-
model.valid?.must_equal true
|
54
|
+
must_be_valid UUIDTools::UUID.random_create.to_s
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
-
def error_in(model)
|
59
|
-
model.valid?.must_equal false
|
60
|
-
model.errors[:key].must_equal ["is not an uuid"]
|
61
|
-
end
|
62
|
-
|
63
58
|
describe "when nil" do
|
64
59
|
it "is invalid" do
|
65
|
-
|
60
|
+
must_be_error nil
|
66
61
|
end
|
67
62
|
end
|
68
63
|
|
69
64
|
describe "when empty string" do
|
70
65
|
it "is invalid" do
|
71
|
-
|
66
|
+
must_be_error ''
|
72
67
|
end
|
73
68
|
end
|
74
69
|
|
75
70
|
describe "when non number" do
|
76
71
|
it "is invalid" do
|
77
|
-
|
78
|
-
|
72
|
+
must_be_error "550e8400-e29b-41d4-a716-XXXXXXXXXXXX"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "validates :key, :uuid => {lower_only: true}" do
|
78
|
+
before do
|
79
|
+
TestModel.validates :key, :uuid => {lower_only: true}
|
80
|
+
end
|
81
|
+
|
82
|
+
describe "passes for wikipedia sample" do
|
83
|
+
it "is valid" do
|
84
|
+
must_be_valid "550e8400-e29b-41d4-a716-446655440000"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
describe "SecureRandom.uuid" do
|
89
|
+
it "is valid" do
|
90
|
+
must_be_valid SecureRandom.uuid
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
describe "SecureRandom.uuid.upcase" do
|
95
|
+
it "is invalid" do
|
96
|
+
must_be_error SecureRandom.uuid.upcase
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe "when nil" do
|
101
|
+
it "is invalid" do
|
102
|
+
must_be_error nil
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
describe "validates :key, :uuid => {upper_only: true}" do
|
108
|
+
before do
|
109
|
+
TestModel.validates :key, :uuid => {upper_only: true}
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "passes for wikipedia sample" do
|
113
|
+
it "is invalid" do
|
114
|
+
must_be_error "550e8400-e29b-41d4-a716-446655440000"
|
79
115
|
end
|
80
116
|
end
|
81
117
|
|
118
|
+
describe "SecureRandom.uuid" do
|
119
|
+
it "is invalid" do
|
120
|
+
must_be_error SecureRandom.uuid
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe "SecureRandom.uuid.upcase" do
|
125
|
+
it "is valid" do
|
126
|
+
must_be_valid SecureRandom.uuid.upcase
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
describe "when nil" do
|
131
|
+
it "is invalid" do
|
132
|
+
must_be_error nil
|
133
|
+
end
|
134
|
+
end
|
82
135
|
end
|
83
136
|
end
|
84
137
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uuid_validator
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-02-06 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
@@ -93,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
93
|
version: '0'
|
94
94
|
segments:
|
95
95
|
- 0
|
96
|
-
hash:
|
96
|
+
hash: -2748658622935870302
|
97
97
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
98
|
none: false
|
99
99
|
requirements:
|
@@ -102,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
102
|
version: '0'
|
103
103
|
segments:
|
104
104
|
- 0
|
105
|
-
hash:
|
105
|
+
hash: -2748658622935870302
|
106
106
|
requirements: []
|
107
107
|
rubyforge_project:
|
108
108
|
rubygems_version: 1.8.24
|