Sha256: 642978b324f623e5d443b8dc77031b5309e8d52bccdd049356f3ff68ae015984
Contents?: true
Size: 1.49 KB
Versions: 2
Compression:
Stored size: 1.49 KB
Contents
require "sinatra/validation/version" require "dry-validation" module Sinatra module Validation class InvalidParameterError < StandardError attr_reader :result def initialize(result) @result = result end end class Result attr_reader :params, :messages def initialize(params) @params = params end def with_message(errors) @messages = errors.to_h.map { |key, message| "#{key.to_s} #{message.first}" } self end end module Helpers def validates(options = {}, &block) schema = Class.new(Dry::Validation::Contract, &block).new validation = schema.call(params) result = Result.new(params).with_message(validation.errors) if options[:silent] || settings.silent_validation return result end if validation.failure? raise InvalidParameterError.new(result) end rescue InvalidParameterError => exception if options[:raise] || settings.raise_sinatra_validation_exception raise exception end errors = exception.result.messages error = errors.first if content_type && content_type.match(mime_type(:json)) error = { errors: errors }.to_json end halt 400, error end end def self.registered(app) app.helpers Validation::Helpers app.set :silent_validation, false app.set :raise_sinatra_validation_exception, false end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
sinatra-validation-0.3.1 | lib/sinatra/validation.rb |
sinatra-validation-0.3.0 | lib/sinatra/validation.rb |