Class: URBANopt::Reporting::DefaultReports::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/urbanopt/reporting/default_reports/validator.rb

Constant Summary collapse

@@schema =
nil

Instance Method Summary collapse

Constructor Details

#initializeValidator

Initialize the root directory



15
16
17
18
19
20
21
22
# File 'lib/urbanopt/reporting/default_reports/validator.rb', line 15

def initialize
  super

  @root_dir = File.absolute_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..'))

  @instance_lock = Mutex.new
  @@schema ||= schema
end

Instance Method Details

#csv_headersObject

get csv headers from csv schema



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/urbanopt/reporting/default_reports/validator.rb', line 48

def csv_headers
  # read scenario csv schema headers
  scenario_csv_schema = open(File.expand_path('schema/scenario_csv_columns.txt', File.dirname(__FILE__))) # .read()

  scenario_csv_schema_headers = []
  File.readlines(scenario_csv_schema).each do |line|
    l = line.delete("\n")
    a = l.delete("\t")
    scenario_csv_schema_headers << a
  end

  return scenario_csv_schema_headers
end

#files_dirObject

Return the absolute path of the default reports files



25
26
27
# File 'lib/urbanopt/reporting/default_reports/validator.rb', line 25

def files_dir
  File.absolute_path(File.join(@root_dir, 'lib/urbanopt/reporting/default_reports/'))
end

#schemaObject

return schema



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/urbanopt/reporting/default_reports/validator.rb', line 35

def schema
  @instance_lock.synchronize do
    if @@schema.nil?
      File.open(schema_file, 'r') do |f|
        @@schema = JSON.parse(f.read, symbolize_names: true)
      end
    end
  end

  @@schema
end

#schema_fileObject

return path to schema file



30
31
32
# File 'lib/urbanopt/reporting/default_reports/validator.rb', line 30

def schema_file
  File.join(files_dir, 'schema/scenario_schema.json')
end

#schema_valid?Boolean

check if the schema is valid

Returns:

  • (Boolean)


74
75
76
77
# File 'lib/urbanopt/reporting/default_reports/validator.rb', line 74

def schema_valid?
  metaschema = JSON::Validator.validator_for_name('draft6').metaschema
  JSON::Validator.validate(metaschema, @@schema)
end

#schema_validation_errorsObject

return detailed schema validation errors



80
81
82
83
# File 'lib/urbanopt/reporting/default_reports/validator.rb', line 80

def schema_validation_errors
  metaschema = JSON::Validator.validator_for_name('draft6').metaschema
  JSON::Validator.fully_validate(metaschema, @@schema)
end

#validate(schema, data) ⇒ Object

validate data against schema

parameters:

schema - Hash - A hash of the JSON scenario_schema. data - Hash - A hash of the data to be validated against scenario_schema.



69
70
71
# File 'lib/urbanopt/reporting/default_reports/validator.rb', line 69

def validate(schema, data)
  JSON::Validator.fully_validate(schema, data)
end