Sha256: 754d3d0ea63db25bc5a706be1b9378e919bc61f9955775b0daebb0133dd46892
Contents?: true
Size: 1.55 KB
Versions: 1
Compression:
Stored size: 1.55 KB
Contents
module ApiValidator class Json < Base def validate(response) response_body = response.body.respond_to?(:to_hash) ? response.body.to_hash : response.body _failed_assertions = failed_assertions(response_body) super.merge( :key => :response_body, :failed_assertions => _failed_assertions.map(&:to_hash), :diff => diff(response_body, _failed_assertions), :valid => _failed_assertions.empty? ) end private def initialize_assertions(expected, path = "") case expected when Hash expected.each_pair do |key, val| item_path = [path, key].join("/") initialize_assertions(val, item_path) end when Array expected.each_with_index do |val, index| item_path = [path, index].join("/") initialize_assertions(val, item_path) end else assertions << Assertion.new(path, expected) end end def failed_assertions(actual) assertions.select do |assertion| pointer = JsonPointer.new(actual, assertion.path) !pointer.exists? || !assertion_valid?(assertion, pointer.value) end end def diff(actual, _failed_assertions) _failed_assertions.map do |assertion| pointer = JsonPointer.new(actual, assertion.path) assertion = assertion.to_hash if pointer.exists? assertion[:op] = "replace" assertion[:current_value] = pointer.value else assertion[:op] = "add" end assertion end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
api-validator-0.0.1 | lib/api-validator/json.rb |