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
@@ -1,7 +1,7 @@
1
1
  ###
2
- ### $Rev: 51 $
3
- ### $Release: 0.6.1 $
4
- ### copyright(c) 2005 kuwata-lab all rights reserved.
2
+ ### $Rev: 95 $
3
+ ### $Release: 0.7.0 $
4
+ ### copyright(c) 2005-2008 kuwata-lab all rights reserved.
5
5
  ###
6
6
 
7
7
  module Kwalify
@@ -15,21 +15,28 @@ module Kwalify
15
15
 
16
16
 
17
17
  @@messages[:command_help] = <<END
18
- kwalify - tiny schema validator for YAML and JSON
19
- Usage1: kwalify [..options..] -f schema.yaml doc.yaml [doc2.yaml ...]
20
- Usage2: kwalify [..options..] -m schema.yaml [schema2.yaml ...]
21
- Usage3: kwalify [..options..] -a action -f schema.yaml [schema2.yaml ...]
22
- -h, --help : help
23
- -v : version
24
- -s : silent
25
- -f schema.yaml : schema definition file
26
- -m : meta-validation mode
27
- -t : expand tab characters
28
- -l : show linenumber when errored (experimental)
29
- -E : show errors in emacs-style (implies '-l')
30
- -a action : action ('genclass-ruby' or 'genclass-java')
31
- (use '-ha action' option to show action properites)
18
+ kwalify - schema validator and data binding tool for YAML and JSON
19
+ ## Usage1: validate yaml document
20
+ kwalify [..options..] -f schema.yaml doc.yaml [doc2.yaml ...]
21
+ ## Usage2: validate schema definition
22
+ kwalify [..options..] -m schema.yaml [schema2.yaml ...]
23
+ ## Usage3: do action
24
+ kwalify [..options..] -a action -f schema.yaml [schema2.yaml ...]
25
+ -h, --help : help
26
+ -v : version
27
+ -q : quiet
28
+ -s : silent (obsolete, use '-q' instead)
29
+ -f schema.yaml : schema definition file
30
+ -m : meta-validation mode
31
+ -t : expand tab characters
32
+ -l : show linenumber when errored (experimental)
33
+ -E : show errors in emacs-style (experimental, implies '-l')
34
+ -a action : action ('genclass-ruby', 'genclass-php', 'genclass-java')
35
+ (try '-ha genclass-ruby' for details)
36
+ -I path : template path (for '-a')
37
+ -P : allow preceding alias
32
38
  END
39
+ # -z : syntax checking of schema file
33
40
  # -I path : path for template of action
34
41
 
35
42
 
@@ -40,12 +47,12 @@ END
40
47
  @@messages[:command_option_noaction] = "command-line option '-f' or '-m' required."
41
48
  @@messages[:command_option_notemplate] = "%s: invalid action (template not found).\n"
42
49
  @@messages[:schema_empty] = "%s: empty schema.\n"
50
+ @@messages[:validation_empty] = "%s#%d: empty."
43
51
  @@messages[:validation_empty] = "%s#%d: empty.\n"
44
- @@messages[:validation_valid] = "%s#%d: valid.\n"
45
- @@messages[:validation_invalid] = "%s#%d: INVALID\n"
46
- @@messages[:command_option_noschema] = "-%s: schema filename required."
47
- @@messages[:command_option_noaction] = "-%s: action required."
48
- @@messages[:command_option_notpath] = "-%s: template path required."
52
+ @@messages[:validation_valid] = "%s#%d: valid."
53
+ @@messages[:command_option_schema_required] = "-%s: schema filename required."
54
+ @@messages[:command_option_action_required] = "-%s: action required."
55
+ @@messages[:command_option_tpath_required] = "-%s: template path required."
49
56
  @@messages[:command_property_invalid] = "%s: invalid property."
50
57
  @@messages[:command_option_invalid] = "-%s: invalid command option."
51
58
  # --
@@ -54,7 +61,7 @@ END
54
61
  @@messages[:key_unknown] = "unknown key."
55
62
  @@messages[:type_notstr] = "not a string."
56
63
  @@messages[:type_unknown] = "unknown type."
