Skip to content

Instantly share code, notes, and snippets.

@PJK
Created May 30, 2011 15:23
Show Gist options
  • Save PJK/999049 to your computer and use it in GitHub Desktop.
Save PJK/999049 to your computer and use it in GitHub Desktop.
Sinatra validation autowiring
module Helpers
# Only a few dragons here. This method validates body of the request
# against a schema. If no schema was passed to the method, it will
# try to find it automagically
def validate!(schema = nil)
schema ||= Kernel.const_get(
settings.validation[:module]
).const_get(
/^\/(.*)\//.match(request.path)[1].capitalize
).const_get(
request.env['REQUEST_METHOD'].downcase.capitalize
)
res = Kwalify::Validator.new(schema).validate(request.body)
res.map! { |error|
error.to_s
}
error(1012, nil, res) unless res == []
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment