Created
May 30, 2011 15:23
-
-
Save PJK/999049 to your computer and use it in GitHub Desktop.
Sinatra validation autowiring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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