kwalify 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/ChangeLog +29 -62
- data/README.txt +39 -11
- data/bin/kwalify +16 -177
- data/doc/users-guide.html +279 -145
- data/examples/address-book/Makefile +1 -0
- data/examples/address-book/address-book.schema.yaml +10 -10
- data/examples/invoice/Makefile +1 -0
- data/examples/invoice/invoice.schema.yaml +17 -34
- data/examples/tapkit/Makefile +5 -0
- data/examples/tapkit/tapkit.schema.yaml +137 -0
- data/examples/tapkit/tapkit.yaml +85 -0
- data/lib/kwalify.rb +12 -3
- data/lib/kwalify/errors.rb +36 -60
- data/lib/kwalify/main-program.rb +214 -0
- data/lib/kwalify/messages.rb +91 -0
- data/lib/kwalify/meta-validator.rb +199 -76
- data/lib/kwalify/rule.rb +326 -0
- data/lib/kwalify/types.rb +86 -20
- data/lib/kwalify/util/assert-diff.rb +1 -1
- data/lib/kwalify/util/option-parser.rb +1 -1
- data/lib/kwalify/util/yaml-helper.rb +6 -6
- data/lib/kwalify/validator.rb +111 -218
- data/test/test-metavalidator.rb +608 -0
- data/test/test-metavalidator.rb.error +490 -0
- data/test/test-validator.rb +526 -0
- data/test/test.rb +12 -351
- data/todo.txt +14 -4
- metadata +12 -3
- data/lib/kwalify/error-msg.rb +0 -41
@@ -0,0 +1,214 @@
|
|
1
|
+
###
|
2
|
+
### $Rev: 18 $
|
3
|
+
### $Release: 0.2.0 $
|
4
|
+
### copyright(c) 2005 kuwata-lab all rights reserved.
|
5
|
+
###
|
6
|
+
|
7
|
+
require 'yaml'
|
8
|
+
require 'kwalify'
|
9
|
+
require 'kwalify/util/yaml-helper'
|
10
|
+
require 'kwalify/util/option-parser'
|
11
|
+
|
12
|
+
|
13
|
+
module Kwalify
|
14
|
+
|
15
|
+
##
|
16
|
+
## ex.
|
17
|
+
## command = File.basename($0)
|
18
|
+
## begin
|
19
|
+
## main = Kwalify::MainProgram.new(ARGV, command)
|
20
|
+
## s = main.execute
|
21
|
+
## print s if s
|
22
|
+
## rescue CommandOptionError => ex
|
23
|
+
## $stderr.puts "ERROR: #{ex.message}"
|
24
|
+
## exit 1
|
25
|
+
## rescue Kwalify::KwalifyError => ex
|
26
|
+
## $stderr.puts "ERROR: #{ex.message}"
|
27
|
+
## exit 1
|
28
|
+
## end
|
29
|
+
##
|
30
|
+
class MainProgram
|
31
|
+
|
32
|
+
def initialize(argv=ARGV, command=nil)
|
33
|
+
@command = command || File.basename($0)
|
34
|
+
@options, @proerties = _parse_argv(argv)
|
35
|
+
@filenames = argv
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
def execute(filenames=@filenames)
|
40
|
+
if @options[:help] || @options[:version]
|
41
|
+
s = ''
|
42
|
+
s << _version() if @options[:version]
|
43
|
+
s << _usage() if @options[:help]
|
44
|
+
return s
|
45
|
+
end
|
46
|
+
if @options[:meta] || @options[:meta2]
|
47
|
+
s = _meta_validate(filenames)
|
48
|
+
elsif @options[:schema]
|
49
|
+
if @options[:debug]
|
50
|
+
s = _inspect_schema(@options[:schema])
|
51
|
+
else
|
52
|
+
s = _validate(filenames)
|
53
|
+
end
|
54
|
+
else
|
55
|
+
#* key=:action_required msg="command-line option '-f' or '-m' required."
|
56
|
+
raise CommandOptionError.new(nil, nil, Kwalify.msg(:action_required))
|
57
|
+
end
|
58
|
+
return s # or return (s == nil || s.empty?) ? nil : s
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
|
65
|
+
def _meta_validate(filenames)
|
66
|
+
filenames = [ nil ] if filenames.empty?
|
67
|
+
meta_validator = @options[:meta2] ? nil : Kwalify.meta_validator()
|
68
|
+
flag_no_validate = @options[:meta2] == true
|
69
|
+
s = ''
|
70
|
+
filenames.each do |filename|
|
71
|
+
s << __meta_validate(meta_validator, filename)
|
72
|
+
end
|
73
|
+
return s
|
74
|
+
end
|
75
|
+
|
76
|
+
|
77
|
+
def __meta_validate(meta_validator, filename=nil)
|
78
|
+
str = filename ? File.open(filename) { |f| f.read() } : $stdin.read()
|
79
|
+
str = YamlHelper.untabify(tr) if @options[:untabify]
|
80
|
+
schema = YAML.load(str)
|
81
|
+
schema = schema.value if schema.is_a?(YAML::Syck::DomainType)
|
82
|
+
filename ||= "(stdin)"
|
83
|
+
if !schema || schema.empty?
|
84
|
+
#* key=:meta_schema_empty msg="%s: empty.\n"
|
85
|
+
return Kwalify.msg(:meta_schema_emtpy) % filename
|
86
|
+
end
|
87
|
+
errors = nil
|
88
|
+
if meta_validator
|
89
|
+
errors = meta_validator.validate(schema)
|
90
|
+
else
|
91
|
+
begin
|
92
|
+
validator = Kwalify::Validator.new(schema) # error raised when schema is wrong
|
93
|
+
rescue Kwalify::SchemaError => ex
|
94
|
+
errors = [ ex ]
|
95
|
+
end
|
96
|
+
end
|
97
|
+
s = ''
|
98
|
+
if !errors || errors.empty?
|
99
|
+
#* key=:meta_validation_valid msg="%s: ok.\n"
|
100
|
+
s << (Kwalify.msg(:meta_validation_valid) % filename) unless @options[:silent]
|
101
|
+
else
|
102
|
+
#* key=:meta_validation_invalid msg="%s: NG!\n"
|
103
|
+
s << (Kwalify.msg(:meta_validation_invalid) % filename)
|
104
|
+
errors.each { |error| s << " - [#{error.path}] #{error.message}\n" }
|
105
|
+
end
|
106
|
+
return s
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
def _validate(filenames)
|
111
|
+
schema_filename = @options[:schema]
|
112
|
+
str = File.open(schema_filename) { |f| f.read() }
|
113
|
+
str = YamlHelper.untabify(str) if @options[:untabify]
|
114
|
+
schema = YAML.load(str)
|
115
|
+
schema = schema.value if schema.is_a?(YAML::Syck::DomainType)
|
116
|
+
validator = Kwalify::Validator.new(schema)
|
117
|
+
#
|
118
|
+
filenames = [ nil ] if filenames.empty?
|
119
|
+
s = ''
|
120
|
+
filenames.each do |filename|
|
121
|
+
s << __validate(validator, filename)
|
122
|
+
end
|
123
|
+
return s
|
124
|
+
end
|
125
|
+
|
126
|
+
|
127
|
+
def __validate(validator, filename=nil)
|
128
|
+
str = filename ? File.open(filename) { |f| f.read() } : $stdin.read()
|
129
|
+
str = YamlHelper.untabify(str) if @options[:untabify]
|
130
|
+
stream = YAML::load_stream(str)
|
131
|
+
filename ||= "(stdin)"
|
132
|
+
i = -1 # or 0? *index*
|
133
|
+
s = ''
|
134
|
+
stream.documents.each do |document|
|
135
|
+
i += 1
|
136
|
+
document = document.value if document.is_a?(YAML::Syck::DomainType)
|
137
|
+
if !document || document.empty?
|
138
|
+
#* key=:schema_empty msg="%s#%d: empty.\n"
|
139
|
+
s << (Kwalify.msg(:schema_empty) % i)
|
140
|
+
next
|
141
|
+
end
|
142
|
+
errors = validator.validate(document)
|
143
|
+
if errors.empty?
|
144
|
+
#* key=:validation_valid msg="%s#%d: valid.\n"
|
145
|
+
s << (Kwalify.msg(:validation_valid) % [filename, i]) unless @options[:silent]
|
146
|
+
else
|
147
|
+
#* key=:validation_invalid msg="%s#%d: INVALID\n"
|
148
|
+
s << (Kwalify.msg(:validation_invalid) % [filename, i])
|
149
|
+
errors.each { |error| s << " - [#{error.path}] #{error.message}\n" }
|
150
|
+
end
|
151
|
+
end
|
152
|
+
return s
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
def _inspect_schema(schema_filename)
|
157
|
+
filename = schema_filename
|
158
|
+
str = filename ? File.open(filename) { |f| f.read() } : $stdin.read()
|
159
|
+
str = YamlHelper.untabify(tr) if @options[:untabify]
|
160
|
+
schema = YAML.load(str)
|
161
|
+
schema = schema.value if schema.is_a?(YAML::Syck::DomainType)
|
162
|
+
return nil if !schema || schema.empty?
|
163
|
+
validator = Kwalify::Validator.new(schema) # error raised when schema is wrong
|
164
|
+
s = validator._inspect()
|
165
|
+
s << "\n" unless s[-1] == ?\n
|
166
|
+
return s
|
167
|
+
end
|
168
|
+
|
169
|
+
|
170
|
+
def _usage()
|
171
|
+
s = ''
|
172
|
+
s << "Usage1: #{@command} [-hvst] -f schema.yaml document.yaml [document2.yaml ...]\n"
|
173
|
+
s << "Usage2: #{@command} [-hvst] -m schema.yaml [schema2.yaml ...]\n"
|
174
|
+
s << " -h, --help : help\n"
|
175
|
+
s << " -v : version\n"
|
176
|
+
s << " -s : silent\n"
|
177
|
+
s << " -f schema.yaml : schema definition file\n"
|
178
|
+
s << " -m : meta-validation mode\n"
|
179
|
+
s << " -t : expand tab character automatically\n"
|
180
|
+
return s
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
def _version()
|
185
|
+
return "Kwalify: #{RELEASE}\n"
|
186
|
+
end
|
187
|
+
|
188
|
+
|
189
|
+
def _parse_argv(argv)
|
190
|
+
opt_parser = CommandOptionParser.new("hvstmMD", "f")
|
191
|
+
#opts, props = opt_parser.parse(argv)
|
192
|
+
#properties = {}
|
193
|
+
#props.each do |pname, pvalue|
|
194
|
+
# key = pname.gsub(/-/, '_').intern
|
195
|
+
# value = value == nil ? true: CommandOptionParser.to_value(pvalue)
|
196
|
+
# properties[key] = value
|
197
|
+
#end
|
198
|
+
auto_convert = true
|
199
|
+
opts, properties = opt_parser.parse(argv, auto_convert)
|
200
|
+
options = {}
|
201
|
+
options[:help] = opts[?h] || properties[:help]
|
202
|
+
options[:version] = opts[?v] || properties[:version]
|
203
|
+
options[:silent] = opts[?s]
|
204
|
+
options[:schema] = opts[?f]
|
205
|
+
options[:meta] = opts[?m]
|
206
|
+
options[:meta2] = opts[?M]
|
207
|
+
options[:untabify] = opts[?t]
|
208
|
+
options[:debug] = opts[?D]
|
209
|
+
return options, properties
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
###
|
2
|
+
### $Rev: 18 $
|
3
|
+
### $Release: 0.2.0 $
|
4
|
+
### copyright(c) 2005 kuwata-lab all rights reserved.
|
5
|
+
###
|
6
|
+
|
7
|
+
module Kwalify
|
8
|
+
|
9
|
+
@@messages = {}
|
10
|
+
|
11
|
+
def self.msg(key)
|
12
|
+
return @@messages[key]
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
##----- begin
|
17
|
+
# filename: lib/kwalify/main-program.rb
|
18
|
+
@@messages[:action_required] = "command-line option '-f' or '-m' required."
|
19
|
+
@@messages[:meta_schema_empty] = "%s: empty.\n"
|
20
|
+
@@messages[:meta_validation_invalid] = "%s: NG!\n"
|
21
|
+
@@messages[:meta_validation_valid] = "%s: ok.\n"
|
22
|
+
@@messages[:meta_validation_invalid2] = "%s: NG! %s\n"
|
23
|
+
@@messages[:schema_empty] = "%s#%d: empty.\n"
|
24
|
+
@@messages[:validation_valid] = "%s#%d: valid.\n"
|
25
|
+
@@messages[:validation_invalid] = "%s#%d: INVALID\n"
|
26
|
+
# --
|
27
|
+
# filename: lib/kwalify/rule.rb
|
28
|
+
@@messages[:schema_notmap] = "schema definition is not a mapping."
|
29
|
+
@@messages[:type_notstr] = "not a string."
|
30
|
+
@@messages[:type_unknown] = "unknown type."
|
31
|
+
@@messages[:required_notbool] = "not a boolean."
|
32
|
+
@@messages[:pattern_syntaxerr] = "has regexp error."
|
33
|
+
@@messages[:enum_notseq] = "not a sequence."
|
34
|
+
@@messages[:enum_notscalar] = "not available with seq or map."
|
35
|
+
@@messages[:enum_duplicate] = "duplicated enum value."
|
36
|
+
@@messages[:enum_type_unmatch] = "%s type expected."
|
37
|
+
@@messages[:assert_notstr] = "not a string."
|
38
|
+
@@messages[:assert_noval] = "'val' is not used."
|
39
|
+
@@messages[:assert_syntaxerr] = "expression syntax error."
|
40
|
+
@@messages[:range_notmap] = "not a mapping."
|
41
|
+
@@messages[:range_notscalar] = "is available only with scalar type."
|
42
|
+
@@messages[:range_type_unmatch] = "not a %s."
|
43
|
+
@@messages[:range_undefined] = "undefined key."
|
44
|
+
@@messages[:length_notmap] = "not a mapping."
|
45
|
+
@@messages[:length_nottext] = "is available only with string or text."
|
46
|
+
@@messages[:length_notint] = "not an integer."
|
47
|
+
@@messages[:length_undefined] = "undefined key."
|
48
|
+
@@messages[:sequence_notseq] = "not a sequence."
|
49
|
+
@@messages[:sequence_noelem] = "required one element."
|
50
|
+
@@messages[:sequence_toomany] = "required just one element."
|
51
|
+
@@messages[:mapping_notmap] = "not a mapping."
|
52
|
+
@@messages[:mapping_noelem] = "required at least one element."
|
53
|
+
@@messages[:key_unknown] = "unknown key."
|
54
|
+
@@messages[:seq_nosequence] = "type 'seq' requires 'sequence:'."
|
55
|
+
@@messages[:seq_conflict] = "not available with sequence."
|
56
|
+
@@messages[:map_nomapping] = "type 'map' requires 'mapping:'."
|
57
|
+
@@messages[:map_conflict] = "not available with mapping."
|
58
|
+
@@messages[:scalar_conflict] = "not available with scalar type."
|
59
|
+
@@messages[:enum_conflict] = "not available with 'enum:'."
|
60
|
+
# --
|
61
|
+
# filename: lib/kwalify/validator.rb
|
62
|
+
@@messages[:required_novalue] = "value required but none."
|
63
|
+
@@messages[:type_unmatch] = "not a %s."
|
64
|
+
@@messages[:assert_failed] = "assertion expression failed (%s)."
|
65
|
+
@@messages[:enum_notexist] = "invalid %s value."
|
66
|
+
@@messages[:pattern_unmatch] = "not matched to pattern %s."
|
67
|
+
@@messages[:range_toolarge] = "too large (> max %s)."
|
68
|
+
@@messages[:range_toosmall] = "too small (< min %s)."
|
69
|
+
@@messages[:length_toolong] = "too long (length %d > max %d)."
|
70
|
+
@@messages[:length_tooshort] = "too short (length %d < min %d)."
|
71
|
+
@@messages[:required_nokey] = "key '%s:' is required."
|
72
|
+
@@messages[:key_undefined] = "key '%s' is undefined."
|
73
|
+
# --
|
74
|
+
##----- end
|
75
|
+
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
@@words = {}
|
80
|
+
|
81
|
+
def self.word(key)
|
82
|
+
return @@words[key] || key
|
83
|
+
end
|
84
|
+
|
85
|
+
@@words['str'] = 'string'
|
86
|
+
@@words['int'] = 'integer'
|
87
|
+
@@words['bool'] = 'boolean'
|
88
|
+
@@words['seq'] = 'sequence'
|
89
|
+
@@words['map'] = 'mapping'
|
90
|
+
|
91
|
+
end
|
@@ -1,137 +1,260 @@
|
|
1
1
|
###
|
2
|
-
### $Rev:
|
3
|
-
### $Release: 0.
|
2
|
+
### $Rev: 18 $
|
3
|
+
### $Release: 0.2.0 $
|
4
4
|
### copyright(c) 2005 kuwata-lab all rights reserved.
|
5
5
|
###
|
6
6
|
|
7
7
|
require 'kwalify/errors'
|
8
|
+
require 'kwalify/rule'
|
8
9
|
require 'kwalify/validator'
|
9
10
|
require 'yaml'
|
10
11
|
|
11
12
|
module Kwalify
|
12
13
|
|
13
|
-
|
14
|
-
|
14
|
+
META_SCHEMA = <<'END'
|
15
15
|
name: MAIN
|
16
16
|
type: map
|
17
17
|
required: yes
|
18
|
-
mapping: &main-
|
18
|
+
mapping: &main-rule
|
19
19
|
"name":
|
20
|
-
type:
|
20
|
+
type: str
|
21
21
|
"desc":
|
22
|
-
type:
|
22
|
+
type: str
|
23
23
|
"type":
|
24
|
-
type:
|
24
|
+
type: str
|
25
|
+
#required: yes
|
25
26
|
enum:
|
26
27
|
- seq
|
27
|
-
|
28
|
+
#- sequence
|
28
29
|
#- list
|
29
30
|
- map
|
30
|
-
|
31
|
+
#- mapping
|
31
32
|
#- hash
|
32
|
-
-
|
33
|
-
|
33
|
+
- str
|
34
|
+
#- string
|
35
|
+
- int
|
36
|
+
#- integer
|
34
37
|
- float
|
35
38
|
- number
|
36
|
-
|
39
|
+
#- numeric
|
37
40
|
- bool
|
38
|
-
|
41
|
+
#- boolean
|
39
42
|
- text
|
40
43
|
- date
|
41
44
|
- time
|
42
|
-
-
|
45
|
+
- timestamp
|
46
|
+
#- object
|
43
47
|
- any
|
48
|
+
- scalar
|
49
|
+
#- collection
|
44
50
|
"required":
|
45
|
-
type:
|
51
|
+
type: bool
|
46
52
|
"enum":
|
47
53
|
type: seq
|
48
54
|
sequence:
|
49
|
-
- type:
|
55
|
+
- type: scalar
|
50
56
|
"pattern":
|
51
|
-
type:
|
57
|
+
type: str
|
58
|
+
"assert":
|
59
|
+
type: str
|
60
|
+
pattern: /\bval\b/
|
61
|
+
"range":
|
62
|
+
type: map
|
63
|
+
mapping:
|
64
|
+
"max":
|
65
|
+
type: scalar
|
66
|
+
"min":
|
67
|
+
type: scalar
|
68
|
+
"length":
|
69
|
+
type: map
|
70
|
+
mapping:
|
71
|
+
"max":
|
72
|
+
type: int
|
73
|
+
"min":
|
74
|
+
type: int
|
52
75
|
"sequence":
|
53
76
|
name: SEQUENCE
|
54
77
|
type: seq
|
55
78
|
sequence:
|
56
79
|
- type: map
|
57
|
-
mapping: *main-
|
80
|
+
mapping: *main-rule
|
58
81
|
name: MAIN
|
82
|
+
#required: yes
|
59
83
|
"mapping":
|
60
84
|
name: MAPPING
|
61
85
|
type: map
|
62
86
|
mapping:
|
63
|
-
|
87
|
+
'*':
|
64
88
|
type: map
|
65
|
-
mapping: *main-
|
89
|
+
mapping: *main-rule
|
66
90
|
name: MAIN
|
67
|
-
|
91
|
+
#required: yes
|
68
92
|
END
|
69
93
|
|
70
94
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
95
|
+
##
|
96
|
+
## ex.
|
97
|
+
## meta_validator = Kwalify.meta_validator()
|
98
|
+
## schema = File.load_file('schema.yaml')
|
99
|
+
## errors << meta_validator.validate(schema)
|
100
|
+
## if !errors.empty?
|
101
|
+
## errors.each do |error|
|
102
|
+
## puts "[#{error.path}] #{error.message}"
|
103
|
+
## end
|
104
|
+
## end
|
105
|
+
##
|
106
|
+
class MetaValidator < Validator
|
107
|
+
def initialize(schema, &block)
|
108
|
+
super
|
109
|
+
end
|
77
110
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
111
|
+
def validate_hook(value, rule, path, errors)
|
112
|
+
return if value == nil ## realy?
|
113
|
+
return unless rule.name == "MAIN"
|
114
|
+
#
|
115
|
+
hash = value
|
116
|
+
type = hash['type']
|
117
|
+
type = Kwalify::DEFAULT_TYPE if type == nil
|
118
|
+
unless type.is_a?(String)
|
119
|
+
errors << Kwalify.validate_error(:type_notstr, rule, "#{path}/type", type.to_s)
|
120
|
+
end
|
121
|
+
klass = Kwalify.get_type_class(type)
|
122
|
+
unless klass
|
123
|
+
errors << Kwalify.validate_error(:type_unknown, rule, "#{path}/type", type.to_s)
|
124
|
+
end
|
125
|
+
#
|
126
|
+
if hash.key?('pattern')
|
127
|
+
val = hash['pattern']
|
128
|
+
pat = (val =~ /\A\/(.*)\/([mi]?[mi]?)\z/ ? $1 : val)
|
129
|
+
begin
|
130
|
+
Regexp.compile(pat)
|
131
|
+
rescue RegexpError => ex
|
132
|
+
errors << Kwalify.validate_error(:pattern_syntaxerr, rule, "#{path}/pattern", val)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
#
|
136
|
+
if hash.key?('enum')
|
137
|
+
if type == 'seq' || type == 'map' # unless Kwalify.scalar_class?(klass)
|
138
|
+
errors << schema_error(:enum_notscalar, rule, path, 'enum:')
|
139
|
+
else
|
140
|
+
elem_table = {}
|
141
|
+
hash['enum'].each do |elem|
|
142
|
+
if elem_table[elem]
|
143
|
+
errors << Kwalify.validate_error(:enum_duplicate, rule, "#{path}/enum", elem.to_s)
|
144
|
+
end
|
145
|
+
elem_table[elem] = true
|
146
|
+
unless elem.is_a?(klass)
|
147
|
+
errors << schema_error(:enum_type_unmatch, rule, "#{path}/enum", elem, [Kwalify.word(type)])
|
148
|
+
end
|
91
149
|
end
|
92
150
|
end
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
151
|
+
end
|
152
|
+
#
|
153
|
+
if hash.key?('assert')
|
154
|
+
val = hash['assert']
|
155
|
+
val =~ /\bval\b/ or errors << Kwalify.validate_error(:assert_noval, rule, "#{path}/assert", val)
|
156
|
+
begin
|
157
|
+
eval "proc { |val| #{val} }"
|
158
|
+
rescue SyntaxError => ex
|
159
|
+
errors << Kwalify.validate_error(:assert_syntaxerr, rule, "#{path}/assert", val)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
#
|
163
|
+
if hash.key?('range')
|
164
|
+
val = hash['range']
|
165
|
+
curr_path = path + "/range"
|
166
|
+
if ! val.is_a?(Hash)
|
167
|
+
errors << Kwalify.validate_error(:range_notmap, rule, curr_path, val)
|
168
|
+
elsif type == 'seq' || type == 'map' || type == 'bool' || type == 'any'
|
169
|
+
errors << Kwalify.validate_error(:range_notscalar, rule, path, 'range:')
|
170
|
+
else
|
171
|
+
val.each do |rkey, rval|
|
172
|
+
case rkey
|
173
|
+
when 'max', 'min'
|
174
|
+
unless rval.is_a?(klass)
|
175
|
+
typename = Kwalify.word(type) || type
|
176
|
+
errors << Kwalify.validate_error(:range_type_unmatch, rule, "#{curr_path}/#{rkey}", rval, [typename])
|
177
|
+
end
|
178
|
+
else
|
179
|
+
errors << Kwalify.validate_error(:range_undefined, rule, curr_path, "#{rkey}:")
|
180
|
+
end
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
#
|
185
|
+
if hash.key?('length')
|
186
|
+
val = hash['length']
|
187
|
+
curr_path = path + "/length"
|
188
|
+
val.is_a?(Hash) or errors << Kwalify.validate_error(:length_notmap, rule, curr_path, val)
|
189
|
+
unless type == 'str' || type == 'text'
|
190
|
+
errors << validate_error(:length_nottext, rule, path, 'length:')
|
191
|
+
end
|
192
|
+
val.each do |lkey, lval|
|
193
|
+
case lkey
|
194
|
+
when 'max', 'min'
|
195
|
+
unless lval.is_a?(Integer)
|
196
|
+
errors << Kwalify.validate_error(:length_notint, rule, "#{curr_path}/#{lkey}", lval)
|
197
|
+
end
|
99
198
|
else
|
100
|
-
|
199
|
+
errors << validate_error(:length_undefined, rule, curr_path, "#{lkey}:")
|
101
200
|
end
|
102
201
|
end
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
errors << Kwalify
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
errors << Kwalify
|
113
|
-
errors << Kwalify::Errors.validate_error(:map_has_sequence, schema, path, obj) if hash.key?('sequence')
|
114
|
-
else
|
115
|
-
errors << Kwalify::Errors.validate_error(:scalar_has_sequence, schema, path, obj) if hash.key?('sequence')
|
116
|
-
errors << Kwalify::Errors.validate_error(:scalar_has_mapping, schema, path, obj) if hash.key?('mapping')
|
202
|
+
end
|
203
|
+
#
|
204
|
+
if hash.key?('sequence')
|
205
|
+
val = hash['sequence']
|
206
|
+
if val != nil && !val.is_a?(Array)
|
207
|
+
errors << Kwalify.validate_error(:sequence_notseq, rule, "#{path}/sequence", val)
|
208
|
+
elsif val == nil || val.empty?
|
209
|
+
errors << Kwalify.validate_error(:sequence_noelem, rule, "#{path}/sequence", val)
|
210
|
+
elsif val.length > 1
|
211
|
+
errors << Kwalify.validate_error(:sequence_toomany, rule, "#{path}/sequence", val)
|
117
212
|
end
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
errors << Kwalify
|
213
|
+
end
|
214
|
+
#
|
215
|
+
if hash.key?('mapping')
|
216
|
+
val = hash['mapping']
|
217
|
+
if val != nil && !val.is_a?(Hash)
|
218
|
+
errors << Kwalify.validate_error(:mapping_notmap, rule, "#{path}/mapping", val)
|
219
|
+
elsif val == nil || val.empty?
|
220
|
+
errors << Kwalify.validate_error(:mapping_noelem, rule, "#{path}/mapping", val)
|
124
221
|
end
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
222
|
+
end
|
223
|
+
#
|
224
|
+
if type == 'seq'
|
225
|
+
errors << Kwalify.validate_error(:seq_nosequence, rule, path, nil) unless hash.key?('sequence')
|
226
|
+
#errors << Kwalify.validate_error(:seq_conflict, rule, path, 'enum:') if hash.key?('enum')
|
227
|
+
errors << Kwalify.validate_error(:seq_conflict, rule, path, 'pattern:') if hash.key?('pattern')
|
228
|
+
errors << Kwalify.validate_error(:seq_conflict, rule, path, 'mapping:') if hash.key?('mapping')
|
229
|
+
#errors << Kwalify.validate_error(:seq_conflict, rule, path, 'range:') if hash.key?('range')
|
230
|
+
errors << Kwalify.validate_error(:seq_conflict, rule, path, 'length:') if hash.key?('length')
|
231
|
+
elsif type == 'map'
|
232
|
+
errors << Kwalify.validate_error(:map_nomapping, rule, path, nil) unless hash.key?('mapping')
|
233
|
+
#errors << Kwalify.validate_error(:map_conflict, rule, path, 'enum:') if hash.key?('enum')
|
234
|
+
errors << Kwalify.validate_error(:map_conflict, rule, path, 'pattern:') if hash.key?('pattern')
|
235
|
+
errors << Kwalify.validate_error(:map_conflict, rule, path, 'sequence:') if hash.key?('sequence')
|
236
|
+
#errors << Kwalify.validate_error(:map_conflict, rule, path, 'range:') if hash.key?('range')
|
237
|
+
errors << Kwalify.validate_error(:map_conflict, rule, path, 'length:') if hash.key?('length')
|
238
|
+
else
|
239
|
+
errors << Kwalify.validate_error(:scalar_conflict, rule, path, 'sequence:') if hash.key?('sequence')
|
240
|
+
errors << Kwalify.validate_error(:scalar_conflict, rule, path, 'mapping:') if hash.key?('mapping')
|
241
|
+
if hash.key?('enum')
|
242
|
+
errors << Kwalify.validate_error(:enum_conflict, rule, path, 'range:') if hash.key?('range')
|
243
|
+
errors << Kwalify.validate_error(:enum_conflict, rule, path, 'length:') if hash.key?('length')
|
244
|
+
errors << Kwalify.validate_error(:enum_conflict, rule, path, 'pattern:') if hash.key?('pattern')
|
130
245
|
end
|
131
246
|
end
|
132
|
-
|
133
|
-
|
247
|
+
|
248
|
+
end # end of def validate_hook()
|
249
|
+
|
250
|
+
end # end of class MetaValidator
|
251
|
+
|
252
|
+
|
253
|
+
schema = YAML.load(META_SCHEMA)
|
254
|
+
META_VALIDATOR = MetaValidator.new(schema)
|
255
|
+
|
256
|
+
def self.meta_validator
|
257
|
+
return META_VALIDATOR
|
134
258
|
end
|
135
259
|
|
136
260
|
end
|
137
|
-
|