lib/har/archive.rb in har-0.0.8 vs lib/har/archive.rb in har-0.0.9
- old
+ new
@@ -32,10 +32,27 @@
end
result
end
+ # @api private
+ def self.schemas
+ @schemas ||= {}
+ end
+
+ # @api private
+ def self.add_schema(path)
+ data = JSON.parse(File.read(path))
+ id = data.fetch('id')
+
+ schemas[id] = data
+ end
+
+ Dir[File.expand_path("../schemas/*.json", __FILE__)].each do |path|
+ add_schema path
+ end
+
attr_reader :uri
def initialize(input, uri = nil)
@data = input
@uri = uri
@@ -79,30 +96,27 @@
def save_to(path)
File.open(path, "w") { |io| io << @data.to_json }
end
def valid?
- JSON::Validator.validate schema_file, @data
+ Jschematic.validate @data, log_type_schema, :debug => true, :context => self.class.schemas.values
end
def validate!
- JSON::Validator.validate2 schema_file, @data
- rescue JSON::ValidationError => ex
- # add archive uri to the message
- if @uri
- raise ValidationError, "#{@uri}: #{ex.message}"
- else
- raise ValidationError, ex.message
- end
+ Jschematic.validate! @data, log_type_schema, :debug => true, :context => self.class.schemas.values
+ nil
+ rescue Jschematic::ValidationError => ex
+ msg = ex.message
+ msg = "#{@uri}: #{msg}" if @uri
+
+ raise ValidationError, msg
end
- protected
-
private
- def schema_file
- @schema_file ||= File.expand_path("../schemas/logType", __FILE__)
+ def log_type_schema
+ @schema ||= self.class.schemas.fetch('logType')
end
def merge_data(left, right, uri)
log = left.fetch('log')
other_log = right.fetch('log')
@@ -158,6 +172,5 @@
Marshal.load Marshal.dump(obj)
end
end # Archive
end # HAR
-