swifter icon indicating copy to clipboard operation
swifter copied to clipboard

is there a way to define routes using codables?

Open avnerbarr opened this issue 5 years ago • 0 comments

I'd like to have a route post body associated to a type. Not clear if I need to explicitly handle the body deserialize (i.e. using codable) and error handle myself or if I can define a route with that information included and the framework will reject invalid requests.

i.e

// define my type
struct User: Codable {
   let name
   let age 
   ...
}
// define my route which should receive this type
server.POST['user'] =  { requst in // request body should be a typed "User" type

or do I need to handle the serde dance in every route explicitly (leading to a lot of boiler plate):

i.e

// explicit handling with errors
{ request in 
let decodedUser = .... decode request.body
if decodedUser is invalid return .error(....)
...
// handle valid user case

thanks

avnerbarr avatar Jul 16 '20 11:07 avnerbarr