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
|
# File 'lib/adiwg/mdtranslator/readers/adiwgJson/adiwgJson_validator.rb', line 17
def self.validate(file)
$response[:readerFormat] = 'json'
begin
hashObj = JSON.parse(file)
$response[:readerStructurePass] = true
rescue JSON::JSONError => err
$response[:readerStructurePass] = false
$response[:readerStructureMessages] << err
return
end
if hashObj.has_key?('version')
hVersion = hashObj['version']
else
$response[:readerStructurePass] = false
$response[:readerStructureMessages] << 'input file is missing the version:{} block'
return
end
if hVersion.has_key?('name')
s = hVersion['name']
if !s.nil?
$response[:readerNameFound] = s
if s != 'adiwgJson'
$response[:readerStructurePass] = false
$response[:readerStructureMessages] << "input file version name must be 'adiwgJson'"
$response[:readerStructureMessages] << "found version name '#{s}'"
return
end
end
else
$response[:readerStructurePass] = false
$response[:readerStructureMessages] << "input file version:{} block is missing the 'name' attribute"
return
end
if hVersion.has_key?('version')
requestReaderVersion = hVersion['version']
$response[:readerVersionFound] = requestReaderVersion
if !requestReaderVersion.nil?
aVersionParts = requestReaderVersion.split('.')
readerVersion = aVersionParts[0] +'.' + aVersionParts[1] + '.0'
dir = File.join(File.dirname(__FILE__),'modules_' + readerVersion)
if !File.directory?(dir)
$response[:readerStructurePass] = false
$response[:readerStructureMessages] << 'input file version is not supported'
$response[:readerStructureMessages] << "adiwgJson version requested was '#{requestReaderVersion}'"
return
end
$response[:readerVersionUsed] = readerVersion
end
else
$response[:readerStructurePass] = false
$response[:readerStructureMessages] << "input file version:{} block is missing the 'version' attribute"
return
end
begin
schema = ADIWG::JsonSchemas::Utils.schema_path
aValErrs = Array.new
if $response[:readerValidationLevel] == 'strict'
aValErrs = JSON::Validator.fully_validate(schema, file, :strict => true, :errors_as_objects => true)
elsif $response[:readerValidationLevel] == 'normal'
aValErrs = JSON::Validator.fully_validate(schema, file, :errors_as_objects => true)
end
if aValErrs.length > 0
$response[:readerValidationPass] = false
$response[:readerValidationMessages] = aValErrs
return
end
rescue JSON::Schema::ValidationError
$response[:readerValidationPass] = false
$response[:readerValidationMessages] << $!.message
return
end
$response[:readerValidationPass] = true
return
end
|