kwalify 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,364 @@
1
+ ###
2
+ ### $Rev: 7 $
3
+ ### $Release: 0.1.0 $
4
+ ### copyright(c) 2005 kuwata-lab all rights reserved.
5
+ ###
6
+
7
+ BASE_DIR = File::dirname($0)
8
+ $: << "#{BASE_DIR}/../lib"
9
+ $: << "#{BASE_DIR}/../../lib"
10
+
11
+ require 'test/unit'
12
+ require 'test/unit/ui/console/testrunner'
13
+
14
+ require 'kwalify'
15
+ require 'kwalify/util/assert-diff'
16
+
17
+ require 'yaml'
18
+ require 'pp'
19
+
20
+ class ValidatorTest < Test::Unit::TestCase
21
+
22
+ def _test(flag_print=false)
23
+ schema = YAML.load(@schema.gsub(/^\t/, ''))
24
+ document = YAML.load(@data.gsub(/^\t/, ''))
25
+ validator = Kwalify::Validator.new(schema)
26
+ error_list = validator.validate(document)
27
+ actual = ""
28
+ error_list.each do |error|
29
+ actual << "%-20s: %s\n" % [error.error_symbol.inspect, error.message]
30
+ end
31
+ if flag_print
32
+ print actual
33
+ else
34
+ assert_equal_with_diff(@expected.gsub(/^\t/, ''), actual)
35
+ end
36
+ return error_list
37
+ end
38
+
39
+
40
+ def test_validate01
41
+ @schema = <<-'END'
42
+ type: map
43
+ required: true
44
+ mapping:
45
+ name:
46
+ type: string
47
+ required: true
48
+ email:
49
+ type: string
50
+ pattern: /@/
51
+ required: yes
52
+ age:
53
+ type: integer
54
+ blood:
55
+ type: string
56
+ enum:
57
+ - A
58
+ - B
59
+ - O
60
+ - AB
61
+ birth:
62
+ type: date
63
+ END
64
+ ## correct data
65
+ @data = <<-'END'
66
+ name: foo
67
+ email: foo@mail.com
68
+ age: 20
69
+ blood: AB
70
+ birth: 1985-01-01
71
+ END
72
+ @expected = ""
73
+ errors = _test()
74
+ ## incorrect data
75
+ @data = <<-'END'
76
+ nam: foo
77
+ email: foo(at)mail.com
78
+ age: twenty
79
+ blood: ab
80
+ birth: Jul 01, 1985
81
+ END
82
+ @expected = <<-'END'
83
+ :missing_key : [/] key 'name' required but not found.
84
+ :invalid_key : [/] key 'nam' is not expected.
85
+ :invalid_enum : [/blood] 'ab' is invalid enum value.
86
+ :invalid_type : [/birth] date type expected but got String.
87
+ :invalid_type : [/age] integer type expected but got String.
88
+ :invalid_pattern : [/email] 'foo(at)mail.com' is not matched to pattern /@/.
89
+ END
90
+ errors = _test()
91
+ end
92
+
93
+
94
+
95
+ def test_validate02
96
+ @schema = <<-'END'
97
+ type: seq
98
+ required: true
99
+ sequence:
100
+ - type: string
101
+ required: true
102
+ END
103
+ ## correct data
104
+ @data = <<-'END'
105
+ - foo
106
+ - bar
107
+ - baz
108
+ END
109
+ @expected = ''
110
+ _test()
111
+ ## incorrect data
112
+ @data = <<-'END'
113
+ - foo
114
+ - bar
115
+ -
116
+ - baz
117
+ - 100
118
+ END
119
+ @expected = <<-'END'
120
+ :missing_value : [/3] value required but not found.
121
+ :invalid_type : [/5] string type expected but got Fixnum.
122
+ END
123
+ errors = _test()
124
+ end
125
+
126
+
127
+ def test_validate03
128
+ @schema = <<-'END'
129
+ type: map
130
+ required: true
131
+ mapping:
132
+ address-book:
133
+ type: seq
134
+ required: true
135
+ sequence:
136
+ - type: map
137
+ mapping:
138
+ name:
139
+ type: string
140
+ required: yes
141
+ email:
142
+ type: string
143
+ pattern: /@/
144
+ required: yes
145
+ age:
146
+ type: integer
147
+ blood:
148
+ type: string
149
+ enum:
150
+ - A
151
+ - B
152
+ - O
153
+ - AB
154
+ birth:
155
+ type: date
156
+ END
157
+ ## correct data
158
+ @data = <<-'END'
159
+ address-book:
160
+ - name: foo
161
+ email: foo@mail.com
162
+ age: 20
163
+ blood: AB
164
+ birth: 1985-01-01
165
+ - name: bar
166
+ email: foo@mail.com
167
+ END
168
+ @expected = <<-'END'
169
+ END
170
+ errors = _test()
171
+ ## incorrect data
172
+ @data = <<-'END'
173
+ address-book:
174
+ - name: foo
175
+ mail: foo@mail.com
176
+ age: twenty
177
+ blood: ab
178
+ birth: 1985/01/01
179
+ - name: bar
180
+ email: bar(at)mail.com
181
+ END
182
+ @expected = <<-'END'
183
+ :missing_key : [/address-book/1] key 'email' required but not found.
184
+ :invalid_key : [/address-book/1] key 'mail' is not expected.
185
+ :invalid_enum : [/address-book/1/blood] 'ab' is invalid enum value.
186
+ :invalid_type : [/address-book/1/birth] date type expected but got String.
187
+ :invalid_type : [/address-book/1/age] integer type expected but got String.
188
+ :invalid_pattern : [/address-book/2/email] 'bar(at)mail.com' is not matched to pattern /@/.
189
+ END
190
+ errors = _test()
191
+ end
192
+
193
+
194
+
195
+ def test_validate11
196
+ @schema = <<-'END'
197
+ type: seq
198
+ required: true
199
+ sequence:
200
+ - type: map
201
+ required: true
202
+ mapping:
203
+ first-name: &name
204
+ type: string
205
+ required: yes
206
+ family-name: *name
207
+ END
208
+ ## correct data
209
+ @data = <<-'END'
210
+ - first-name: foo
211
+ family-name: Foo
212
+ - first-name: bar
213
+ family-name: Bar
214
+ END
215
+ @expected = <<-'END'
216
+ END
217
+ errors = _test()
218
+ ## incorrect data
219
+ @data = <<-'END'
220
+ - first-name: foo
221
+ last-name: Foo
222
+ - first-name: bar
223
+ family-name: 100
224
+ END
225
+ @expected = <<-'END'
226
+ :missing_key : [/1] key 'family-name' required but not found.
227
+ :invalid_key : [/1] key 'last-name' is not expected.
228
+ :invalid_type : [/2/family-name] string type expected but got Fixnum.
229
+ END
230
+ errors = _test()
231
+ end
232
+
233
+
234
+
235
+ def test_validate12
236
+ @schema = <<-'END'
237
+ type: map
238
+ required: true
239
+ mapping:
240
+ title: &name
241
+ type: string
242
+ required: true
243
+ address-book:
244
+ type: seq
245
+ required: true
246
+ sequence:
247
+ - type: map
248
+ mapping:
249
+ name: *name
250
+ email:
251
+ type: string
252
+ required: yes
253
+ END
254
+ ## correct data
255
+ @data = <<-'END'
256
+ title: my friends
257
+ address-book:
258
+ - name: foo
259
+ email: foo@mail.com
260
+ - name: bar
261
+ email: bar@mail.com
262
+ END
263
+ @expected = ""
264
+ errors = _test()
265
+ ## incorrect data
266
+ @data = <<-'END'
267
+ title: my friends
268
+ address-book:
269
+ - name: 100
270
+ email: foo@mail.com
271
+ - first-name: bar
272
+ email: bar@mail.com
273
+ END
274
+ @expected = <<-'END'
275
+ :invalid_type : [/address-book/1/name] string type expected but got Fixnum.
276
+ :missing_key : [/address-book/2] key 'name' required but not found.
277
+ :invalid_key : [/address-book/2] key 'first-name' is not expected.
278
+ END
279
+ errors = _test()
280
+ end
281
+
282
+
283
+
284
+ def test_validate13
285
+ @schema = <<-'END'
286
+ type: seq
287
+ sequence:
288
+ - &employee
289
+ type: map
290
+ mapping:
291
+ name:
292
+ type: string
293
+ post:
294
+ enum:
295
+ - exective
296
+ - manager
297
+ - clerk
298
+ supervisor: *employee
299
+ END
300
+ ## correct data
301
+ @data = <<-'END'
302
+ - &foo
303
+ name: foo
304
+ post: exective
305
+ - &bar
306
+ name: bar
307
+ post: manager
308
+ supervisor: *foo
309
+ - &baz
310
+ name: baz
311
+ post: clerk
312
+ supervisor: *bar
313
+ - &zak
314
+ name: zak
315
+ post: clerk
316
+ supervisor: *bar
317
+ END
318
+ @expected = ""
319
+ errors = _test()
320
+ ## incorrect data
321
+ @data = <<-'END'
322
+ - &foo
323
+ name: 100
324
+ post: exective
325
+ supervisor: *foo
326
+ - &bar
327
+ name: foo
328
+ post: worker
329
+ supervisor: *foo
330
+ END
331
+ @expected = <<-'END'
332
+ :invalid_type : [/1/name] string type expected but got Fixnum.
333
+ :invalid_enum : [/2/post] 'worker' is invalid enum value.
334
+ END
335
+ errors = _test()
336
+ end
337
+
338
+
339
+
340
+ # def test_validateXX
341
+ # @schema = <<-'END'
342
+ # END
343
+ # ## correct data
344
+ # @data = <<-'END'
345
+ # END
346
+ # @expected = <<-'END'
347
+ # END
348
+ # errors = _test()
349
+ # ## incorrect data
350
+ # @data = <<-'END'
351
+ # END
352
+ # @expected = <<-'END'
353
+ # END
354
+ # errors = _test()
355
+ # end
356
+
357
+
358
+ end
359
+
360
+
361
+ suite = Test::Unit::TestSuite.new()
362
+ suite << ValidatorTest.suite()
363
+ Test::Unit::UI::Console::TestRunner.run(suite)
364
+
@@ -0,0 +1,21 @@
1
+ .- [v] change filename 'error-messages.rb' to 'error-msg.rb'
2
+ .- [_] support i18n error message
3
+ .- [v] change class name 'RuleSet' to 'Rule' or 'Schema'
4
+ .- [v] use hash style to define hash children
5
+ .- [v] rename 'children' to 'elements' or 'entries'
6
+ .- [v] support element anchor
7
+ .- [v] users-guide
8
+ .- [v] examples
9
+ .- [v] gemspec
10
+ .- [_] 'unique-keys:'
11
+ .- [_] 'not-null:'
12
+ .- [_] 'primary-key:'
13
+ .- [_] 'constraints:'
14
+ .- [v] meta-validation
15
+ .- [v] use 'sequence'/'mapping' or 'hash-elems'/'list-elems' instead of 'elements'
16
+ .- [X] 'default:'
17
+ .- [v] set default type as 'String'
18
+ .- [v] type 'Object'
19
+ .- [_] check RelaxNG
20
+ .- [_] check XML Schema type
21
+ .- [_] separate test script
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.10
3
+ specification_version: 1
4
+ name: kwalify
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2005-08-01
8
+ summary: a tiny schema validator for YAML document.
9
+ require_paths:
10
+ - lib
11
+ email:
12
+ homepage: http://www.kuwata-lab.com/kwalify
13
+ rubyforge_project:
14
+ description: Kwalify is a tiny schema validator for YAML document. It is in fact very poor compared to Relax NG or DTD. I hope you extend/customize Kwalify for your own way.
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ -
22
+ - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 0.0.0
25
+ version:
26
+ platform: ruby
27
+ authors:
28
+ - kwatch
29
+ files:
30
+ - lib/kwalify.rb
31
+ - lib/kwalify
32
+ - lib/kwalify/error-msg.rb
33
+ - lib/kwalify/types.rb
34
+ - lib/kwalify/validator.rb
35
+ - lib/kwalify/errors.rb
36
+ - lib/kwalify/meta-validator.rb
37
+ - lib/kwalify/util
38
+ - lib/kwalify/util/yaml-helper.rb
39
+ - lib/kwalify/util/option-parser.rb
40
+ - lib/kwalify/util/assert-diff.rb
41
+ - bin/kwalify
42
+ - examples/invoice
43
+ - examples/address-book
44
+ - examples/invoice/invoice.schema.yaml
45
+ - examples/invoice/invoice.yaml
46
+ - examples/invoice/Makefile
47
+ - examples/address-book/Makefile
48
+ - examples/address-book/address-book.schema.yaml
49
+ - examples/address-book/address-book.yaml
50
+ - test/test.rb
51
+ - doc/users-guide.html
52
+ - doc/docstyle.css
53
+ - README.txt
54
+ - ChangeLog
55
+ - COPYING
56
+ - setup.rb
57
+ - todo.txt
58
+ test_files:
59
+ - test/test.rb
60
+ rdoc_options: []
61
+ extra_rdoc_files: []
62
+ executables:
63
+ - kwalify
64
+ extensions: []
65
+ requirements: []
66
+ dependencies: []