57
- @@messages[:classname_notmap] = "available only with map type."
64
+ @@messages[:class_notmap] = "available only with map type."
58
65
  @@messages[:required_notbool] = "not a boolean."
59
66
  @@messages[:pattern_notstr] = "not a string (or regexp)"
60
67
  @@messages[:pattern_notmatch] = "should be '/..../'."
@@ -93,6 +100,9 @@ END
93
100
  @@messages[:unique_notbool] = "not a boolean."
94
101
  @@messages[:unique_notscalar] = "is available only with a scalar type."
95
102
  @@messages[:unique_onroot] = "is not available on root element."
103
+ @@messages[:default_nonscalarval] = "not a scalar."
104
+ @@messages[:default_notscalar] = "is available only with a scalar type."
105
+ @@messages[:default_unmatch] = "not a %s."
96
106
  @@messages[:sequence_notseq] = "not a sequence."
97
107
  @@messages[:sequence_noelem] = "required one element."
98
108
  @@messages[:sequence_toomany] = "required just one element."
@@ -104,10 +114,14 @@ END
104
114
  @@messages[:map_conflict] = "not available with mapping."
105
115
  @@messages[:scalar_conflict] = "not available with scalar type."
106
116
  @@messages[:enum_conflict] = "not available with 'enum:'."
117
+ @@messages[:default_conflict] = "not available when 'required:' is true."
107
118
  # --
108
119
  # filename: lib/kwalify/validator.rb
109
120
  @@messages[:required_novalue] = "value required but none."
110
121
  @@messages[:type_unmatch] = "not a %s."
122
+ @@messages[:key_undefined] = "key '%s' is undefined."
123
+ @@messages[:required_nokey] = "key '%s:' is required."
124
+ @@messages[:value_notunique] = "is already used at '%s'."
111
125
  @@messages[:assert_failed] = "assertion expression failed (%s)."
112
126
  @@messages[:enum_notexist] = "invalid %s value."
113
127
  @@messages[:pattern_unmatch] = "not matched to pattern %s."
@@ -119,18 +133,18 @@ END
119
133
  @@messages[:length_tooshort] = "too short (length %d < min %d)."
120
134
  @@messages[:length_toolongex] = "too long (length %d >= max %d)."
121
135
  @@messages[:length_tooshortex] = "too short (length %d <= min %d)."
122
- @@messages[:value_notunique] = "is already used at '%s'."
123
- @@messages[:required_nokey] = "key '%s:' is required."
124
- @@messages[:key_undefined] = "key '%s' is undefined."
125
136
  # --
126
137
  # filename: lib/kwalify/yaml-parser.rb
127
138
  @@messages[:flow_hastail] = "flow style sequence is closed but got '%s'."
128
139
  @@messages[:flow_eof] = "found EOF when parsing flow style."
140
+ @@messages[:flow_alias_label] = "alias name expected."
141
+ @@messages[:flow_anchor_label] = "anchor name expected."
129
142
  @@messages[:flow_noseqitem] = "sequence item required (or last comma is extra)."
130
143
  @@messages[:flow_seqnotclosed] = "flow style sequence requires ']'."
131
144
  @@messages[:flow_mapnoitem] = "mapping item required (or last comma is extra)."
132
145
  @@messages[:flow_mapnotclosed] = "flow style mapping requires '}'."
133
- @@messages[:flow_nocolon] = "':' expected but got '%s'."
146
+ @@messages[:flow_nocolon] = "':' expected but got %s."
147
+ @@messages[:flow_str_notclosed] = "%s: string not closed."
134
148
  @@messages[:anchor_duplicated] = "anchor '%s' is already used."
135
149
  @@messages[:alias_extradata] = "alias cannot take any data."
136
150
  @@messages[:anchor_notfound] = "anchor '%s' not found"
@@ -1,355 +1,275 @@
1
1
  ###
2
- ### $Rev: 51 $
3
- ### $Release: 0.6.1 $
4
- ### copyright(c) 2005 kuwata-lab all rights reserved.
2
+ ### $Rev: 95 $
3
+ ### $Release: 0.7.0 $
4
+ ### copyright(c) 2005-2008 kuwata-lab all rights reserved.
5
5
  ###
6
6
 
7
7
  require 'kwalify/errors'
