Class: JsonSchemaValidator::JSchema
- Defined in:
- lib/json_schema_validator/j_schema.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#initialize(files_to_validate) ⇒ JSchema
constructor
A new instance of JSchema.
- #use_for_validation(schema) ⇒ Object
- #validate(schema) ⇒ Object
Methods inherited from Base
#errors, #log, #log_fmt, run, #success, #validate_all, #validate_data, #validate_schemas
Constructor Details
#initialize(files_to_validate) ⇒ JSchema
Returns a new instance of JSchema.
5 6 7 8 9 10 |
# File 'lib/json_schema_validator/j_schema.rb', line 5 def initialize(files_to_validate) @files_to_validate = files_to_validate schema_data = File.read("#{File.dirname(__FILE__)}/schema.json") schema_json = JSON.parse(schema_data) @meta_schema = ::JSchema.build(schema_json) end |
Instance Method Details
#use_for_validation(schema) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/json_schema_validator/j_schema.rb', line 22 def use_for_validation(schema) schema_to_test = JSON.parse(schema) begin ::JSchema.build(schema_to_test) rescue errors << "Data validation failed: #{schema_to_test['title']}\n#{$!}" # puts $!.backtrace else success << schema_to_test['title'] || schema_to_test.keys[0] end end |
#validate(schema) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/json_schema_validator/j_schema.rb', line 12 def validate(schema) schema_to_test = JSON.parse(schema) result = @meta_schema.validate schema_to_test if !result || result.length == 0 success << schema_to_test['title'] || schema_to_test.keys[0] else errors << "Schema validation failed: #{schema_to_test['title']}\n#{result.join("\n")}" end end |