class Sinatra::Schema::Resource
Attributes
defs[RW]
description[RW]
id[RW]
links[RW]
path[RW]
properties[RW]
title[RW]
Public Class Methods
new(options)
click to toggle source
# File lib/sinatra/schema/resource.rb, line 6 def initialize(options) @id = options[:id] @path = (options[:path] || "").chomp("/") @links = [] @defs = {} @properties = {} end
Public Instance Methods
validate_properties!(received)
click to toggle source
# File lib/sinatra/schema/resource.rb, line 43 def validate_properties!(received) required_properties = properties.map do |k, prop| # ignore nested properties for now, we'll cover these next k unless prop.is_a?(Hash) || prop.optional end.compact missing = required_properties.map(&:to_s).sort - received.keys.map(&:to_s).sort unless missing.empty? raise BadResponse.new("Missing properties: #{missing}") end extra = received.keys.map(&:to_s).sort - properties.keys.map(&:to_s).sort unless extra.empty? raise BadResponse.new("Unexpected properties: #{extra}") end properties.each do |id, definition| unless definition.valid?(received[id.to_s]) raise BadResponse.new("Bad response property: #{id}") end end end
validate_response!(rel, raw)
click to toggle source
# File lib/sinatra/schema/resource.rb, line 22 def validate_response!(rel, raw) # only validate responses in tests return unless ENV["RACK_ENV"] == "test" res = MultiJson.decode(raw) if rel == :instances unless res.is_a?(Array) raise BadResponse.new("Response should return an array") end if sample = res.first validate_properties!(sample) end else unless res.is_a?(Hash) raise BadResponse.new("Response should return a hash") end validate_properties!(res) end end