kwalify 0.6.1 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES.txt +232 -0
- data/MIT-LICENSE +20 -0
- data/README.txt +16 -19
- data/bin/kwalify +3 -3
- data/contrib/inline-require +6 -4
- data/contrib/kwalify +3719 -2427
- data/doc-api/classes/CommandOptionError.html +17 -17
- data/doc-api/classes/CommandOptionParser.html +63 -63
- data/doc-api/classes/Kwalify.html +29 -7
- data/doc-api/classes/Kwalify/AssertionError.html +9 -9
- data/doc-api/classes/Kwalify/BaseError.html +72 -71
- data/doc-api/classes/Kwalify/BaseParser.html +461 -0
- data/doc-api/classes/Kwalify/CommandOptionError.html +11 -11
- data/doc-api/classes/Kwalify/ErrorHelper.html +51 -46
- data/doc-api/classes/Kwalify/HashInterface.html +13 -135
- data/doc-api/classes/Kwalify/Json.html +105 -0
- data/doc-api/classes/Kwalify/Main.html +129 -126
- data/doc-api/classes/Kwalify/MetaValidator.html +248 -232
- data/doc-api/classes/Kwalify/Parser.html +12 -12
- data/doc-api/classes/Kwalify/PlainYamlParser.html +166 -163
- data/doc-api/classes/Kwalify/PlainYamlParser/Alias.html +11 -11
- data/doc-api/classes/Kwalify/Rule.html +152 -130
- data/doc-api/classes/Kwalify/SchemaError.html +10 -10
- data/doc-api/classes/Kwalify/SyntaxError.html +185 -0
- data/doc-api/classes/Kwalify/Types.html +26 -25
- data/doc-api/classes/Kwalify/Util.html +389 -0
- data/doc-api/classes/Kwalify/Util/HashLike.html +246 -0
- data/doc-api/classes/Kwalify/Util/OrderedHash.html +330 -0
- data/doc-api/classes/Kwalify/ValidationError.html +10 -10
- data/doc-api/classes/Kwalify/Validator.html +153 -86
- data/doc-api/classes/Kwalify/Yaml.html +181 -0
- data/doc-api/classes/Kwalify/Yaml/Parser.html +1538 -0
- data/doc-api/classes/Kwalify/YamlParser.html +190 -183
- data/doc-api/classes/Kwalify/YamlSyntaxError.html +8 -57
- data/doc-api/created.rid +1 -1
- data/doc-api/files/__/README_txt.html +17 -22
- data/doc-api/files/kwalify/errors_rb.html +2 -2
- data/doc-api/files/kwalify/main_rb.html +4 -3
- data/doc-api/files/kwalify/messages_rb.html +2 -2
- data/doc-api/files/kwalify/meta-validator_rb.html +3 -3
- data/doc-api/files/kwalify/{util/yaml-helper_rb.html → parser/base_rb.html} +8 -6
- data/doc-api/files/kwalify/parser/yaml_rb.html +117 -0
- data/doc-api/files/kwalify/rule_rb.html +2 -2
- data/doc-api/files/kwalify/types_rb.html +2 -2
- data/doc-api/files/kwalify/util/assert-text-equal_rb.html +2 -2
- data/doc-api/files/kwalify/util/hash-interface_rb.html +9 -2
- data/doc-api/files/kwalify/util/hashlike_rb.html +107 -0
- data/doc-api/files/kwalify/util/option-parser_rb.html +2 -2
- data/doc-api/files/kwalify/util/ordered-hash_rb.html +107 -0
- data/doc-api/files/kwalify/util/testcase-helper_rb.html +2 -2
- data/doc-api/files/kwalify/util_rb.html +107 -0
- data/doc-api/files/kwalify/validator_rb.html +2 -2
- data/doc-api/files/kwalify/yaml-parser_rb.html +2 -2
- data/doc-api/files/kwalify_rb.html +3 -2
- data/doc-api/fr_class_index.html +8 -1
- data/doc-api/fr_file_index.html +5 -1
- data/doc-api/fr_method_index.html +128 -69
- data/doc/img/fig01.png +0 -0
- data/doc/users-guide.html +882 -717
- data/examples/address-book/address-book.schema.yaml +2 -2
- data/examples/data-binding/BABEL.data.yaml +63 -0
- data/examples/data-binding/BABEL.schema.yaml +31 -0
- data/examples/data-binding/Makefile +8 -0
- data/examples/data-binding/Rakefile +13 -0
- data/examples/data-binding/main.rb +27 -0
- data/examples/invoice/invoice.schema.yaml +3 -3
- data/examples/tapkit/tapkit.schema.yaml +2 -2
- data/lib/kwalify.rb +41 -4
- data/lib/kwalify/errors.rb +118 -96
- data/lib/kwalify/kwalify.schema.yaml +58 -0
- data/lib/kwalify/main.rb +384 -377
- data/lib/kwalify/messages.rb +41 -27
- data/lib/kwalify/meta-validator.rb +251 -331
- data/lib/kwalify/parser/base.rb +127 -0
- data/lib/kwalify/parser/yaml.rb +837 -0
- data/lib/kwalify/rule.rb +545 -487
- data/lib/kwalify/templates/genclass-java.eruby +189 -162
- data/lib/kwalify/templates/genclass-php.eruby +104 -0
- data/lib/kwalify/templates/genclass-ruby.eruby +95 -66
- data/lib/kwalify/types.rb +107 -106
- data/lib/kwalify/util.rb +157 -0
- data/lib/kwalify/util/assert-text-equal.rb +33 -31
- data/lib/kwalify/util/hash-interface.rb +11 -30
- data/lib/kwalify/util/hashlike.rb +51 -0
- data/lib/kwalify/util/option-parser.rb +144 -144
- data/lib/kwalify/util/ordered-hash.rb +57 -0
- data/lib/kwalify/util/testcase-helper.rb +3 -3
- data/lib/kwalify/validator.rb +267 -212
- data/lib/kwalify/yaml-parser.rb +822 -768
- data/setup.rb +861 -607
- data/test/Rookbook.yaml +10 -0
- data/test/{tmp.dir/Context.java → data/users-guide/AddressBook.java.expected} +11 -11
- data/test/data/users-guide/BABEL.data.yaml +24 -0
- data/test/data/users-guide/BABEL.schema.yaml +30 -0
- data/test/data/users-guide/ExampleAddressBook.java +47 -0
- data/test/{tmp.dir/Group.java → data/users-guide/Group.java.expected} +2 -11
- data/test/data/users-guide/Person.java.expected +44 -0
- data/test/data/users-guide/address_book.rb +52 -0
- data/test/data/users-guide/address_book.schema.yaml +28 -0
- data/test/data/users-guide/address_book.yaml +27 -0
- data/test/data/users-guide/answers-schema.yaml +12 -0
- data/test/data/users-guide/answers-validator.rb +52 -0
- data/test/data/users-guide/babel_genclass.result +26 -0
- data/test/data/users-guide/config.schema.yaml +7 -0
- data/test/data/users-guide/config.yaml +4 -0
- data/test/{tmp.dir/silent1.document → data/users-guide/document01a.yaml} +0 -0
- data/test/data/users-guide/document01b.yaml +3 -0
- data/test/data/users-guide/document02a.yaml +4 -0
- data/test/data/users-guide/document02b.yaml +4 -0
- data/test/data/users-guide/document03a.yaml +6 -0
- data/test/data/users-guide/document03b.yaml +6 -0
- data/test/data/users-guide/document04a.yaml +9 -0
- data/test/data/users-guide/document04b.yaml +9 -0
- data/test/data/users-guide/document05a.yaml +11 -0
- data/test/data/users-guide/document05b.yaml +12 -0
- data/test/data/users-guide/document06a.yaml +15 -0
- data/test/data/users-guide/document06b.yaml +16 -0
- data/test/data/users-guide/document07a.yaml +9 -0
- data/test/data/users-guide/document07b.yaml +7 -0
- data/test/data/users-guide/document12a.json +10 -0
- data/test/data/users-guide/document12b.json +6 -0
- data/test/data/users-guide/document13a.yaml +17 -0
- data/test/data/users-guide/document14a.yaml +3 -0
- data/test/data/users-guide/document14b.yaml +3 -0
- data/test/data/users-guide/document15a.yaml +6 -0
- data/test/data/users-guide/document15b.yaml +5 -0
- data/test/data/users-guide/example_address_book.rb +10 -0
- data/test/data/users-guide/example_address_book_java.result +32 -0
- data/test/data/users-guide/example_address_book_ruby.result +31 -0
- data/test/data/users-guide/genclass_java.result +4 -0
- data/test/data/users-guide/howto-validation-with-parsing.rb +28 -0
- data/test/data/users-guide/howto-validation.rb +25 -0
- data/test/data/users-guide/howto3.rb +6 -0
- data/test/data/users-guide/howto3.result +5 -0
- data/test/data/users-guide/howto3.yaml +8 -0
- data/test/data/users-guide/howto5_databinding.result +111 -0
- data/test/data/users-guide/invalid01.result +3 -0
- data/test/data/users-guide/invalid02.result +5 -0
- data/test/data/users-guide/invalid03.result +5 -0
- data/test/data/users-guide/invalid04.result +4 -0
- data/test/data/users-guide/invalid05.result +11 -0
- data/test/data/users-guide/invalid06.result +4 -0
- data/test/data/users-guide/invalid07.result +3 -0
- data/test/data/users-guide/invalid08.result +3 -0
- data/test/data/users-guide/invalid12.json +8 -0
- data/test/data/users-guide/invalid14.result +4 -0
- data/test/data/users-guide/invalid15.result +4 -0
- data/test/data/users-guide/loadbabel.rb +27 -0
- data/test/data/users-guide/loadconfig.rb +15 -0
- data/test/data/users-guide/loadconfig.result +2 -0
- data/test/data/users-guide/models.rb +22 -0
- data/test/data/users-guide/option_ha.result +6 -0
- data/test/data/users-guide/option_ha_genclass_java.result +7 -0
- data/test/{tmp.dir/meta1.schema → data/users-guide/schema01.yaml} +0 -0
- data/test/data/users-guide/schema02.yaml +12 -0
- data/test/data/users-guide/schema03.yaml +9 -0
- data/test/data/users-guide/schema04.yaml +20 -0
- data/test/data/users-guide/schema05.yaml +29 -0
- data/test/data/users-guide/schema06.yaml +11 -0
- data/test/data/users-guide/schema12.json +12 -0
- data/test/data/users-guide/schema13.yaml +13 -0
- data/test/data/users-guide/schema14.yaml +5 -0
- data/test/data/users-guide/schema15.yaml +21 -0
- data/test/data/users-guide/valid01.result +2 -0
- data/test/data/users-guide/valid02.result +2 -0
- data/test/data/users-guide/valid03.result +2 -0
- data/test/data/users-guide/valid04.result +2 -0
- data/test/data/users-guide/valid05.result +2 -0
- data/test/data/users-guide/valid06.result +2 -0
- data/test/data/users-guide/valid07.result +2 -0
- data/test/data/users-guide/valid08.result +2 -0
- data/test/data/users-guide/valid12.result +2 -0
- data/test/data/users-guide/valid13.result +2 -0
- data/test/data/users-guide/valid14.result +2 -0
- data/test/data/users-guide/valid15.result +2 -0
- data/test/data/users-guide/validate08.rb +37 -0
- data/test/test-action.rb +78 -0
- data/test/test-action.yaml +738 -0
- data/test/test-databinding.rb +80 -0
- data/test/test-databinding.yaml +301 -0
- data/test/test-main.rb +129 -150
- data/test/test-main.yaml +126 -321
- data/test/test-metavalidator.rb +47 -47
- data/test/test-metavalidator.yaml +77 -21
- data/test/test-parser-yaml.rb +57 -0
- data/test/test-parser-yaml.yaml +1749 -0
- data/test/test-rule.rb +14 -15
- data/test/test-rule.yaml +6 -3
- data/test/test-users-guide.rb +75 -0
- data/test/test-validator.rb +77 -52
- data/test/test-validator.yaml +168 -6
- data/test/test-yaml-parser.rb +47 -0
- data/test/{test-yamlparser.yaml → test-yaml-parser.yaml} +159 -52
- data/test/test.rb +37 -21
- metadata +136 -37
- data/COPYING +0 -340
- data/ChangeLog +0 -70
- data/doc-api/classes/YamlHelper.html +0 -259
- data/lib/kwalify/util/yaml-helper.rb +0 -82
- data/test/test-yamlparser.rb +0 -58
- data/test/tmp.dir/User.java +0 -43
- data/test/tmp.dir/action1.document +0 -18
- data/test/tmp.dir/action1.schema +0 -32
- data/test/tmp.dir/action2.document +0 -18
- data/test/tmp.dir/action2.schema +0 -32
- data/test/tmp.dir/emacs.document +0 -6
- data/test/tmp.dir/emacs.schema +0 -6
- data/test/tmp.dir/meta1.document +0 -0
- data/test/tmp.dir/meta2.document +0 -0
- data/test/tmp.dir/meta2.schema +0 -3
- data/test/tmp.dir/silent1.schema +0 -3
- data/test/tmp.dir/silent2.document +0 -7
- data/test/tmp.dir/silent2.schema +0 -3
- data/test/tmp.dir/stream.invalid +0 -8
- data/test/tmp.dir/stream.schema +0 -3
- data/test/tmp.dir/stream.valid +0 -8
- data/test/tmp.dir/untabify.document +0 -5
- data/test/tmp.dir/untabify.schema +0 -10
- data/todo.txt +0 -34
@@ -0,0 +1,57 @@
|
|
1
|
+
###
|
2
|
+
### $Rev: 84 $
|
3
|
+
### 0.7.0
|
4
|
+
### $COPYRIGHT$
|
5
|
+
###
|
6
|
+
|
7
|
+
module Kwalify
|
8
|
+
|
9
|
+
module Util
|
10
|
+
|
11
|
+
class OrderedHash < Hash
|
12
|
+
|
13
|
+
def initialize(*args, &block)
|
14
|
+
super
|
15
|
+
@_keys = []
|
16
|
+
end
|
17
|
+
|
18
|
+
alias __set__ []=
|
19
|
+
|
20
|
+
def put(key, val)
|
21
|
+
@_keys << key unless self.key?(key)
|
22
|
+
__set__(key, val)
|
23
|
+
end
|
24
|
+
|
25
|
+
def add(key, val)
|
26
|
+
@_keys.delete_at(@_keys.index(key)) if self.key?(key)
|
27
|
+
@_keys << key
|
28
|
+
__set__(key, val)
|
29
|
+
end
|
30
|
+
|
31
|
+
alias []= put
|
32
|
+
#alias []= add
|
33
|
+
|
34
|
+
def keys
|
35
|
+
return @_keys.dup
|
36
|
+
end
|
37
|
+
|
38
|
+
def values
|
39
|
+
return @_keys.collect {|key| self[key] }
|
40
|
+
end
|
41
|
+
|
42
|
+
def delete(key)
|
43
|
+
@_keys.delete_at(@_keys.index(key)) if self.key?(key)
|
44
|
+
super
|
45
|
+
end
|
46
|
+
|
47
|
+
def each
|
48
|
+
@_keys.each do |key|
|
49
|
+
yield key, self[key]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
data/lib/kwalify/validator.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
###
|
2
|
-
### $Rev:
|
3
|
-
### $Release: 0.
|
4
|
-
### copyright(c) 2005 kuwata-lab all rights reserved.
|
2
|
+
### $Rev: 89 $
|
3
|
+
### $Release: 0.7.0 $
|
4
|
+
### copyright(c) 2005-2008 kuwata-lab all rights reserved.
|
5
5
|
###
|
6
6
|
|
7
7
|
require 'kwalify/messages'
|
@@ -11,214 +11,269 @@ require 'kwalify/rule'
|
|
11
11
|
|
12
12
|
module Kwalify
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
end
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
14
|
+
##
|
15
|
+
## validate YAML document
|
16
|
+
##
|
17
|
+
## ex1. validate yaml document
|
18
|
+
## schema = YAML.load_file('schema.yaml')
|
19
|
+
## validator = Kwalify::Validator.new(schema)
|
20
|
+
## document = YAML.load_file('document.yaml')
|
21
|
+
## erros = validator.validate(document)
|
22
|
+
## if errors && !errors.empty?
|
23
|
+
## errors.each do |err|
|
24
|
+
## puts "- [#{err.path}] #{err.message}"
|
25
|
+
## end
|
26
|
+
## end
|
27
|
+
##
|
28
|
+
## ex2. validate with parsing
|
29
|
+
## schema = YAML.load_file('schema.yaml')
|
30
|
+
## validator = Kwalify::Validator.new(schema)
|
31
|
+
## parser = Kwalify::Yaml::Parser.new(validator)
|
32
|
+
## document = parser.parse(File.read('document.yaml'))
|
33
|
+
## errors = parser.errors
|
34
|
+
## if errors && errors.empty?
|
35
|
+
## errors.each do |e|
|
36
|
+
## puts "#{e.linenum}:#{e.column} [#{e.path}] #{e.message}"
|
37
|
+
## end
|
38
|
+
## end
|
39
|
+
##
|
40
|
+
class Validator
|
41
|
+
include Kwalify::ErrorHelper
|
42
|
+
|
43
|
+
|
44
|
+
def initialize(hash_or_rule, &block)
|
45
|
+
obj = hash_or_rule
|
46
|
+
@rule = (obj.nil? || obj.is_a?(Rule)) ? obj : Rule.new(obj)
|
47
|
+
@block = block
|
48
|
+
end
|
49
|
+
attr_reader :rule
|
50
|
+
|
51
|
+
|
52
|
+
def _inspect
|
53
|
+
@rule._inspect
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
def validate(value)
|
58
|
+
path = ''; errors = []; done = {}; uniq_table = nil
|
59
|
+
_validate(value, @rule, path, errors, done, uniq_table)
|
60
|
+
return errors
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
protected
|
65
|
+
|
66
|
+
|
67
|
+
def validate_hook(value, rule, path, errors)
|
68
|
+
## may be overrided by subclass
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
public
|
73
|
+
|
74
|
+
|
75
|
+
def _validate(value, rule, path, errors, done, uniq_table, recursive=true)
|
76
|
+
#if Types.collection?(value)
|
77
|
+
if !Types.scalar?(value)
|
78
|
+
#if done[value.__id__]
|
79
|
+
# rule2 = done[value.__id__]
|
80
|
+
# if rule2.is_a?(Rule)
|
81
|
+
# return if rule.equal?(rule2)
|
82
|
+
# done[value.__id__] = [rule2, rule]
|
83
|
+
# elsif rule2.is_a?(Array)
|
84
|
+
# return if rule2.any? {|r| r.equal?(rule)}
|
85
|
+
# done[value.__id__] << rule
|
86
|
+
# else
|
87
|
+
# raise "unreachable"
|
88
|
+
# end
|
89
|
+
#end
|
90
|
+
return if done[value.__id__] # avoid infinite loop
|
91
|
+
done[value.__id__] = rule
|
92
|
+
end
|
93
|
+
if rule.required && value.nil?
|
94
|
+
#* key=:required_novalue msg="value required but none."
|
95
|
+
errors << validate_error(:required_novalue, rule, path, value)
|
96
|
+
return
|
97
|
+
end
|
98
|
+
if rule.type_class && !value.nil? && !value.is_a?(rule.type_class)
|
99
|
+
unless rule.classobj && value.is_a?(rule.classobj)
|
100
|
+
#* key=:type_unmatch msg="not a %s."
|
101
|
+
errors << validate_error(:type_unmatch, rule, path, value, [Kwalify.word(rule.type)])
|
102
|
+
return
|
103
|
+
end
|
104
|
+
end
|
105
|
+
#
|
106
|
+
n = errors.length
|
107
|
+
if rule.sequence
|
108
|
+
_validate_sequence(value, rule, path, errors, done, uniq_table, recursive)
|
109
|
+
elsif rule.mapping
|
110
|
+
_validate_mapping(value, rule, path, errors, done, uniq_table, recursive)
|
111
|
+
else
|
112
|
+
_validate_scalar(value, rule, path, errors, done, uniq_table)
|
113
|
+
end
|
114
|
+
return unless errors.length == n
|
115
|
+
#
|
116
|
+
validate_hook(value, rule, path, errors)
|
117
|
+
@block.call(value, rule, path, errors) if @block
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
private
|
122
|
+
|
123
|
+
|
124
|
+
def _validate_sequence(list, seq_rule, path, errors, done, uniq_table, recursive=true)
|
125
|
+
assert_error("seq_rule.sequence.class==#{seq_rule.sequence.class.name} (expected Array)") unless seq_rule.sequence.is_a?(Array)
|
126
|
+
assert_error("seq_rule.sequence.length==#{seq_rule.sequence.length} (expected 1)") unless seq_rule.sequence.length == 1
|
127
|
+
return if list.nil? || !recursive
|
128
|
+
rule = seq_rule.sequence[0]
|
129
|
+
uniq_table = rule._uniqueness_check_table()
|
130
|
+
list.each_with_index do |val, i|
|
131
|
+
child_path = path.is_a?(Array) ? path + [i] : "#{path}/#{i}"
|
132
|
+
_validate(val, rule, child_path, errors, done, uniq_table) ## validate recursively
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
|
137
|
+
def _validate_mapping(hash, map_rule, path, errors, done, uniq_table, recursive=true)
|
138
|
+
assert_error("map_rule.mapping.class==#{map_rule.mapping.class.name} (expected Hash)") unless map_rule.mapping.is_a?(Hash)
|
139
|
+
return if hash.nil?
|
140
|
+
return if !recursive
|
141
|
+
_validate_mapping_required_keys(hash, map_rule, path, errors)
|
142
|
+
hash.each do |key, val|
|
143
|
+
rule = map_rule.mapping[key]
|
144
|
+
child_path = path.is_a?(Array) ? path+[key] : "#{path}/#{key}"
|
145
|
+
unless rule
|
146
|
+
#* key=:key_undefined msg="key '%s' is undefined."
|
147
|
+
errors << validate_error(:key_undefined, rule, child_path, hash, ["#{key}:"])
|
148
|
+
##* key=:key_undefined msg="undefined key."
|
149
|
+
#errors << validate_error(:key_undefined, rule, child_path, "#{key}:")
|
150
|
+
else
|
151
|
+
_validate(val, rule, child_path, errors, done,
|
152
|
+
uniq_table ? uniq_table[key] : nil) ## validate recursively
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
def _validate_mapping_required_keys(hash, map_rule, path, errors) #:nodoc:
|
159
|
+
map_rule.mapping.each do |key, rule|
|
160
|
+
#next unless rule.required
|
161
|
+
#val = hash.is_a?(Hash) ? hash[key] : hash.instance_variable_get("@#{key}")
|
162
|
+
#if val.nil?
|
163
|
+
if rule.required && hash[key].nil? # or !hash.key?(key)
|
164
|
+
#* key=:required_nokey msg="key '%s:' is required."
|
165
|
+
errors << validate_error(:required_nokey, rule, path, hash, [key])
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
public :_validate_mapping_required_keys
|
170
|
+
|
171
|
+
|
172
|
+
def _validate_scalar(value, rule, path, errors, done, uniq_table)
|
173
|
+
assert_error("rule.sequence.class==#{rule.sequence.class.name} (expected NilClass)") if rule.sequence
|
174
|
+
assert_error("rule.mapping.class==#{rule.mapping.class.name} (expected NilClass)") if rule.mapping
|
175
|
+
_validate_assert( value, rule, path, errors) if rule.assert_proc
|
176
|
+
_validate_enum( value, rule, path, errors) if rule.enum
|
177
|
+
return if value.nil?
|
178
|
+
_validate_pattern(value, rule, path, errors) if rule.pattern
|
179
|
+
_validate_range( value, rule, path, errors) if rule.range
|
180
|
+
_validate_length( value, rule, path, errors) if rule.length
|
181
|
+
_validate_unique( value, rule, path, errors, uniq_table) if uniq_table
|
182
|
+
end
|
183
|
+
|
184
|
+
|
185
|
+
def _validate_unique(value, rule, path, errors, uniq_table)
|
186
|
+
assert_error "uniq_table=#{uniq_table.inspect}" unless rule.unique || rule.ident
|
187
|
+
if uniq_table.key?(value)
|
188
|
+
exist_at = uniq_table[value]
|
189
|
+
exist_at = "/#{exist_at.join('/')}" if exist_at.is_a?(Array)
|
190
|
+
#* key=:value_notunique msg="is already used at '%s'."
|
191
|
+
errors << validate_error(:value_notunique, rule, path, value, exist_at)
|
192
|
+
else
|
193
|
+
uniq_table[value] = path.dup
|
194
|
+
end
|
195
|
+
end
|
196
|
+
public :_validate_unique
|
197
|
+
|
198
|
+
|
199
|
+
def _validate_assert(value, rule, path, errors)
|
200
|
+
assert_error("rule=#{rule._inspect}") unless rule.assert_proc
|
201
|
+
unless rule.assert_proc.call(value)
|
202
|
+
#* key=:assert_failed msg="assertion expression failed (%s)."
|
203
|
+
errors << validate_error(:assert_failed, rule, path, value, [rule.assert])
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
|
208
|
+
def _validate_enum(value, rule, path, errors)
|
209
|
+
assert_error("rule=#{rule._inspect}") unless rule.enum
|
210
|
+
unless rule.enum.include?(value)
|
211
|
+
keyname = path.is_a?(Array) ? path[-1] : File.basename(path)
|
212
|
+
keyname = 'enum' if keyname =~ /\A\d+\z/
|
213
|
+
#* key=:enum_notexist msg="invalid %s value."
|
214
|
+
errors << validate_error(:enum_notexist, rule, path, value, [keyname])
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
|
219
|
+
def _validate_pattern(value, rule, path, errors)
|
220
|
+
assert_error("rule=#{rule._inspect}") unless rule.pattern
|
221
|
+
unless value.to_s =~ rule.regexp
|
222
|
+
#* key=:pattern_unmatch msg="not matched to pattern %s."
|
223
|
+
errors << validate_error(:pattern_unmatch, rule, path, value, [rule.pattern])
|
224
|
+
end
|
225
|
+
end
|
226
|
+
|
227
|
+
|
228
|
+
def _validate_range(value, rule, path, errors)
|
229
|
+
assert_error("rule=#{rule._inspect}") unless rule.range
|
230
|
+
assert_error("value.class=#{value.class.name}") unless Types.scalar?(value)
|
231
|
+
h = rule.range
|
232
|
+
max, min, max_ex, min_ex = h['max'], h['min'], h['max-ex'], h['min-ex']
|
233
|
+
if max && max < value
|
234
|
+
#* key=:range_toolarge msg="too large (> max %s)."
|
235
|
+
errors << validate_error(:range_toolarge, rule, path, value, [max.to_s])
|
236
|
+
end
|
237
|
+
if min && min > value
|
238
|
+
#* key=:range_toosmall msg="too small (< min %s)."
|
239
|
+
errors << validate_error(:range_toosmall, rule, path, value, [min.to_s])
|
240
|
+
end
|
241
|
+
if max_ex && max_ex <= value
|
242
|
+
#* key=:range_toolargeex msg="too large (>= max %s)."
|
243
|
+
errors << validate_error(:range_toolargeex, rule, path, value, [max_ex.to_s])
|
244
|
+
end
|
245
|
+
if min_ex && min_ex >= value
|
246
|
+
#* key=:range_toosmallex msg="too small (<= min %s)."
|
247
|
+
errors << validate_error(:range_toosmallex, rule, path, value, [min_ex.to_s])
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
|
252
|
+
def _validate_length(value, rule, path, errors)
|
253
|
+
assert_error("rule=#{rule._inspect}") unless rule.length
|
254
|
+
assert_error("value.class=#{value.class.name}") unless value.is_a?(String) || value.is_a?(Text)
|
255
|
+
len = value.to_s.length
|
256
|
+
h = rule.length
|
257
|
+
max, min, max_ex, min_ex = h['max'], h['min'], h['max-ex'], h['min-ex']
|
258
|
+
if max && max < len
|
259
|
+
#* key=:length_toolong msg="too long (length %d > max %d)."
|
260
|
+
errors << validate_error(:length_toolong, rule, path, value, [len, max])
|
261
|
+
end
|
262
|
+
if min && min > len
|
263
|
+
#* key=:length_tooshort msg="too short (length %d < min %d)."
|
264
|
+
errors << validate_error(:length_tooshort, rule, path, value, [len, min])
|
265
|
+
end
|
266
|
+
if max_ex && max_ex <= len
|
267
|
+
#* key=:length_toolongex msg="too long (length %d >= max %d)."
|
268
|
+
errors << validate_error(:length_toolongex, rule, path, value, [len, max_ex])
|
269
|
+
end
|
270
|
+
if min_ex && min_ex >= len
|
271
|
+
#* key=:length_tooshortex msg="too short (length %d <= min %d)."
|
272
|
+
errors << validate_error(:length_tooshortex, rule, path, value, [len, min_ex])
|
273
|
+
end
|
274
|
+
end
|
275
|
+
|
276
|
+
|
277
|
+
end
|
223
278
|
|
224
279
|
end
|