class Swalidate::Middleware
Public Class Methods
new(app, options = {})
click to toggle source
# File lib/swalidate/middleware.rb, line 3 def initialize(app, options = {}) @app = app @schema = Swalidate::Schema.new(options[:file_path]) # TODO: puts a log message when not found and dont try to validate @subdomain = options[:subdomain] @path_prefix = options[:path_prefix] end
Public Instance Methods
call(env)
click to toggle source
# File lib/swalidate/middleware.rb, line 12 def call(env) @request = ActionDispatch::Request.new(env) @endpoint = @schema.endpoint(@request.method, @request.path.gsub(@path_prefix.to_s, '').sub(/\.[^.]+\z/, '')) if should_validate? validator = Swalidate::Validator::Main.new(@endpoint, @request.params) validator.call return error_response(validator.errors) unless validator.valid? end @app.call(env) end
Private Instance Methods
error_response(errors)
click to toggle source
# File lib/swalidate/middleware.rb, line 27 def error_response(errors) [ 400, { 'Content-Type' => 'application/vnd.api+json' }, [{ errors: errors }.to_json] ] end
should_validate?()
click to toggle source
# File lib/swalidate/middleware.rb, line 35 def should_validate? valid_subdomain? && valid_path_prefix? && valid_path? end
valid_path?()
click to toggle source
# File lib/swalidate/middleware.rb, line 47 def valid_path? @endpoint.should_validate? end
valid_path_prefix?()
click to toggle source
# File lib/swalidate/middleware.rb, line 43 def valid_path_prefix? @request.path =~ Regexp.new(@path_prefix.to_s) end
valid_subdomain?()
click to toggle source
# File lib/swalidate/middleware.rb, line 39 def valid_subdomain? @subdomain.to_s == @request.subdomain.to_s end