Module: AdiwgJsonValidation

Defined in:
lib/adiwg/mdtranslator/readers/adiwgJson/adiwgJson_validator.rb

Class Method Summary collapse

Class Method Details

.validate(file) ⇒ Object



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)

  # set the anticipated format of the input file based on the reader specified
  $response[:readerFormat] = 'json'

  # validate the input file structure
  # test for valid json syntax by attempting to parse the file
  begin
    hashObj = JSON.parse(file)
    $response[:readerStructurePass] = true
  rescue JSON::JSONError => err
    $response[:readerStructurePass] = false
    $response[:readerStructureMessages] << err
    return
  end

  # find name and version of input json file
  if hashObj.has_key?('version')
    hVersion = hashObj['version']
  else
    $response[:readerStructurePass] = false
    $response[:readerStructureMessages] << 'input file is missing the version:{} block'
    return
  end

  # get the version name
  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

  # get version number
  if hVersion.has_key?('version')
    requestReaderVersion = hVersion['version']
    $response[:readerVersionFound] = requestReaderVersion
    # test if reader version is supported
    # remove minor release number
    # ...and look for a module folder name ending with the requested version
    # ...example: 'modules_1.2.0'
    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

  # validate json against the adiwg-json_schemas
  # only one schema version is supported at this time
  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