kwalify 0.6.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (219) hide show
  1. data/CHANGES.txt +232 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.txt +16 -19
  4. data/bin/kwalify +3 -3
  5. data/contrib/inline-require +6 -4
  6. data/contrib/kwalify +3719 -2427
  7. data/doc-api/classes/CommandOptionError.html +17 -17
  8. data/doc-api/classes/CommandOptionParser.html +63 -63
  9. data/doc-api/classes/Kwalify.html +29 -7
  10. data/doc-api/classes/Kwalify/AssertionError.html +9 -9
  11. data/doc-api/classes/Kwalify/BaseError.html +72 -71
  12. data/doc-api/classes/Kwalify/BaseParser.html +461 -0
  13. data/doc-api/classes/Kwalify/CommandOptionError.html +11 -11
  14. data/doc-api/classes/Kwalify/ErrorHelper.html +51 -46
  15. data/doc-api/classes/Kwalify/HashInterface.html +13 -135
  16. data/doc-api/classes/Kwalify/Json.html +105 -0
  17. data/doc-api/classes/Kwalify/Main.html +129 -126
  18. data/doc-api/classes/Kwalify/MetaValidator.html +248 -232
  19. data/doc-api/classes/Kwalify/Parser.html +12 -12
  20. data/doc-api/classes/Kwalify/PlainYamlParser.html +166 -163
  21. data/doc-api/classes/Kwalify/PlainYamlParser/Alias.html +11 -11
  22. data/doc-api/classes/Kwalify/Rule.html +152 -130
  23. data/doc-api/classes/Kwalify/SchemaError.html +10 -10
  24. data/doc-api/classes/Kwalify/SyntaxError.html +185 -0
  25. data/doc-api/classes/Kwalify/Types.html +26 -25
  26. data/doc-api/classes/Kwalify/Util.html +389 -0
  27. data/doc-api/classes/Kwalify/Util/HashLike.html +246 -0
  28. data/doc-api/classes/Kwalify/Util/OrderedHash.html +330 -0
  29. data/doc-api/classes/Kwalify/ValidationError.html +10 -10
  30. data/doc-api/classes/Kwalify/Validator.html +153 -86
  31. data/doc-api/classes/Kwalify/Yaml.html +181 -0
  32. data/doc-api/classes/Kwalify/Yaml/Parser.html +1538 -0
  33. data/doc-api/classes/Kwalify/YamlParser.html +190 -183
  34. data/doc-api/classes/Kwalify/YamlSyntaxError.html +8 -57
  35. data/doc-api/created.rid +1 -1
  36. data/doc-api/files/__/README_txt.html +17 -22
  37. data/doc-api/files/kwalify/errors_rb.html +2 -2
  38. data/doc-api/files/kwalify/main_rb.html +4 -3
  39. data/doc-api/files/kwalify/messages_rb.html +2 -2
  40. data/doc-api/files/kwalify/meta-validator_rb.html +3 -3
  41. data/doc-api/files/kwalify/{util/yaml-helper_rb.html → parser/base_rb.html} +8 -6
  42. data/doc-api/files/kwalify/parser/yaml_rb.html +117 -0
  43. data/doc-api/files/kwalify/rule_rb.html +2 -2
  44. data/doc-api/files/kwalify/types_rb.html +2 -2
  45. data/doc-api/files/kwalify/util/assert-text-equal_rb.html +2 -2
  46. data/doc-api/files/kwalify/util/hash-interface_rb.html +9 -2
  47. data/doc-api/files/kwalify/util/hashlike_rb.html +107 -0
  48. data/doc-api/files/kwalify/util/option-parser_rb.html +2 -2
  49. data/doc-api/files/kwalify/util/ordered-hash_rb.html +107 -0
  50. data/doc-api/files/kwalify/util/testcase-helper_rb.html +2 -2
  51. data/doc-api/files/kwalify/util_rb.html +107 -0
  52. data/doc-api/files/kwalify/validator_rb.html +2 -2
  53. data/doc-api/files/kwalify/yaml-parser_rb.html +2 -2
  54. data/doc-api/files/kwalify_rb.html +3 -2
  55. data/doc-api/fr_class_index.html +8 -1
  56. data/doc-api/fr_file_index.html +5 -1
  57. data/doc-api/fr_method_index.html +128 -69
  58. data/doc/img/fig01.png +0 -0
  59. data/doc/users-guide.html +882 -717
  60. data/examples/address-book/address-book.schema.yaml +2 -2
  61. data/examples/data-binding/BABEL.data.yaml +63 -0
  62. data/examples/data-binding/BABEL.schema.yaml +31 -0
  63. data/examples/data-binding/Makefile +8 -0
  64. data/examples/data-binding/Rakefile +13 -0
  65. data/examples/data-binding/main.rb +27 -0
  66. data/examples/invoice/invoice.schema.yaml +3 -3
  67. data/examples/tapkit/tapkit.schema.yaml +2 -2
  68. data/lib/kwalify.rb +41 -4
  69. data/lib/kwalify/errors.rb +118 -96
  70. data/lib/kwalify/kwalify.schema.yaml +58 -0
  71. data/lib/kwalify/main.rb +384 -377
  72. data/lib/kwalify/messages.rb +41 -27
  73. data/lib/kwalify/meta-validator.rb +251 -331
  74. data/lib/kwalify/parser/base.rb +127 -0
  75. data/lib/kwalify/parser/yaml.rb +837 -0
  76. data/lib/kwalify/rule.rb +545 -487
  77. data/lib/kwalify/templates/genclass-java.eruby +189 -162
  78. data/lib/kwalify/templates/genclass-php.eruby +104 -0
  79. data/lib/kwalify/templates/genclass-ruby.eruby +95 -66
  80. data/lib/kwalify/types.rb +107 -106
  81. data/lib/kwalify/util.rb +157 -0
  82. data/lib/kwalify/util/assert-text-equal.rb +33 -31
  83. data/lib/kwalify/util/hash-interface.rb +11 -30
  84. data/lib/kwalify/util/hashlike.rb +51 -0
  85. data/lib/kwalify/util/option-parser.rb +144 -144
  86. data/lib/kwalify/util/ordered-hash.rb +57 -0
  87. data/lib/kwalify/util/testcase-helper.rb +3 -3
  88. data/lib/kwalify/validator.rb +267 -212
  89. data/lib/kwalify/yaml-parser.rb +822 -768
  90. data/setup.rb +861 -607
  91. data/test/Rookbook.yaml +10 -0
  92. data/test/{tmp.dir/Context.java → data/users-guide/AddressBook.java.expected} +11 -11
  93. data/test/data/users-guide/BABEL.data.yaml +24 -0
  94. data/test/data/users-guide/BABEL.schema.yaml +30 -0
  95. data/test/data/users-guide/ExampleAddressBook.java +47 -0
  96. data/test/{tmp.dir/Group.java → data/users-guide/Group.java.expected} +2 -11
  97. data/test/data/users-guide/Person.java.expected +44 -0
  98. data/test/data/users-guide/address_book.rb +52 -0
  99. data/test/data/users-guide/address_book.schema.yaml +28 -0
  100. data/test/data/users-guide/address_book.yaml +27 -0
  101. data/test/data/users-guide/answers-schema.yaml +12 -0
  102. data/test/data/users-guide/answers-validator.rb +52 -0
  103. data/test/data/users-guide/babel_genclass.result +26 -0
  104. data/test/data/users-guide/config.schema.yaml +7 -0
  105. data/test/data/users-guide/config.yaml +4 -0
  106. data/test/{tmp.dir/silent1.document → data/users-guide/document01a.yaml} +0 -0
  107. data/test/data/users-guide/document01b.yaml +3 -0
  108. data/test/data/users-guide/document02a.yaml +4 -0
  109. data/test/data/users-guide/document02b.yaml +4 -0
  110. data/test/data/users-guide/document03a.yaml +6 -0
  111. data/test/data/users-guide/document03b.yaml +6 -0
  112. data/test/data/users-guide/document04a.yaml +9 -0
  113. data/test/data/users-guide/document04b.yaml +9 -0
  114. data/test/data/users-guide/document05a.yaml +11 -0
  115. data/test/data/users-guide/document05b.yaml +12 -0
  116. data/test/data/users-guide/document06a.yaml +15 -0
  117. data/test/data/users-guide/document06b.yaml +16 -0
  118. data/test/data/users-guide/document07a.yaml +9 -0
  119. data/test/data/users-guide/document07b.yaml +7 -0
  120. data/test/data/users-guide/document12a.json +10 -0
  121. data/test/data/users-guide/document12b.json +6 -0
  122. data/test/data/users-guide/document13a.yaml +17 -0
  123. data/test/data/users-guide/document14a.yaml +3 -0
  124. data/test/data/users-guide/document14b.yaml +3 -0
  125. data/test/data/users-guide/document15a.yaml +6 -0
  126. data/test/data/users-guide/document15b.yaml +5 -0
  127. data/test/data/users-guide/example_address_book.rb +10 -0
  128. data/test/data/users-guide/example_address_book_java.result +32 -0
  129. data/test/data/users-guide/example_address_book_ruby.result +31 -0
  130. data/test/data/users-guide/genclass_java.result +4 -0
  131. data/test/data/users-guide/howto-validation-with-parsing.rb +28 -0
  132. data/test/data/users-guide/howto-validation.rb +25 -0
  133. data/test/data/users-guide/howto3.rb +6 -0
  134. data/test/data/users-guide/howto3.result +5 -0
  135. data/test/data/users-guide/howto3.yaml +8 -0
  136. data/test/data/users-guide/howto5_databinding.result +111 -0
  137. data/test/data/users-guide/invalid01.result +3 -0
  138. data/test/data/users-guide/invalid02.result +5 -0
  139. data/test/data/users-guide/invalid03.result +5 -0
  140. data/test/data/users-guide/invalid04.result +4 -0
  141. data/test/data/users-guide/invalid05.result +11 -0
  142. data/test/data/users-guide/invalid06.result +4 -0
  143. data/test/data/users-guide/invalid07.result +3 -0
  144. data/test/data/users-guide/invalid08.result +3 -0
  145. data/test/data/users-guide/invalid12.json +8 -0
  146. data/test/data/users-guide/invalid14.result +4 -0
  147. data/test/data/users-guide/invalid15.result +4 -0
  148. data/test/data/users-guide/loadbabel.rb +27 -0
  149. data/test/data/users-guide/loadconfig.rb +15 -0
  150. data/test/data/users-guide/loadconfig.result +2 -0
  151. data/test/data/users-guide/models.rb +22 -0
  152. data/test/data/users-guide/option_ha.result +6 -0
  153. data/test/data/users-guide/option_ha_genclass_java.result +7 -0
  154. data/test/{tmp.dir/meta1.schema → data/users-guide/schema01.yaml} +0 -0
  155. data/test/data/users-guide/schema02.yaml +12 -0
  156. data/test/data/users-guide/schema03.yaml +9 -0
  157. data/test/data/users-guide/schema04.yaml +20 -0
  158. data/test/data/users-guide/schema05.yaml +29 -0
  159. data/test/data/users-guide/schema06.yaml +11 -0
  160. data/test/data/users-guide/schema12.json +12 -0
  161. data/test/data/users-guide/schema13.yaml +13 -0
  162. data/test/data/users-guide/schema14.yaml +5 -0
  163. data/test/data/users-guide/schema15.yaml +21 -0
  164. data/test/data/users-guide/valid01.result +2 -0
  165. data/test/data/users-guide/valid02.result +2 -0
  166. data/test/data/users-guide/valid03.result +2 -0
  167. data/test/data/users-guide/valid04.result +2 -0
  168. data/test/data/users-guide/valid05.result +2 -0
  169. data/test/data/users-guide/valid06.result +2 -0
  170. data/test/data/users-guide/valid07.result +2 -0
  171. data/test/data/users-guide/valid08.result +2 -0
  172. data/test/data/users-guide/valid12.result +2 -0
  173. data/test/data/users-guide/valid13.result +2 -0
  174. data/test/data/users-guide/valid14.result +2 -0
  175. data/test/data/users-guide/valid15.result +2 -0
  176. data/test/data/users-guide/validate08.rb +37 -0
  177. data/test/test-action.rb +78 -0
  178. data/test/test-action.yaml +738 -0
  179. data/test/test-databinding.rb +80 -0
  180. data/test/test-databinding.yaml +301 -0
  181. data/test/test-main.rb +129 -150
  182. data/test/test-main.yaml +126 -321
  183. data/test/test-metavalidator.rb +47 -47
  184. data/test/test-metavalidator.yaml +77 -21
  185. data/test/test-parser-yaml.rb +57 -0
  186. data/test/test-parser-yaml.yaml +1749 -0
  187. data/test/test-rule.rb +14 -15
  188. data/test/test-rule.yaml +6 -3
  189. data/test/test-users-guide.rb +75 -0
  190. data/test/test-validator.rb +77 -52
  191. data/test/test-validator.yaml +168 -6
  192. data/test/test-yaml-parser.rb +47 -0
  193. data/test/{test-yamlparser.yaml → test-yaml-parser.yaml} +159 -52
  194. data/test/test.rb +37 -21
  195. metadata +136 -37
  196. data/COPYING +0 -340
  197. data/ChangeLog +0 -70
  198. data/doc-api/classes/YamlHelper.html +0 -259
  199. data/lib/kwalify/util/yaml-helper.rb +0 -82
  200. data/test/test-yamlparser.rb +0 -58
  201. data/test/tmp.dir/User.java +0 -43
  202. data/test/tmp.dir/action1.document +0 -18
  203. data/test/tmp.dir/action1.schema +0 -32
  204. data/test/tmp.dir/action2.document +0 -18
  205. data/test/tmp.dir/action2.schema +0 -32
  206. data/test/tmp.dir/emacs.document +0 -6
  207. data/test/tmp.dir/emacs.schema +0 -6
  208. data/test/tmp.dir/meta1.document +0 -0
  209. data/test/tmp.dir/meta2.document +0 -0
  210. data/test/tmp.dir/meta2.schema +0 -3
  211. data/test/tmp.dir/silent1.schema +0 -3
  212. data/test/tmp.dir/silent2.document +0 -7
  213. data/test/tmp.dir/silent2.schema +0 -3
  214. data/test/tmp.dir/stream.invalid +0 -8
  215. data/test/tmp.dir/stream.schema +0 -3
  216. data/test/tmp.dir/stream.valid +0 -8
  217. data/test/tmp.dir/untabify.document +0 -5
  218. data/test/tmp.dir/untabify.schema +0 -10
  219. data/todo.txt +0 -34
