Class: URBANopt::Scenario::DefaultReports::Validator
- Inherits:
-
Object
- Object
- URBANopt::Scenario::DefaultReports::Validator
- Defined in:
- lib/urbanopt/scenario/default_reports/validator.rb
Constant Summary collapse
- @@schema =
nil
Instance Method Summary collapse
-
#files_dir ⇒ Object
Return the absolute path of the default reports files.
-
#initialize ⇒ Validator
constructor
Initialize the root directory.
-
#schema ⇒ Object
return schema.
-
#schema_file ⇒ Object
return path to schema file.
-
#schema_valid? ⇒ Boolean
check if the schema is valid.
-
#schema_validation_errors ⇒ Object
return detailed schema validation errors.
-
#validate(schema, data) ⇒ Object
validate data against schema.
Constructor Details
#initialize ⇒ Validator
Initialize the root directory
40 41 42 43 44 45 46 47 |
# File 'lib/urbanopt/scenario/default_reports/validator.rb', line 40 def initialize super @root_dir = File.absolute_path(File.join(File.dirname(__FILE__), '..', '..', '..', '..')) @instance_lock = Mutex.new @@schema ||= schema end |
Instance Method Details
#files_dir ⇒ Object
Return the absolute path of the default reports files
50 51 52 |
# File 'lib/urbanopt/scenario/default_reports/validator.rb', line 50 def files_dir File.absolute_path(File.join(@root_dir, 'lib/urbanopt/scenario/default_reports/')) end |
#schema ⇒ Object
return schema
60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/urbanopt/scenario/default_reports/validator.rb', line 60 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_file ⇒ Object
return path to schema file
55 56 57 |
# File 'lib/urbanopt/scenario/default_reports/validator.rb', line 55 def schema_file File.join(files_dir, 'schema/scenario_schema.json') end |
#schema_valid? ⇒ Boolean
check if the schema is valid
84 85 86 87 |
# File 'lib/urbanopt/scenario/default_reports/validator.rb', line 84 def schema_valid? = JSON::Validator.validator_for_name('draft6'). JSON::Validator.validate(, @@schema) end |
#schema_validation_errors ⇒ Object
return detailed schema validation errors
90 91 92 93 |
# File 'lib/urbanopt/scenario/default_reports/validator.rb', line 90 def schema_validation_errors = JSON::Validator.validator_for_name('draft6'). JSON::Validator.fully_validate(, @@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.
79 80 81 |
# File 'lib/urbanopt/scenario/default_reports/validator.rb', line 79 def validate(schema, data) JSON::Validator.fully_validate(schema, data) end |