8
8
  require 'kwalify/rule'
9
9
  require 'kwalify/validator'
10
- require 'yaml'
10
+ require 'kwalify/parser/yaml'
11
+ #require 'yaml'
11
12
 
12
13
  module Kwalify
13
14
 
14
15
 
15
- ##
16
- ## ex.
17
- ## meta_validator = Kwalify::MetaValidator.instance()
18
- ## schema = File.load_file('schema.yaml')
19
- ## errors = meta_validator.validate(schema)
20
- ## if !errors.empty?
21
- ## errors.each do |error|
22
- ## puts "[#{error.path}] #{error.message}"
23
- ## end
24
- ## end
25
- ##
26
- class MetaValidator < Validator
16
+ ##
17
+ ## ex.
18
+ ## meta_validator = Kwalify::MetaValidator.instance()
19
+ ## schema = File.load_file('schema.yaml')
20
+ ## errors = meta_validator.validate(schema)
21
+ ## if !errors.empty?
22
+ ## errors.each do |error|
23
+ ## puts "[#{error.path}] #{error.message}"
24
+ ## end
25
+ ## end
26
+ ##
27
+ class MetaValidator < Validator
27
28
 
28
- META_SCHEMA = <<'END'
29
- name: MAIN
30
- type: map
31
- required: yes
32
- mapping: &main-rule
33
- "name":
34
- type: str
35
- "desc":
36
- type: str
37
- "classname":
38
- type: str
39
- "type":
40
- type: str
41
- #required: yes
42
- enum:
43
- - seq
44
- #- sequence
45
- #- list
46
- - map
47
- #- mapping
48
- #- hash
49
- - str
50
- #- string
51
- - int
52
- #- integer
53
- - float
54
- - number
55
- #- numeric
56
- - bool
57
- #- boolean
58
- - text
59
- - date
60
- - time
61
- - timestamp
62
- #- object
63
- - any
64
- - scalar
65
- #- collection
66
- "required":
67
- type: bool
68
- "enum":
69
- type: seq
70
- sequence:
71
- - type: scalar
72
- unique: yes
73
- "pattern":
74
- type: str
75
- "assert":
76
- type: str
77
- pattern: /\bval\b/
78
- "range":
79
- type: map
80
- mapping:
81
- "max":
82
- type: scalar
83
- "min":
84
- type: scalar
85
- "max-ex":
86
- type: scalar
87
- "min-ex":
88
- type: scalar
89
- "length":
90
- type: map
91
- mapping:
92
- "max":
93
- type: int
94
- "min":
95
- type: int
96
- "max-ex":
97
- type: int
98
- "min-ex":
99
- type: int
100
- "ident":
101
- type: bool
102
- "unique":
103
- type: bool
104
- "sequence":
105
- name: SEQUENCE
106
- type: seq
107
- sequence:
108
- - type: map
109
- mapping: *main-rule
110
- name: MAIN
111
- #required: yes
112
- "mapping":
113
- name: MAPPING
114
- type: map
115
- mapping:
116
- =:
117
- type: map
118
- mapping: *main-rule
119
- name: MAIN
120
- #required: yes
121
- END
29
+ filename = File.join(File.dirname(__FILE__), 'kwalify.schema.yaml')
30
+ META_SCHEMA = File.read(filename)
122
31
 
123
-
124
- def initialize(schema, &block)
125
- super
32
+ def self.instance()
33
+ unless @instance
34
+ schema = Kwalify::Yaml::Parser.new().parse(META_SCHEMA)
35
+ @instance = MetaValidator.new(schema)
126
36
  end
37
+ return @instance
38
+ end
127
39
 