@@ -0,0 +1,3 @@
1
+ $ kwalify -lf schema01.yaml document01b.yaml
2
+ document01b.yaml#0: INVALID
3
+ - (line 2) [/1] '123': not a string.
@@ -0,0 +1,5 @@
1
+ $ kwalify -lf schema02.yaml document02b.yaml
2
+ document02b.yaml#0: INVALID
3
+ - (line 2) [/email] 'foo(at)mail.com': not matched to pattern /@/.
4
+ - (line 3) [/age] 'twenty': not a integer.
5
+ - (line 4) [/birth] 'Jun 01, 1985': not a date.
@@ -0,0 +1,5 @@
1
+ $ kwalify -lf schema03.yaml document03b.yaml
2
+ document03b.yaml#0: INVALID
3
+ - (line 3) [/1] key 'name:' is required.
4
+ - (line 3) [/1/naem] key 'naem:' is undefined.
5
+ - (line 6) [/2/mail] key 'mail:' is undefined.
@@ -0,0 +1,4 @@
1
+ $ kwalify -lf schema04.yaml document04b.yaml
2
+ document04b.yaml#0: INVALID
3
+ - (line 4) [/employees/0/code] 'A101': not a integer.
4
+ - (line 9) [/employees/1/mail] key 'mail:' is undefined.
@@ -0,0 +1,11 @@
1
+ $ kwalify -lf schema05.yaml document05b.yaml
2
+ document05b.yaml#0: INVALID
3
+ - (line 2) [/0/email] 'foo(at)mail.com': not matched to pattern /@/.
4
+ - (line 3) [/0/password] 'xxx123': too short (length 6 < min 8).
5
+ - (line 4) [/0/age] 'twenty': not a integer.
6
+ - (line 5) [/0/blood] 'a': invalid blood value.
7
+ - (line 7) [/1] key 'name:' is required.
8
+ - (line 7) [/1/given-name] key 'given-name:' is undefined.
9
+ - (line 8) [/1/family-name] key 'family-name:' is undefined.
10
+ - (line 10) [/1/age] '15': too small (< min 18).
11
+ - (line 12) [/1/birth] '1980/01/01': not a date.
@@ -0,0 +1,4 @@
1
+ $ kwalify -lf schema06.yaml document06b.yaml
2
+ document06b.yaml#0: INVALID
3
+ - (line 7) [/0/groups/3] 'foo': is already used at '/0/groups/0'.
4
+ - (line 13) [/2/name] 'bar': is already used at '/1/name'.
@@ -0,0 +1,3 @@
1
+ $ ruby answers-validator.rb document07b.yaml
2
+ *** INVALID!
3
+ - [/answers/1] : reason is required when answer is 'bad'.
@@ -0,0 +1,3 @@
1
+ $ ruby validate08.rb document07b.yaml
2
+ *** INVALID!
3
+ - [/answers/1] : reason is required when answer is 'bad'.
@@ -0,0 +1,8 @@
1
+ $ kwalify -lf schema12.json document12b.json
2
+ document12b.json#0: INVALID
3
+ - (line 1) [/] key 'name:' is required.
4
+ - (line 2) [/mail] key 'mail:' is undefined.
5
+ - (line 3) [/age] 'twenty': not a integer.
6
+ - (line 4) [/gender] 'X': invalid gender value.
7
+ - (line 5) [/favorite/0] '123': not a string.
8
+ - (line 5) [/favorite/1] '456': not a string.
@@ -0,0 +1,4 @@
1
+ $ kwalify -lf schema14.yaml document14b.yaml
2
+ document14b.yaml#0: INVALID
3
+ - (line 2) [/value2] '1.1': too large (> max 1).
4
+ - (line 3) [/value3] '-2.0': too small (< min -1).
@@ -0,0 +1,4 @@
1
+ $ kwalify -lf schema15.yaml document15b.yaml
2
+ document15b.yaml#0: INVALID
3
+ - (line 5) [/user] key 'email:' is required.
4
+ - (line 5) [/user/name] 'toooooo-looooong-name': too long (length 21 > max 16).
@@ -0,0 +1,27 @@
1
+ require 'kwalify'
2
+ require 'models'
3
+
4
+ ## load schema definition
5
+ schema = Kwalify::Yaml.load_file('BABEL.schema.yaml',
6
+ :untabify=>true,
7
+ :preceding_alias=>true)
8
+
9
+ ## add module name to 'class:'
10
+ Kwalify::Util.traverse_schema(schema) do |rulehash|
11
+ if rulehash['class']
12
+ rulehash['class'] = 'Babel::' + rulehash['class']
13
+ end
14
+ end
15
+
16
+ ## create validator
17
+ validator = Kwalify::Validator.new(schema)
18
+
19
+ ## parse with data-binding
20
+ parser = Kwalify::Yaml::Parser.new(validator)
21
+ parser.preceding_alias = true
22
+ parser.data_binding = true
23
+ ydoc = parser.parse_file('BABEL.data.yaml', :untabify=>true)
24
+
25
+ ## show document
26
+ require 'pp'
27
+ pp ydoc
@@ -0,0 +1,15 @@
1
+ ## class definition
2
+ require 'kwalify/util/hashlike'
3
+ class Config
4
+ include Kwalify::Util::HashLike # defines [], []=, and keys?
5
+ attr_accessor :host, :posrt, :user, :pass
6
+ end
7
+ ## create validator object
8
+ require 'kwalify'
9
+ schema = Kwalify::Yaml.load_file('config.schema.yaml')
10
+ validator = Kwalify::Validator.new(schema)
11
+ ## parse configuration file with data binding
12
+ parser = Kwalify::Yaml::Parser.new(validator)
13
+ parser.data_binding = true # enable data binding
14
+ config = parser.parse_file('config.yaml')
15
+ p config
@@ -0,0 +1,2 @@
1
+ $ ruby loadconfig.rb
2
+ #<Config:0x542590 @host="localhost", @pass="password1", @port=8080, @user="user1">
@@ -0,0 +1,22 @@
1
+ require 'kwalify/util/hashlike'
2
+
3
+ module Babel
4
+
5
+ ##
6
+ class Team
7
+ include Kwalify::Util::HashLike
8
+ attr_accessor :name # str
9
+ attr_accessor :desc # str
10
+ attr_accessor :chief # map
11
+ attr_accessor :members # seq
12
+ end
13
+
14
+ ##
15
+ class Member
16
+ include Kwalify::Util::HashLike
17
+ attr_accessor :name # str
18
+ attr_accessor :desc # str
19
+ attr_accessor :team # map
20
+ end
21
+
22
+ end
@@ -0,0 +1,6 @@
1
+ $ kwalify -ha genclass-ruby
2
+ --module=name : module name in which class defined
3
+ --parent=name : parent class name
4
+ --include=name : module name which all classes include
5
+ --initialize=false : not print initialize() method
6
+ --hashlike : include Kwalify::Util::HashLike module
@@ -0,0 +1,7 @@
1
+ $ kwalify -ha genclass-java
2
+ --package=name : package name
3
+ --extends=name : class name to extend
4
+ --implements=name,... : interface names to implement
5
+ --dir=path : directory to locate output file
6
+ --basedir=path : base directory to locate output file
7
+ --constructor=false : not print initialize() method
@@ -0,0 +1,12 @@
1
+ type: map
2
+ mapping:
3
+ "name":
4
+ type: str
5
+ required: yes
6
+ "email":
7
+ type: str
8
+ pattern: /@/
9
+ "age":
10
+ type: int
11
+ "birth":
12
+ type: date
@@ -0,0 +1,9 @@
1
+ type: seq
2
+ sequence:
3
+ - type: map
4
+ mapping:
5
+ "name":
6
+ type: str
7
+ required: true
8
+ "email":
9
+ type: str
@@ -0,0 +1,20 @@
1
+ type: map
2
+ mapping:
3
+ "company":
4
+ type: str
5
+ required: yes
6
+ "email":
7
+ type: str
8
+ "employees":
9
+ type: seq
10
+ sequence:
11
+ - type: map
12
+ mapping:
13
+ "code":
14
+ type: int
15
+ required: yes
16
+ "name":
17
+ type: str
18
+ required: yes
19
+ "email":
20
+ type: str
@@ -0,0 +1,29 @@
1
+ type: seq
2
+ sequence:
3
+ -
4
+ type: map
5
+ mapping:
6
+ "name":
7
+ type: str
8
+ required: yes
9
+ "email":
10
+ type: str
11
+ required: yes
12
+ pattern: /@/
13
+ "password":
14
+ type: text
15
+ length: { max: 16, min: 8 }
16
+ "age":
17
+ type: int
18
+ range: { max: 30, min: 18 }
19
+ # or assert: 18 <= val && val <= 30
20
+ "blood":
21
+ type: str
22
+ enum: [A, B, O, AB]
23
+ "birth":
24
+ type: date
25
+ "memo":
26
+ type: any
27
+ "deleted":
28
+ type: bool
29
+ default: false
@@ -0,0 +1,11 @@
1
+ type: seq
2
+ sequence:
3
+ - type: map
4
+ required: yes
5
+ mapping:
6
+ "name": { type: str, required: yes, unique: yes }
7
+ "email": { type: str }
8
+ "groups":
9
+ type: seq
10
+ sequence:
11
+ - { type: str, unique: yes }
@@ -0,0 +1,12 @@
1
+ { "type": "map",
2
+ "required": true,
3
+ "mapping": {
4
+ "name": { "type": "str", "required": true },
5
+ "email": { "type": "str" },
6
+ "age": { "type": "int" },
7
+ "gender": { "type": "str", "enum": ["M", "F"] },
8
+ "favorite": { "type": "seq",
9
+ "sequence": [ { "type": "str" } ]
10
+ }
11
+ }
12
+ }
@@ -0,0 +1,13 @@
1
+ type: seq
2
+ sequence:
3
+ - &employee
4
+ type: map
5
+ mapping:
6
+ "given-name": &name
7
+ type: str
8
+ required: yes
9
+ "family-name": *name
10
+ "post":
11
+ type: str
12
+ enum: [exective, manager, clerk]
13
+ "supervisor": *employee
@@ -0,0 +1,5 @@
1
+ type: map
2
+ mapping:
3
+ =: # default rule
4
+ type: number
5
+ range: { max: 1, min: -1 }
@@ -0,0 +1,21 @@
1
+ type: map
2
+ mapping:
3
+ "group":
4
+ type: map
5
+ mapping:
6
+ "name": &name
7
+ type: str
8
+ required: yes
9
+ "email": &email
10
+ type: str
11
+ pattern: /@/
12
+ required: no
13
+ "user":
14
+ type: map
15
+ mapping:
16
+ "name":
17
+ <<: *name # merge
18
+ length: { max: 16 } # add
19
+ "email":
20
+ <<: *email # merge
21
+ required: yes # override
@@ -0,0 +1,2 @@
1
+ $ kwalify -lf schema01.yaml document01a.yaml
2
+ document01a.yaml#0: valid.
@@ -0,0 +1,2 @@
1
+ $ kwalify -lf schema02.yaml document02a.yaml
2
+ document02a.yaml#0: valid.
@@ -0,0 +1,2 @@
1
+ $ kwalify -lf schema03.yaml document03a.yaml
2
+ document03a.yaml#0: valid.
@@ -0,0 +1,2 @@
1
+ $ kwalify -lf schema04.yaml document04a.yaml
2
+ document04a.yaml#0: valid.
@@ -0,0 +1,2 @@
1
+ $ kwalify -lf schema05.yaml document05a.yaml
2
+ document05a.yaml#0: valid.
@@ -0,0 +1,2 @@
1
+ $ kwalify -lf schema06.yaml document06a.yaml
2
+ document06a.yaml#0: valid.
@@ -0,0 +1,2 @@
1
+ $ ruby answers-validator.rb document07a.yaml
2
+ Valid.
@@ -0,0 +1,2 @@
1
+ $ ruby validate08.rb document07a.yaml
2
+ Valid.
@@ -0,0 +1,2 @@
1
+ $ kwalify -lf schema12.json document12a.json
2
+ document12a.json#0: valid.
@@ -0,0 +1,2 @@
1
+ $ kwalify -lf schema13.yaml document13a.yaml
2
+ document13a.yaml#0: valid.
@@ -0,0 +1,2 @@
1
+ $ kwalify -lf schema14.yaml document14a.yaml
2
+ document14a.yaml#0: valid.
@@ -0,0 +1,2 @@
1
+ $ kwalify -lf schema15.yaml document15a.yaml
2
+ document15a.yaml#0: valid.
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'kwalify'
4
+ require 'yaml'
5
+
6
+ ## load schema definition
7
+ schema = YAML.load_file('answers-schema.yaml')
8
+
9
+ ## create validator for answers
10
+ validator = Kwalify::Validator.new(schema) { |value, rule, path, errors|
11
+ case rule.name
12
+ when 'Answer'
13
+ if value['answer'] == 'bad'
14
+ reason = value['reason']
15
+ if !reason || reason.empty?
16
+ msg = "reason is required when answer is 'bad'."
17
+ errors << Kwalify::ValidationError.new(msg, path)
18
+ end
19
+ end
20
+ end
21
+ }
22
+
23
+ ## load YAML document
24
+ input = ARGF.read()
25
+ document = YAML.load(input)
26
+
27
+ ## validate
28
+ errors = validator.validate(document)
29
+ if errors.empty?
30
+ puts "Valid."
31
+ else
32
+ puts "*** INVALID!"
33
+ errors.each do |error|
34
+ # error.class == Kwalify::ValidationError
35
+ puts " - [#{error.path}] : #{error.message}"
36
+ end
37
+ end
@@ -0,0 +1,78 @@
1
+ ###
2
+ ### $Rev: 90 $
3
+ ### $Release: 0.7.0 $
4
+ ### copyright(c) 2005-2008 kuwata-lab all rights reserved.
5
+ ###
6
+
7
+ require File.dirname(__FILE__) + '/test.rb'
8
+
9
+ require 'kwalify/main'
10
+
11
+
12
+ module Kwalify
13
+ class Main
14
+ public :_parse_argv
15
+ end
16
+ end
17
+
18
+
19
+ class File
20
+ def self.move(filename, dirname)
21
+ File.rename(filename, "#{dirname}/#{filename}")
22
+ end
23
+ end
24
+
25
+
26
+ class ActionTest < Test::Unit::TestCase
27
+
28
+ ## define test methods
29
+ filename = __FILE__.sub(/\.rb$/, ".yaml")
30
+ load_yaml_testdata(filename, :lang=>'ruby')
31
+
32
+
33
+ ## temporary directory
34
+ @@tmpdir = "tmp.dir"
35
+ Dir.mkdir(@@tmpdir) unless test(?d, @@tmpdir)
36
+
37
+
38
+ def _test
39
+ if @exception
40
+ classname = @exception =~ /Kwalify::(.*)/ ? $1 : @exception
41
+ @exception_class = Kwalify.const_get(classname)
42
+ end
43
+ return if $target && $target != @name
44
+ raise "*** #{@name}: args is required." unless @args
45
+ raise "*** #{@name}: expected is require." unless @expected
46
+ #
47
+ File.open("#{@name}.schema", 'w') { |f| f.write(@schema) } if @schema
48
+ File.open("#{@name}.document", 'w') { |f| f.write(@document) } if @document
49
+ #
50
+ begin
51
+ main = Kwalify::Main.new("kwalify")
52
+ $stdout = StringWriter.new
53
+ main.execute(@args)
54
+ actual = $stdout; $stdout = STDOUT
55
+ if @output_files
56
+ if @output_message
57
+ assert_text_equal(@output_message, actual)
58
+ else
59
+ assert(actual.nil? || actual=='')
60
+ end
61
+ @output_files.each do |filename|
62
+ actual = File.read(filename)
63
+ assert_text_equal(@expected[filename], actual)
64
+ end
65
+ else
66
+ assert_text_equal(@expected, actual)
67
+ end
68
+ ensure
69
+ File.move("#{@name}.schema", @@tmpdir) if @schema
70
+ File.move("#{@name}.document", @@tmpdir) if @document
71
+ @output_files.each do |filename|
72
+ File.move(filename, @@tmpdir) if test(?f, filename)
73
+ end if @output_files
74
+ end
75
+ end
76
+
77
+
78
+ end