9
10
11
12
13
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
41
|
# File 'lib/sinatra/validations.rb', line 9
def validate(parameters, key, options = {})
default = options[:default]
type = options[:type]
cast = true
if options.key?(:cast)
cast = options[:cast]
end
param = parameters[key]
if !param
if options.key?(:default)
return param = parameters[key] = default
end
raise InvalidParameter.new("Missing required parameter #{key}")
end
if cast
if type == Integer
param = parameters[key] = cast_into_integer(param, key)
elsif type == String
param = parameters[key] = cast_into_string(param, key)
else
raise InvalidParameter.new("Unsupported casting for #{key}")
end
end
if type && !param.is_a?(type)
raise InvalidParameter.new("Wrong type for #{key}. Expected #{type}, got #{param.class}")
end
end
|