sholladay / pogo

Server framework for Deno
Mozilla Public License 2.0
482 stars 32 forks source link

What do you use for validation? #67

Closed KaKi87 closed 2 years ago

sholladay commented 2 years ago

I use joi.

import joi from 'https://jspm.dev/joi';

// ...

const headerSchema =  joi.object().required().keys({
    'content-type' : joi.string().required().valid('application/json')
});
server.route('/', () => {
    joi.attempt(Object.fromEntries(request.headers), headerSchema);
    return 'Headers are valid';
});

At the moment, Pogo has no special handling of joi schemas or error objects, so it's up to you to use joi in the route handler, as shown above. In the future, I intend to implement a system like hapi has, where you can provide a schema for the request and response as route options and Pogo will handle the validation automatically to simplify your code. PRs welcome if you want that implemented quickly.

KaKi87 commented 2 years ago

I'm using Joi from Node, but I didn't realized it was possible to use it out-of-the-box from Deno, so I went to the trouble of looking for an alternative on deno.land, without much success.

But, yes, I'm actually satisfied with using Joi on Deno.

I intend to implement a system like hapi has, where you can provide a schema for the request and response as route options and Pogo will handle the validation automatically to simplify your code

Well, I created my own createRoute function at @cv.vg/api/lib/route.js which handles that, as well as CORS.