128
- def validate_hook(value, rule, path, errors)
129
- return if value == nil ## realy?
130
- return unless rule.name == "MAIN"
131
- #
132
- hash = value
133
- type = hash['type']
134
- type = Types::DEFAULT_TYPE if type == nil
135
- klass = Types.type_class(type)
136
- #unless klass
137
- # errors << validate_error(:type_unknown, rule, "#{path}/type", type)
138
- #end
139
- #
140
- if hash.key?('classname')
141
- val = hash['classname']
142
- unless val.nil? || type == 'map'
143
- errors << validate_error(:classname_notmap, rule, "#{path}/classname", 'classname:')
144
- end
145
- end
146
- #
147
- if hash.key?('pattern')
148
- val = hash['pattern']
149
- pat = (val =~ /\A\/(.*)\/([mi]?[mi]?)\z/ ? $1 : val)
150
- begin
151
- Regexp.compile(pat)
152
- rescue RegexpError => ex
153
- errors << validate_error(:pattern_syntaxerr, rule, "#{path}/pattern", val)
154
- end
155
- end
156
- #
157
- if hash.key?('enum')
158
- if Types.collection_type?(type)
159
- errors << validate_error(:enum_notscalar, rule, path, 'enum:')
160
- else
161
- #elem_table = {}
162
- hash['enum'].each do |elem|
163
- #if elem_table[elem]
164
- # errors << validate_error(:enum_duplicate, rule, "#{path}/enum", elem.to_s)
165
- #end
166
- #elem_table[elem] = true
167
- unless elem.is_a?(klass)
168
- errors << validate_error(:enum_type_unmatch, rule, "#{path}/enum", elem, [Kwalify.word(type)])
169
- end
170
- end
171
- end
172
- end
173
- #
174
- if hash.key?('assert')
175
- val = hash['assert']
176
- #val =~ /\bval\b/ or errors << validate_error(:assert_noval, rule, "#{path}/assert", val)
177
- begin
178
- eval "proc { |val| #{val} }"
179
- rescue SyntaxError => ex
180
- errors << validate_error(:assert_syntaxerr, rule, "#{path}/assert", val)
181
- end
182
- end
183
- #
184
- if hash.key?('range')
185
- val = hash['range']
186
- curr_path = path + "/range"
187
- #if ! val.is_a?(Hash)
188
- # errors << validate_error(:range_notmap, rule, curr_path, val)
189
- #elsif ...
190
- if Types.collection_type?(type) || type == 'bool' || type == 'any'
191
- errors << validate_error(:range_notscalar, rule, path, 'range:')
192
- else
193
- val.each do |rkey, rval|
194
- #case rkey
195
- #when 'max', 'min', 'max-ex', 'min-ex'
196
- unless rval.is_a?(klass)
197
- typename = Kwalify.word(type) || type
198
- errors << validate_error(:range_type_unmatch, rule, "#{curr_path}/#{rkey}", rval, [typename])
199
- end
200
- #else
201
- # errors << validate_error(:range_undefined, rule, curr_path, "#{rkey}:")
202
- #end
203
- end
204
- end
205
- if val.key?('max') && val.key?('max-ex')
206
- errors << validate_error(:range_twomax, rule, curr_path, nil)
207
- end
208
- if val.key?('min') && val.key?('min-ex')
209
- errors << validate_error(:range_twomin, rule, curr_path, nil)
210
- end
211
- max, min, max_ex, min_ex = val['max'], val['min'], val['max-ex'], val['min-ex']
212
- if max
213
- if min && max < min
214
- errors << validate_error(:range_maxltmin, rule, curr_path, nil, [max, min])
215
- elsif min_ex && max <= min_ex
216
- errors << validate_error(:range_maxleminex, rule, curr_path, nil, [max, min_ex])
217
- end
218
- elsif max_ex
219
- if min && max_ex <= min
220
- errors << validate_error(:range_maxexlemin, rule, curr_path, nil, [max_ex, min])
221
- elsif min_ex && max_ex <= min_ex
222
- errors << validate_error(:range_maxexleminex, rule, curr_path, nil, [max_ex, min_ex])
223
- end
224
- end
225
- end
226
- #
227
- if hash.key?('length')
228
- val = hash['length']
229
- curr_path = path + "/length"
230
- #val.is_a?(Hash) or errors << validate_error(:length_notmap, rule, curr_path, val)
231
- unless type == 'str' || type == 'text'
232
- errors << validate_error(:length_nottext, rule, path, 'length:')
233
- end
234
- #val.each do |lkey, lval|
235
- # #case lkey
236
- # #when 'max', 'min', 'max-ex', 'min-ex'
237
- # unless lval.is_a?(Integer)
238
- # errors << validate_error(:length_notint, rule, "#{curr_path}/#{lkey}", lval)
239
- # end
240
- # #else
241
- # # errors << validate_error(:length_undefined, rule, curr_path, "#{lkey}:")
242
- # #end
40
+ def initialize(schema, &block)
41
+ super
42
+ end
43
+
44
+ def validate_hook(value, rule, path, errors)
45
+ return if value.nil? ## realy?
46
+ return unless rule.name == "MAIN"
47
+ #
48
+ hash = value
49
+ type = hash['type']
50
+ type = Types::DEFAULT_TYPE if type.nil?
51
+ klass = Types.type_class(type)
52
+ #unless klass
53
+ # errors << validate_error(:type_unknown, rule, "#{path}/type", type)
54
+ #end
55
+ #
56
+ if hash.key?('class')
57
+ val = hash['class']
58
+ unless val.nil? || type == 'map'
59
+ errors << validate_error(:class_notmap, rule, "#{path}/class", 'class:')
60
+ end
61
+ end
62
+ #
63
+ if hash.key?('pattern')
64
+ val = hash['pattern']
65
+ pat = (val =~ /\A\/(.*)\/([mi]?[mi]?)\z/ ? $1 : val)
66
+ begin
67
+ Regexp.compile(pat)
68
+ rescue RegexpError => ex
69
+ errors << validate_error(:pattern_syntaxerr, rule, "#{path}/pattern", val)
70
+ end
71
+ end
72
+ #
73
+ if hash.key?('enum')
74
+ if Types.collection_type?(type)
75
+ errors << validate_error(:enum_notscalar, rule, path, 'enum:')
76
+ else
77
+ #elem_table = {}
78
+ hash['enum'].each do |elem|
79
+ #if elem_table[elem]
80
+ # errors << validate_error(:enum_duplicate, rule, "#{path}/enum", elem.to_s)
243
81
  #end
