data-validator 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,96 @@
1
+ require 'test-unit'
2
+ require 'data/validator'
3
+
4
+ class Test004Xor < Test::Unit::TestCase
5
+
6
+ def test_test
7
+ v = Data::Validator.new(
8
+ 'uri' => { xor: %w(schema host path_query) },
9
+
10
+ 'schema' => { default: 'http' },
11
+ 'host' => { default: '127.0.0.1' },
12
+ 'path_query' => { default: '/' },
13
+
14
+ 'method' => { default: 'GET' },
15
+ )
16
+
17
+ # note 'success cases';
18
+
19
+ args = v.validate({ 'uri' => 'https://example.com/' })
20
+ assert { args == { 'uri' => 'https://example.com/', 'method' => 'GET' } }
21
+
22
+ args = v.validate({
23
+ 'schema' => 'https',
24
+ 'host' => 'example.com',
25
+ 'path_query' => '/index.html',
26
+ })
27
+ assert_equal args, {
28
+ 'schema' => 'https',
29
+ 'host' => 'example.com',
30
+ 'path_query' => '/index.html',
31
+ 'method' => 'GET',
32
+ }
33
+
34
+ args = v.validate({
35
+ 'host' => 'example.com',
36
+ })
37
+ assert_equal args, {
38
+ 'schema' => 'http',
39
+ 'host' => 'example.com',
40
+ 'path_query' => '/',
41
+ 'method' => 'GET',
42
+ }
43
+
44
+ args = v.validate();
45
+ assert_equal args, {
46
+ 'schema' => 'http',
47
+ 'host' => '127.0.0.1',
48
+ 'path_query' => '/',
49
+ 'method' => 'GET',
50
+ }
51
+
52
+ # note 'failure cases';
53
+
54
+ assert_raise_message(/uri versus.+schema/) {
55
+ v.validate({ 'uri' => 'foo', 'schema' => 'http' })
56
+ }
57
+
58
+ assert_raise_message(/uri versus.+(host|schema)/) {
59
+ v.validate({ 'uri' => 'foo', 'schema' => 'http', 'host' => 'example.com' })
60
+ }
61
+
62
+ # note 'case without defaults';
63
+
64
+ v = Data::Validator.new(
65
+ 'uri' => { xor: %w(schema host path_query) },
66
+
67
+ 'schema' => { default: 'http' },
68
+ 'host' => { },
69
+ 'path_query' => { default: '/' },
70
+
71
+ 'method' => { default: 'GET' },
72
+ )
73
+
74
+ args = v.validate(
75
+ 'uri' => 'http://example.com/',
76
+ )
77
+ assert_equal args, {
78
+ 'uri' => 'http://example.com/',
79
+ 'method' => 'GET',
80
+ }
81
+
82
+ args = v.validate(
83
+ 'host' => 'example.com',
84
+ )
85
+ assert_equal args, {
86
+ 'schema' => 'http',
87
+ 'host' => 'example.com',
88
+ 'path_query' => '/',
89
+ 'method' => 'GET',
90
+ }
91
+
92
+ assert_raise_message(/(uri|host)/) {
93
+ v.validate()
94
+ }
95
+ end
96
+ end
@@ -0,0 +1,27 @@
1
+ require 'test-unit'
2
+ require 'data/validator'
3
+
4
+ class Test008Optional < Test::Unit::TestCase
5
+
6
+ def test_test
7
+ v = Data::Validator.new(
8
+ 'foo' => { },
9
+ 'bar' => { },
10
+ 'baz' => { optional => 1 }
11
+ )
12
+ asset { v.is_a? Data::Validator }
13
+
14
+ args = v.validate({ 'foo' => 1, 'bar' => 2 })
15
+ assert { args == { 'foo' => 1, 'bar' => 2 } }
16
+
17
+ args = v.validate({ 'foo' => 1, 'bar' => 2, 'baz' => 3 })
18
+ assert { args == { 'foo' => 1, 'bar' => 2, 'baz' => 3 } }
19
+
20
+ # note 'failing cases';
21
+
22
+ assert_raise_message(/foo|bar/) { v.validate() }
23
+ assert_raise_message(/foo|bar/) { v.validate('baz' => 1) }
24
+ assert_raise_message(/bar/) { v.validate('foo' => 1) }
25
+ assert_raise_message(/foo/) { v.validate('bar' => 1) }
26
+ end
27
+ end
@@ -0,0 +1,18 @@
1
+ require 'test-unit'
2
+ require 'data/validator'
3
+
4
+ class Test010CommonParams < Test::Unit::TestCase
5
+
6
+ def test_test
7
+ base_param = {
8
+ 'member_id' => { isa: Integer },
9
+ 'type' => { isa: Integer, default: 0, optional: true },
10
+ }
11
+
12
+ foo = Data::Validator.new(base_param)
13
+ bar = Data::Validator.new(base_param)
14
+
15
+ assert_nothing_raised { foo.validate('member_id' => 10) }
16
+ assert_nothing_raised { bar.validate('member_id' => 10) }
17
+ end
18
+ end
@@ -0,0 +1,17 @@
1
+ require 'test-unit'
2
+ require 'data/validator'
3
+
4
+ class Test104ExtraArgs < Test::Unit::TestCase
5
+
6
+ def test_test
7
+ v = Data::Validator.new(
8
+ 'foo' => { },
9
+ ).with('AllowExtra')
10
+
11
+ args = v.validate('foo' => 42, 'bar' => 15)
12
+ assert { args == {'foo' => 42, 'bar' => 15} }
13
+
14
+ args = v.validate('bar' => 15, 'foo' => 42) # reversed order
15
+ assert { args == {'foo' => 42, 'bar' => 15} }
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ require 'test-unit'
2
+
3
+ class Test00Compile < Test::Unit::TestCase
4
+ def test_require
5
+ assert_nothing_raised do
6
+ require 'data/validator'
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,46 @@
1
+ require 'test-unit'
2
+ require 'data/validator'
3
+
4
+ class Test01New < Test::Unit::TestCase
5
+ test 'no args' do
6
+ assert_raise ArgumentError do
7
+ Data::Validator.new
8
+ end
9
+ end
10
+
11
+ test 'simple' do
12
+ rule = Data::Validator::Recursive.new(
13
+ 'foo' => String
14
+ )
15
+
16
+ assert { rule.is_a? Data::Validator }
17
+ end
18
+
19
+ test 'nested' do
20
+ rule = Data::Validator::Recursive.new(
21
+ 'foo' => String,
22
+ 'bar' => {
23
+ isa: Integer,
24
+ rule: {
25
+ 'baz' => String,
26
+ }
27
+ },
28
+ )
29
+
30
+ assert { rule.is_a? Data::Validator }
31
+ end
32
+
33
+ # test 'nested (hash)' # is perl specific
34
+
35
+ test 'invalid nested rule' do
36
+ assert_raise TypeError do
37
+ Data::Validator::Recursive.new(
38
+ 'foo' => String,
39
+ 'bar' => {
40
+ isa: Integer,
41
+ rule: 'invalid'
42
+ },
43
+ )
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,159 @@
1
+ require 'test-unit'
2
+ require 'data/validator'
3
+
4
+ class Test02Validate < Test::Unit::TestCase
5
+
6
+ setup do
7
+ @rule = Data::Validator::Recursive.new(
8
+ 'foo' => String,
9
+ 'bar' => { isa: Integer, optional: 1 },
10
+ 'baz' => {
11
+ isa: Hash,
12
+ rule: {
13
+ 'hoge' => String,
14
+ 'fuga' => Integer,
15
+ 'piyo' => {
16
+ isa: Array,
17
+ xor: %w/hoge/,
18
+ optional: true,
19
+ },
20
+ },
21
+ },
22
+ )
23
+ end
24
+
25
+ test 'valid data' do
26
+ input = {
27
+ 'foo' => 'xxx',
28
+ 'bar' => 123,
29
+ 'baz' => {
30
+ 'hoge' => 'xxx',
31
+ 'fuga' => 123,
32
+ },
33
+ }
34
+ params = @rule.validate(input)
35
+
36
+ assert { params == input }
37
+ end
38
+
39
+ test 'invalid data' do
40
+ input = {
41
+ 'foo' => 'xxx',
42
+ 'bar' => 123,
43
+ 'baz' => {
44
+ 'fuga' => 'piyo'
45
+ },
46
+ }
47
+ assert_raise_message(/hoge|fuga/) { @rule.validate(input) }
48
+ end
49
+
50
+ test 'conflicts' do
51
+ input = {
52
+ 'foo' => 'xxx',
53
+ 'bar' => 123,
54
+ 'baz' => {
55
+ 'hoge' => 'yyy',
56
+ 'fuga' => 456,
57
+ 'piyo' => %w/a b c/,
58
+ },
59
+ }
60
+ assert_raise_message(/hoge|piyo/) { @rule.validate(input) }
61
+ end
62
+
63
+ test 'with default option' do
64
+ rule = Data::Validator::Recursive.new(
65
+ 'foo' => String,
66
+ 'bar' => { isa: Integer, default: 1 },
67
+ 'baz' => {
68
+ isa: Hash,
69
+ rule: {
70
+ 'hoge' => String,
71
+ 'fuga' => Integer,
72
+ },
73
+ },
74
+ )
75
+
76
+ input = {
77
+ 'foo' => 'xxx',
78
+ 'baz' => {
79
+ 'hoge' => 'xxx',
80
+ 'fuga' => 123,
81
+ },
82
+ }
83
+
84
+ params = rule.validate(input)
85
+
86
+ assert { params['bar'] == 1 }
87
+ end
88
+
89
+ test 'default option with nested' do
90
+ rule = Data::Validator::Recursive.new(
91
+ 'foo' => String,
92
+ 'bar' => { isa: Integer, default: 1 },
93
+ 'baz' => {
94
+ isa: Hash,
95
+ rule: {
96
+ 'hoge' => { isa: String, default: 'yyy' },
97
+ 'fuga' => Integer,
98
+ },
99
+ },
100
+ )
101
+
102
+ input = {
103
+ 'foo' => 'xxx',
104
+ 'baz' => {
105
+ 'fuga' => 123,
106
+ },
107
+ }
108
+
109
+ params = rule.validate(input)
110
+ expected = {
111
+ 'foo' => 'xxx',
112
+ 'bar' => 1,
113
+ 'baz' => {
114
+ 'hoge' => 'yyy',
115
+ 'fuga' => 123,
116
+ },
117
+ }
118
+ assert { params == expected }
119
+ end
120
+
121
+ test 'with AllowExtra' do
122
+ rule = Data::Validator::Recursive.new(
123
+ 'foo' => String,
124
+ 'bar' => { isa: Integer, default: 1 },
125
+ 'baz' => {
126
+ isa: Hash,
127
+ with: 'AllowExtra',
128
+ rule: {
129
+ 'hoge' => { isa: String, default: 'yyy' },
130
+ 'fuga' => Integer
131
+ },
132
+ },
133
+ ).with('AllowExtra')
134
+
135
+
136
+ input = {
137
+ 'foo' => 'xxx',
138
+ 'baz' => {
139
+ 'fuga' => 123,
140
+ 'extra_param_in_baz' => 1,
141
+ },
142
+ 'extra_param' => 1,
143
+ }
144
+
145
+ params = rule.validate(input)
146
+ expected = {
147
+ 'foo' => 'xxx',
148
+ 'bar' => 1,
149
+ 'baz' => {
150
+ 'hoge' => 'yyy',
151
+ 'fuga' => 123,
152
+ 'extra_param_in_baz' => 1,
153
+ },
154
+ 'extra_param' => 1,
155
+ }
156
+
157
+ assert { params == expected }
158
+ end
159
+ end
@@ -0,0 +1,268 @@
1
+ require 'test-unit'
2
+ require 'data/validator'
3
+
4
+ # There is no such thing like an arrayref in Ruby. But here I follow the
5
+ # original naming anyway.
6
+ class Test03Arrayref < Test::Unit::TestCase
7
+
8
+ setup do
9
+ @rule = Data::Validator::Recursive.new(
10
+ 'foo' => String,
11
+ 'bar' => { isa: Integer, optional: 1 },
12
+ 'baz' => {
13
+ isa: Array,
14
+ rule: {
15
+ 'hoge' => String,
16
+ 'fuga' => Integer,
17
+ 'piyo' => {
18
+ isa: Array,
19
+ xor: %w/hoge/,
20
+ optional: true,
21
+ },
22
+ },
23
+ },
24
+ )
25
+ end
26
+
27
+ test 'valid data' do
28
+ input = {
29
+ 'foo' => 'xxx',
30
+ 'bar' => 123,
31
+ 'baz' => [
32
+ {
33
+ 'hoge' => 'xxx',
34
+ 'fuga' => 123,
35
+ },
36
+ ],
37
+ }
38
+ params = @rule.validate(input)
39
+
40
+ assert { params == input }
41
+ end
42
+
43
+ test 'invalid data as array' do
44
+ input = {
45
+ 'foo' => 'xxx',
46
+ 'bar' => 123,
47
+ 'baz' => {},
48
+ }
49
+ assert_raise_message(/baz/) { @rule.validate(input) }
50
+ end
51
+
52
+ test 'invalid data at the first of array' do
53
+ input = {
54
+ 'foo' => 'xxx',
55
+ 'bar' => 123,
56
+ 'baz' => [
57
+ {
58
+ 'fuga' => 'piyo',
59
+ },
60
+ {
61
+ 'hoge' => 'xxx',
62
+ 'fuga' => 1,
63
+ },
64
+ ]
65
+ }
66
+
67
+ assert_raise_message(/baz:#0/) { @rule.validate(input) }
68
+ end
69
+
70
+ test 'invalid data at the second of array' do
71
+ input = {
72
+ 'foo' => 'xxx',
73
+ 'bar' => 123,
74
+ 'baz' => [
75
+ {
76
+ 'hoge' => 'xxx',
77
+ 'fuga' => 1,
78
+ },
79
+ {
80
+ 'fuga' => 'piyo',
81
+ },
82
+ {
83
+ 'hoge' => 'xxx',
84
+ 'fuga' => 2,
85
+ },
86
+ {
87
+ 'fuga' => 'piyo',
88
+ },
89
+ ]
90
+ }
91
+
92
+ assert_raise_message(/baz:#1:fuga/) { @rule.validate(input) }
93
+ end
94
+
95
+ test 'nested array' do
96
+ rule = Data::Validator::Recursive.new(
97
+ 'foo' => String,
98
+ 'bar' => { isa: Integer, default: 1 },
99
+ 'baz' => {
100
+ isa: Array,
101
+ rule: {
102
+ 'piyo' => {
103
+ isa: Array,
104
+ rule: {
105
+ 'hoge' => { isa: String, default: 'yyy' },
106
+ 'fuga' => Integer,
107
+ }
108
+ },
109
+ },
110
+ },
111
+ )
112
+
113
+ input = {
114
+ 'foo' => 'xxx',
115
+ 'baz' => [
116
+ {
117
+ 'piyo' => [
118
+ {
119
+ 'fuga' => 123,
120
+ },
121
+ ],
122
+ },
123
+ ],
124
+ };
125
+
126
+ params = rule.validate(input)
127
+ expected = {
128
+ 'foo' => 'xxx',
129
+ 'bar' => 1,
130
+ 'baz' => [
131
+ {
132
+ 'piyo' => [
133
+ {
134
+ 'hoge' => 'yyy',
135
+ 'fuga' => 123,
136
+ },
137
+ ],
138
+ },
139
+ ],
140
+ }
141
+
142
+ assert { params == expected }
143
+ end
144
+
145
+ test 'conflicts' do
146
+ input = {
147
+ 'foo' => 'xxx',
148
+ 'bar' => 123,
149
+ 'baz' => [
150
+ {
151
+ 'hoge' => 'xxx',
152
+ 'fuga' => 456,
153
+ 'piyo' => %w/a b c/,
154
+ },
155
+ ]
156
+ }
157
+
158
+ assert_raise_message(/baz:#0:piyo/) { @rule.validate(input) }
159
+ end
160
+
161
+ test 'with default option' do
162
+ rule = Data::Validator::Recursive.new(
163
+ 'foo' => String,
164
+ 'bar' => { isa: Integer, default: 1 },
165
+ 'baz' => {
166
+ isa: Array,
167
+ rule: {
168
+ 'hoge' => String,
169
+ 'fuga' => Integer,
170
+ },
171
+ },
172
+ )
173
+
174
+ input = {
175
+ 'foo' => 'xxx',
176
+ 'baz' => [
177
+ {
178
+ 'hoge' => 'xxx',
179
+ 'fuga' => 456,
180
+ },
181
+ ]
182
+ }
183
+
184
+ params = rule.validate(input)
185
+
186
+ assert { params['bar'] == 1 }
187
+ end
188
+
189
+ test 'default option with nested' do
190
+ rule = Data::Validator::Recursive.new(
191
+ 'foo' => String,
192
+ 'bar' => { isa: Integer, default: 1 },
193
+ 'baz' => {
194
+ isa: Array,
195
+ rule: {
196
+ 'hoge' => { isa: String, default: 'yyy' },
197
+ 'fuga' => Integer,
198
+ },
199
+ },
200
+ )
201
+
202
+ input = {
203
+ 'foo' => 'xxx',
204
+ 'baz' => [
205
+ {
206
+ 'fuga' => 456,
207
+ },
208
+ ]
209
+ }
210
+
211
+ params = rule.validate(input)
212
+ expected = {
213
+ 'foo' => 'xxx',
214
+ 'bar' => 1,
215
+ 'baz' => [
216
+ {
217
+ 'hoge' => 'yyy',
218
+ 'fuga' => 456,
219
+ },
220
+ ]
221
+ }
222
+
223
+ assert { params == expected }
224
+ end
225
+
226
+ test 'with AllowExtra' do
227
+ rule = Data::Validator::Recursive.new(
228
+ 'foo' => String,
229
+ 'bar' => { isa: Integer, default: 1 },
230
+ 'baz' => {
231
+ isa: Array,
232
+ with: 'AllowExtra',
233
+ rule: {
234
+ 'hoge' => { isa: String, default: 'yyy' },
235
+ 'fuga' => Integer
236
+ },
237
+ },
238
+ ).with('AllowExtra')
239
+
240
+
241
+ input = {
242
+ 'foo' => 'xxx',
243
+ 'baz' => [
244
+ {
245
+ 'fuga' => 123,
246
+ 'extra_param_in_baz' => 1,
247
+ },
248
+ ],
249
+ 'extra_param' => 1,
250
+ }
251
+
252
+ params = rule.validate(input)
253
+ expected = {
254
+ 'foo' => 'xxx',
255
+ 'bar' => 1,
256
+ 'baz' => [
257
+ {
258
+ 'hoge' => 'yyy',
259
+ 'fuga' => 123,
260
+ 'extra_param_in_baz' => 1,
261
+ },
262
+ ],
263
+ 'extra_param' => 1,
264
+ }
265
+
266
+ assert { params == expected }
267
+ end
268
+ end