From f8c592168e75ed3a72b199a2bcf286070e82b60c Mon Sep 17 00:00:00 2001 From: Michael Yali Date: Sat, 28 Dec 2019 15:42:56 +0200 Subject: [PATCH] fix(request): request validation error code 400 fix #374, fix #247 --- .../interceptors/crud-request.interceptor.ts | 51 ++++++++++++------- .../crud/test/crud.decorator.base.spec.ts | 12 ++--- .../crud/test/crud.decorator.override.spec.ts | 4 +- packages/crud/test/crud.dto.options.spec.ts | 4 +- 4 files changed, 42 insertions(+), 29 deletions(-) diff --git a/packages/crud/src/interceptors/crud-request.interceptor.ts b/packages/crud/src/interceptors/crud-request.interceptor.ts index 7402030d..902c95d0 100644 --- a/packages/crud/src/interceptors/crud-request.interceptor.ts +++ b/packages/crud/src/interceptors/crud-request.interceptor.ts @@ -1,10 +1,16 @@ import { + BadRequestException, CallHandler, ExecutionContext, Injectable, NestInterceptor, } from '@nestjs/common'; -import { RequestQueryParser, SCondition, QueryFilter } from '@nestjsx/crud-request'; +import { + RequestQueryException, + RequestQueryParser, + SCondition, + QueryFilter, +} from '@nestjsx/crud-request'; import { isNil, isFunction, isArrayFull, hasLength } from '@nestjsx/util'; import { PARSED_CRUD_REQUEST_KEY } from '../constants'; @@ -19,27 +25,34 @@ export class CrudRequestInterceptor extends CrudBaseInterceptor intercept(context: ExecutionContext, next: CallHandler) { const req = context.switchToHttp().getRequest(); - /* istanbul ignore else */ - if (!req[PARSED_CRUD_REQUEST_KEY]) { - const { ctrlOptions, crudOptions, action } = this.getCrudInfo(context); - const parser = RequestQueryParser.create(); - - parser.parseQuery(req.query); - - if (!isNil(ctrlOptions)) { - const search = this.getSearch(parser, crudOptions, action, req.params); - const auth = this.getAuth(parser, crudOptions, req); - parser.search = auth.or - ? { $or: [auth.or, { $and: search }] } - : { $and: [auth.filter, ...search] }; - } else { - parser.search = { $and: this.getSearch(parser, crudOptions, action) }; + try { + /* istanbul ignore else */ + if (!req[PARSED_CRUD_REQUEST_KEY]) { + const { ctrlOptions, crudOptions, action } = this.getCrudInfo(context); + const parser = RequestQueryParser.create(); + + parser.parseQuery(req.query); + + if (!isNil(ctrlOptions)) { + const search = this.getSearch(parser, crudOptions, action, req.params); + const auth = this.getAuth(parser, crudOptions, req); + parser.search = auth.or + ? { $or: [auth.or, { $and: search }] } + : { $and: [auth.filter, ...search] }; + } else { + parser.search = { $and: this.getSearch(parser, crudOptions, action) }; + } + + req[PARSED_CRUD_REQUEST_KEY] = this.getCrudRequest(parser, crudOptions); } - req[PARSED_CRUD_REQUEST_KEY] = this.getCrudRequest(parser, crudOptions); + return next.handle(); + } catch (error) { + /* istanbul ignore next */ + throw error instanceof RequestQueryException + ? new BadRequestException(error.message) + : error; } - - return next.handle(); } getCrudRequest( diff --git a/packages/crud/test/crud.decorator.base.spec.ts b/packages/crud/test/crud.decorator.base.spec.ts index 827afec6..cfd4ffac 100644 --- a/packages/crud/test/crud.decorator.base.spec.ts +++ b/packages/crud/test/crud.decorator.base.spec.ts @@ -55,9 +55,9 @@ describe('#crud', () => { return request(server) .get('/test') .query(query) - .expect(500) .end((_, res) => { const expected = { statusCode: 400, message: 'Invalid filter value' }; + expect(res.status).toEqual(400); expect(res.body).toMatchObject(expected); done(); }); @@ -73,12 +73,12 @@ describe('#crud', () => { it('should return status 400', (done) => { return request(server) .get('/test/invalid') - .expect(400) .end((_, res) => { const expected = { statusCode: 400, message: 'Invalid param id. Number expected', }; + expect(res.status).toEqual(400); expect(res.body).toMatchObject(expected); done(); }); @@ -107,8 +107,8 @@ describe('#crud', () => { return request(server) .post('/test') .send(send) - .expect(400) .end((_, res) => { + expect(res.status).toEqual(400); expect(res.body.message[0].property).toBe('age'); done(); }); @@ -145,8 +145,8 @@ describe('#crud', () => { return request(server) .post('/test/bulk') .send(send) - .expect(400) .end((_, res) => { + expect(res.status).toEqual(400); expect(res.body.message[0].property).toBe('bulk'); done(); }); @@ -176,8 +176,8 @@ describe('#crud', () => { return request(server) .put('/test/1') .send(send) - .expect(400) .end((_, res) => { + expect(res.status).toEqual(400); expect(res.body.message[0].property).toBe('id'); done(); }); @@ -207,8 +207,8 @@ describe('#crud', () => { return request(server) .patch('/test/1') .send(send) - .expect(400) .end((_, res) => { + expect(res.status).toEqual(400); expect(res.body.message[0].property).toBe('id'); done(); }); diff --git a/packages/crud/test/crud.decorator.override.spec.ts b/packages/crud/test/crud.decorator.override.spec.ts index d83dac5d..272c090c 100644 --- a/packages/crud/test/crud.decorator.override.spec.ts +++ b/packages/crud/test/crud.decorator.override.spec.ts @@ -79,9 +79,9 @@ describe('#crud', () => { return request(server) .get('/test') .query(query) - .expect(500) .end((_, res) => { const expected = { statusCode: 400, message: 'Invalid filter value' }; + expect(res.status).toEqual(400); expect(res.body).toMatchObject(expected); done(); }); @@ -124,8 +124,8 @@ describe('#crud', () => { return request(server) .post('/test/bulk') .send(send) - .expect(400) .end((_, res) => { + expect(res.status).toEqual(400); expect(res.body.message[0].property).toBe('bulk'); done(); }); diff --git a/packages/crud/test/crud.dto.options.spec.ts b/packages/crud/test/crud.dto.options.spec.ts index 2f15b08b..a820bc11 100644 --- a/packages/crud/test/crud.dto.options.spec.ts +++ b/packages/crud/test/crud.dto.options.spec.ts @@ -66,8 +66,8 @@ describe('#crud', () => { return request(server) .post('/test') .send(send) - .expect(400) .end((_, res) => { + expect(res.status).toEqual(400); expect(res.body.message[0].property).toBe('age'); done(); }); @@ -97,8 +97,8 @@ describe('#crud', () => { return request(server) .patch('/test/1') .send(send) - .expect(400) .end((_, res) => { + expect(res.status).toEqual(400); expect(res.body.message[0].property).toBe('email'); done(); });