244
- if val.key?('max') && val.key?('max-ex')
245
- errors << validate_error(:length_twomax, rule, curr_path, nil)
246
- end
247
- if val.key?('min') && val.key?('min-ex')
248
- errors << validate_error(:length_twomin, rule, curr_path, nil)
249
- end
250
- max, min, max_ex, min_ex = val['max'], val['min'], val['max-ex'], val['min-ex']
251
- if max
252
- if min && max < min
253
- errors << validate_error(:length_maxltmin, rule, curr_path, nil, [max, min])
254
- elsif min_ex && max <= min_ex
255
- errors << validate_error(:length_maxleminex, rule, curr_path, nil, [max, min_ex])
256
- end
257
- elsif max_ex
258
- if min && max_ex <= min
259
- errors << validate_error(:length_maxexlemin, rule, curr_path, nil, [max_ex, min])
260
- elsif min_ex && max_ex <= min_ex
261
- errors << validate_error(:length_maxexleminex, rule, curr_path, nil, [max_ex, min_ex])
262
- end
263
- end
264
- end
265
- #
266
- if hash.key?('unique')
267
- if hash['unique'] && Types.collection_type?(type)
268
- errors << validate_error(:unique_notscalar, rule, path, "unique:")
269
- end
270
- if path.empty?
271
- errors << validate_error(:unique_onroot, rule, "/", "unique:")
272
- end
273
- end
274
- #
275
- if hash.key?('ident')
276
- if hash['ident'] && Types.collection_type?(type)
277
- errors << validate_error(:ident_notscalar, rule, path, "ident:")
278
- end
279
- if path.empty?
280
- errors << validate_error(:ident_onroot, rule, "/", "ident:")
82
+ #elem_table[elem] = true
83
+ unless elem.is_a?(klass)
84
+ errors << validate_error(:enum_type_unmatch, rule, "#{path}/enum", elem, [Kwalify.word(type)])
281
85
  end
