14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/sinatra/validation.rb', line 14
def validates(options = {}, &block)
schema = Dry::Validation.Schema(&block)
validation = schema.call(params)
if validation.success?
return Result.new(validation.output)
end
errors = validation.messages(full: true).values.flatten;
if options[:silent] || settings.silent_validation
return Result.new(validation.output, errors)
end
raise InvalidParameterError.new(params: validation.output, messages: errors)
rescue InvalidParameterError => exception
if options[:raise] || settings.raise_sinatra_validation_exception
raise exception
end
error = exception.to_s
if content_type && content_type.match(mime_type(:json))
error = { errors: errors }.to_json
end
halt 400, error
end
|