sholladay / pogo

Server framework for Deno

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

What do you use for validation?

KaKi87 opened this issue · comments

What do you use for validation?

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.

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.