282
- end
283
- #
284
- if hash.key?('sequence')
285
- val = hash['sequence']
286
- #if val != nil && !val.is_a?(Array)
287
- # errors << validate_error(:sequence_notseq, rule, "#{path}/sequence", val)
288
- #elsif ...
289
- if val == nil || val.empty?
290
- errors << validate_error(:sequence_noelem, rule, "#{path}/sequence", val)
291
- elsif val.length > 1
292
- errors << validate_error(:sequence_toomany, rule, "#{path}/sequence", val)
293
- else
294
- elem = val[0]
295
- assert_error("elem.class=#{elem.class}") unless elem.is_a?(Hash)
296
- if elem['ident'] && elem['type'] != 'map'
297
- errors << validate_error(:ident_notmap, nil, "#{path}/sequence/0", 'ident:')
298
- end
299
- end
300
- end
301
- #
302
- if hash.key?('mapping')
303
- val = hash['mapping']
304
- if val != nil && !val.is_a?(Hash)
305
- errors << validate_error(:mapping_notmap, rule, "#{path}/mapping", val)
306
- elsif val == nil || (val.empty? && !val.default)
307
- errors << validate_error(:mapping_noelem, rule, "#{path}/mapping", val)
308
- end
309
- end
310
- #
311
- if type == 'seq'
312
- errors << validate_error(:seq_nosequence, rule, path, nil) unless hash.key?('sequence')
313
- #errors << validate_error(:seq_conflict, rule, path, 'enum:') if hash.key?('enum')
314
- errors << validate_error(:seq_conflict, rule, path, 'pattern:') if hash.key?('pattern')
315
- errors << validate_error(:seq_conflict, rule, path, 'mapping:') if hash.key?('mapping')
316
- #errors << validate_error(:seq_conflict, rule, path, 'range:') if hash.key?('range')
317
- #errors << validate_error(:seq_conflict, rule, path, 'length:') if hash.key?('length')
318
- elsif type == 'map'
319
- errors << validate_error(:map_nomapping, rule, path, nil) unless hash.key?('mapping')
320
- #errors << validate_error(:map_conflict, rule, path, 'enum:') if hash.key?('enum')
321
- errors << validate_error(:map_conflict, rule, path, 'pattern:') if hash.key?('pattern')
322
- errors << validate_error(:map_conflict, rule, path, 'sequence:') if hash.key?('sequence')
323
- #errors << validate_error(:map_conflict, rule, path, 'range:') if hash.key?('range')
324
- #errors << validate_error(:map_conflict, rule, path, 'length:') if hash.key?('length')
325
- else
326
- errors << validate_error(:scalar_conflict, rule, path, 'sequence:') if hash.key?('sequence')
327
- errors << validate_error(:scalar_conflict, rule, path, 'mapping:') if hash.key?('mapping')
328
- if hash.key?('enum')
329
- errors << validate_error(:enum_conflict, rule, path, 'range:') if hash.key?('range')
330
- errors << validate_error(:enum_conflict, rule, path, 'length:') if hash.key?('length')
331
- errors << validate_error(:enum_conflict, rule, path, 'pattern:') if hash.key?('pattern')
332
- end
333
- end
334
-
335
- end # end of def validate_hook()
336
-
337
-
338
- schema = YAML.load(META_SCHEMA)
339
- @instance = MetaValidator.new(schema)
340
-
341
- def self.instance()
342
- return @instance
86
+ end
87
+ end
88
+ end
89
+ #
90
+ if hash.key?('assert')
91
+ val = hash['assert']
92
+ #val =~ /\bval\b/ or errors << validate_error(:assert_noval, rule, "#{path}/assert", val)
93
+ begin
94
+ eval "proc { |val| #{val} }"
95
+ rescue ::SyntaxError => ex
96
+ errors << validate_error(:assert_syntaxerr, rule, "#{path}/assert", val)
97
+ end
98
+ end
99
+ #
100
+ if hash.key?('range')
101
+ val = hash['range']
102
+ curr_path = path + "/range"
103
+ #if ! val.is_a?(Hash)
104
+ # errors << validate_error(:range_notmap, rule, curr_path, val)
105
+ #elsif ...
106
+ if Types.collection_type?(type) || type == 'bool' || type == 'any'
107
+ errors << validate_error(:range_notscalar, rule, path, 'range:')
108
+ else
109
+ val.each do |rkey, rval|
110
+ #case rkey
111
+ #when 'max', 'min', 'max-ex', 'min-ex'
112
+ unless rval.is_a?(klass)
113
+ typename = Kwalify.word(type) || type
114
+ errors << validate_error(:range_type_unmatch, rule, "#{curr_path}/#{rkey}", rval, [typename])
115
+ end
116
+ #else
117
+ # errors << validate_error(:range_undefined, rule, curr_path, "#{rkey}:")
118
+ #end
119
+ end
120
+ end
121
+ if val.key?('max') && val.key?('max-ex')
122
+ errors << validate_error(:range_twomax, rule, curr_path, nil)
123
+ end
124
+ if val.key?('min') && val.key?('min-ex')
125
+ errors << validate_error(:range_twomin, rule, curr_path, nil)
126
+ end
127
+ max, min, max_ex, min_ex = val['max'], val['min'], val['max-ex'], val['min-ex']
128
+ if max
129
+ if min && max < min
130
+ errors << validate_error(:range_maxltmin, rule, curr_path, nil, [max, min])
131
+ elsif min_ex && max <= min_ex
132
+ errors << validate_error(:range_maxleminex, rule, curr_path, nil, [max, min_ex])
133
+ end
134
+ elsif max_ex
135
+ if min && max_ex <= min
136
+ errors << validate_error(:range_maxexlemin, rule, curr_path, nil, [max_ex, min])
137
+ elsif min_ex && max_ex <= min_ex
138
+ errors << validate_error(:range_maxexleminex, rule, curr_path, nil, [max_ex, min_ex])
139
+ end
140
+ end
141
+ end
142
+ #
143
+ if hash.key?('length')
144
+ val = hash['length']
145
+ curr_path = path + "/length"
146
+ #val.is_a?(Hash) or errors << validate_error(:length_notmap, rule, curr_path, val)
147
+ unless type == 'str' || type == 'text'
148
+ errors << validate_error(:length_nottext, rule, path, 'length:')
149
+ end
150
+ #val.each do |lkey, lval|
151
+ # #case lkey
152
+ # #when 'max', 'min', 'max-ex', 'min-ex'
153
+ # unless lval.is_a?(Integer)
154
+ # errors << validate_error(:length_notint, rule, "#{curr_path}/#{lkey}", lval)
155
+ # end
156
+ # #else
157
+ # # errors << validate_error(:length_undefined, rule, curr_path, "#{lkey}:")
158
+ # #end
159
+ #end
160
+ if val.key?('max') && val.key?('max-ex')
161
+ errors << validate_error(:length_twomax, rule, curr_path, nil)
162
+ end
163
+ if val.key?('min') && val.key?('min-ex')
164
+ errors << validate_error(:length_twomin, rule, curr_path, nil)
165
+ end
166
+ max, min, max_ex, min_ex = val['max'], val['min'], val['max-ex'], val['min-ex']
167
+ if max
168
+ if min && max < min
169
+ errors << validate_error(:length_maxltmin, rule, curr_path, nil, [max, min])
170
+ elsif min_ex && max <= min_ex
171
+ errors << validate_error(:length_maxleminex, rule, curr_path, nil, [max, min_ex])
172
+ end
173
+ elsif max_ex
174
+ if min && max_ex <= min
175
+ errors << validate_error(:length_maxexlemin, rule, curr_path, nil, [max_ex, min])
176
+ elsif min_ex && max_ex <= min_ex
177
+ errors << validate_error(:length_maxexleminex, rule, curr_path, nil, [max_ex, min_ex])
178
+ end
179
+ end
180
+ end
181
+ #
182
+ if hash.key?('unique')
183
+ if hash['unique'] && Types.collection_type?(type)
184
+ errors << validate_error(:unique_notscalar, rule, path, "unique:")
185
+ end
186
+ if path.empty?
187
+ errors << validate_error(:unique_onroot, rule, "/", "unique:")
188
+ end
189
+ end
190
+ #
191
+ if hash.key?('ident')
192
+ if hash['ident'] && Types.collection_type?(type)
193
+ errors << validate_error(:ident_notscalar, rule, path, "ident:")
194
+ end
195
+ if path.empty?
196
+ errors << validate_error(:ident_onroot, rule, "/", "ident:")
197
+ end
198
+ end
199
+ #
200
+ if hash.key?('default')
201
+ val = hash['default']
202
+ if Types.collection_type?(type)
203
+ errors << validate_error(:default_notscalar, rule, path, "default:")
204
+ elsif !val.nil? && !val.is_a?(klass)
205
+ errors << validate_error(:default_unmatch, rule, "#{path}/default", val, [Kwalify.word(type)])
206
+ end
343
207
  end
