Skip to content

Commit

Permalink
:sparkels:(return body): async validate, return validated body
Browse files Browse the repository at this point in the history
  • Loading branch information
whatwewant committed Dec 16, 2018
1 parent 360bb33 commit 8c0e405
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@koex/validate",
"version": "0.0.1",
"version": "0.0.2",
"description": "validate for koa extend",
"main": "lib/index.js",
"module": "lib/index.js",
Expand All @@ -15,11 +15,11 @@
"prepublish": "npm run build"
},
"devDependencies": {
"@koex/router": "^0.0.2",
"@types/koa": "^2.0.47",
"@types/mocha": "^5.2.5",
"@types/node": "^10.9.4",
"@zcorky/koa-onerror": "^0.0.1",
"@koex/router": "^0.0.1",
"coveralls": "^3.0.2",
"husky": "^1.2.0",
"koa": "^2.6.2",
Expand Down
28 changes: 18 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ declare module 'koa' {
*
* validate(rules, data);
*/
validate: <R, D>(rules: R, data?: D) => void;
validate: <R, D>(rules: R, data?: D) => Promise<D>;
}
}

Expand Down Expand Up @@ -57,16 +57,24 @@ export interface Options {
export default (options?: Options): Middleware => {
const validator = new Parameter(options);

return async function koaEjs(ctx: Context, next: () => Promise<void>) {
if (!ctx.validate) {
ctx.validate = (rules, data) => {
const _data = data || (ctx.request as any).body;
const errors = validator.validate(rules, _data);
const validateFn = async <R, D>(ctx: Context, rules: R, data?: D) => {
const _data: D = data || (ctx.request as any).body;
const errors = validator.validate(rules, _data);

if (errors) {
ctx.throw(422, 'Validation Failed', {
code: 'invalid_param',
errors,
});
}

ctx.throw(422, 'Validation Failed', {
code: 'invalid_param',
errors,
});
return _data;
};

return async function validate(ctx: Context, next: () => Promise<void>) {
if (!ctx.validate) {
ctx.validate = async <R, D>(rules: R, data?: D) => {
return validateFn(ctx, rules, data);
};
}

Expand Down
35 changes: 32 additions & 3 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('koa validate', () => {
app.use(validate());

app.use(router.post('/', async (ctx, next) => {
ctx.validate({
await ctx.validate({
name: 'string',
age: 'int',
});
Expand All @@ -31,7 +31,7 @@ describe('koa validate', () => {
return request(app.listen())
.post('/')
.send({ name: 'name', age: 22 })
.expect(422);
.expect(200);
});

it('should 422', () => {
Expand All @@ -44,7 +44,7 @@ describe('koa validate', () => {
app.use(validate());

app.use(router.post('/', async (ctx, next) => {
ctx.validate({
await ctx.validate({
name: 'string',
age: 'int',
});
Expand All @@ -59,4 +59,33 @@ describe('koa validate', () => {
.send({ name: 'name', age: '22' })
.expect(422, { message: 'Validation Failed' });
});

it('return body', () => {
const app = new Koa();
app.use(onerror({
log: () => null,
}));
app.use(bodyParser());
app.use(validate());

app.use(router.post('/', async (ctx) => {
const { name, age } = await ctx.validate<any, {
name: string;
age: number;
}>({
name: 'string',
age: 'int',
});

ctx.body = {
name,
age,
};
}));

return request(app.listen())
.post('/')
.send({ name: 'name', age: 22 })
.expect(200, { name: 'name', age: 22 });
});
});

0 comments on commit 8c0e405

Please sign in to comment.