Simple, flexible and typesafe validation helpers
yarn add @nexys/validation
import Validation, {Type, Utils} from '@nexys/validation';
see tests
import Router from 'koa-router';
import bodyParser from 'koa-body';
import Validation, { Utils as VU } from '@nexys/validation';
const router = new Router();
router.post(
'/update',
bodyParser(),
Validation.isShapeMiddleware({
uuid: { extraCheck: VU.checkUuid },
name: {}
}),
async ctx => {
// now that the body has been validated this can be safely typed/cast to the expected type.
// Note that the type should match the validation shape
const { uuid, name }: { uuid: Uuid; name: string } = ctx.request.body;
ctx.body = await myFunc(uuid, name);
}
);
export default router.routes();