208
+ #
209
+ if hash.key?('sequence')
210
+ val = hash['sequence']
211
+ #if !val.nil? && !val.is_a?(Array)
212
+ # errors << validate_error(:sequence_notseq, rule, "#{path}/sequence", val)
213
+ #elsif ...
214
+ if val.nil? || val.empty?
215
+ errors << validate_error(:sequence_noelem, rule, "#{path}/sequence", val)
216
+ elsif val.length > 1
217
+ errors << validate_error(:sequence_toomany, rule, "#{path}/sequence", val)
218
+ else
219
+ elem = val[0]
220
+ assert_error("elem.class=#{elem.class}") unless elem.is_a?(Hash)
221
+ if elem['ident'] && elem['type'] != 'map'
222
+ errors << validate_error(:ident_notmap, nil, "#{path}/sequence/0", 'ident:')
223
+ end
224
+ end
225
+ end
226
+ #
227
+ if hash.key?('mapping')
228
+ val = hash['mapping']
229
+ if !val.nil? && !val.is_a?(Hash)
230
+ errors << validate_error(:mapping_notmap, rule, "#{path}/mapping", val)
231
+ elsif val.nil? || (val.empty? && !val.default)
232
+ errors << validate_error(:mapping_noelem, rule, "#{path}/mapping", val)
233
+ end
234
+ end
235
+ #
236
+ if type == 'seq'
237
+ errors << validate_error(:seq_nosequence, rule, path, nil) unless hash.key?('sequence')
238
+ #errors << validate_error(:seq_conflict, rule, path, 'enum:') if hash.key?('enum')
239
+ errors << validate_error(:seq_conflict, rule, path, 'pattern:') if hash.key?('pattern')
240
+ errors << validate_error(:seq_conflict, rule, path, 'mapping:') if hash.key?('mapping')
241
+ #errors << validate_error(:seq_conflict, rule, path, 'range:') if hash.key?('range')
242
+ #errors << validate_error(:seq_conflict, rule, path, 'length:') if hash.key?('length')
243
+ elsif type == 'map'
244
+ errors << validate_error(:map_nomapping, rule, path, nil) unless hash.key?('mapping')
245
+ #errors << validate_error(:map_conflict, rule, path, 'enum:') if hash.key?('enum')
246
+ errors << validate_error(:map_conflict, rule, path, 'pattern:') if hash.key?('pattern')
247
+ errors << validate_error(:map_conflict, rule, path, 'sequence:') if hash.key?('sequence')
248
+ #errors << validate_error(:map_conflict, rule, path, 'range:') if hash.key?('range')
249
+ #errors << validate_error(:map_conflict, rule, path, 'length:') if hash.key?('length')
250
+ else
251
+ errors << validate_error(:scalar_conflict, rule, path, 'sequence:') if hash.key?('sequence')
252
+ errors << validate_error(:scalar_conflict, rule, path, 'mapping:') if hash.key?('mapping')
253
+ if hash.key?('enum')
254
+ errors << validate_error(:enum_conflict, rule, path, 'range:') if hash.key?('range')
255
+ errors << validate_error(:enum_conflict, rule, path, 'length:') if hash.key?('length')
256
+ errors << validate_error(:enum_conflict, rule, path, 'pattern:') if hash.key?('pattern')
257
+ end
258
+ if hash.key?('default')
259
+ errors << validate_error(:default_conflict, rule, path, 'default:') if hash['required']
260
+ end
261
+ end
262
+
263
+ end # end of def validate_hook()
344
264
 
345
265
 
346
- end # end of class MetaValidator
266
+ end # end of class MetaValidator
347
267
 
348
268
 
349
- META_VALIDATOR = MetaValidator.instance()
269
+ META_VALIDATOR = MetaValidator.instance()
350
270
 
351
- def self.meta_validator # obsolete
352
- return META_VALIDATOR
353
- end
271
+ def self.meta_validator # obsolete
272
+ return META_VALIDATOR
273
+ end
354
274
 
355
275
  end