4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/sinatra/schema/param_validation.rb', line 4
def validate_params!(params, properties)
required_properties = properties.map do |k, prop|
k unless prop.is_a?(Hash) || prop.optional
end.compact
missing = required_properties.map(&:to_s).sort - params.keys.map(&:to_s).sort
unless missing.empty?
raise BadParams.new("Missing expected params: #{missing.join(',')}")
end
= params.keys.map(&:to_s).sort - properties.keys.map(&:to_s).sort
unless .empty?
raise BadParams.new("Unexpected params: #{.join(',')}")
end
properties.each do |id, definition|
if definition.is_a?(Hash)
validate_params!(params[id], definition)
else
unless definition.valid?(params[id])
raise BadParams.new("Incorrect format for param '#{id}'. Please encode it as #{definition.type}")
end
end
end
end
|