require "json" require "json-schema" require "open-uri" require "yaml" $:.unshift File.dirname(__FILE__) require "json_schema_spec/tasks" require "json_schema_spec/util" module JsonSchemaSpec class < 'application/json') params = Rails.application.routes.recognize_path(path) controller = "#{params[:controller]}_controller".camelize.constantize endpoint = controller.action(params[:action]) JSON.parse(endpoint.call(env)[2].body) else path = "#{File.expand_path(".")}/spec/fixtures/schema.yml" YAML.load(File.read(path)) end end def return_nil?(key, value, required) !required.include?(key) && ( !value.is_a?(Hash) || value[:optional] || value[:type] == 'null' ) end def validate_json_schema(resource, method, params) return if RUBY_VERSION =~ /^1\.8\./ schema = json_schema(resource, method) [ :request, :response ].each do |direction| validates = JSON::Validator.fully_validate( Util.stringify_keys(schema[direction]), Util.stringify_keys(params[direction]), :validate_schema => true ) expect(validates).to eq([]) end end end