Skip to content

Commit fe4fe16

Browse files
committed
fix(rest): make sure validation system error is reported
1 parent 8fcc938 commit fe4fe16

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

packages/rest/src/parser.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ function getContentType(req: Request): string | undefined {
5656
* controller method
5757
*
5858
* @param request Incoming HTTP request
59-
* @param operationSpec Swagger spec defined in the controller
60-
* @param pathParams Path parameters in incoming HTTP request
59+
* @param route Resolved Route
6160
*/
6261
export async function parseOperationArgs(
6362
request: Request,

packages/rest/src/validation/request-body.validator.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,17 @@ function validateValueAgainstJsonSchema(
9393
const ajv = new AJV({
9494
allErrors: true,
9595
});
96+
9697
try {
9798
if (ajv.validate(schemaWithRef, body)) {
9899
debug('Request body passed AJV validation.');
99100
return;
100101
}
101102
} catch (err) {
102-
debug('Cannot execute AJV validation: %s', util.inspect(err));
103+
debug('Fails to execute AJV validation:', err);
104+
// TODO: [rfeng] Do we want to introduce a flag to disable validation
105+
// or sink validation errors?
106+
throw err;
103107
}
104108

105109
debug('Invalid request body: %s', util.inspect(ajv.errors));

packages/rest/test/unit/request-body.validator.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ const ADDRESS_SCHEMA = {
4343
},
4444
};
4545

46+
const INVALID_ACCOUNT_SCHEMA = {
47+
title: 'Account',
48+
properties: {
49+
title: {type: 'string'},
50+
address: {$ref: '#/components/schemas/Invalid'},
51+
},
52+
};
53+
4654
describe('validateRequestBody', () => {
4755
it('accepts valid data omitting optional properties', () => {
4856
validateRequestBody({title: 'work'}, aBodySpec(TODO_SCHEMA));
@@ -113,6 +121,14 @@ describe('validateRequestBody', () => {
113121
);
114122
});
115123

124+
it('reports schema generation errors', () => {
125+
expect(() =>
126+
validateRequestBody({}, aBodySpec(INVALID_ACCOUNT_SCHEMA)),
127+
).to.throw(
128+
"can't resolve reference #/components/schemas/Invalid from id #",
129+
);
130+
});
131+
116132
it('resolves schema references', () => {
117133
const details: RestHttpErrors.ValidationErrorDetails[] = [
118134
{

0 commit comments

Comments
 (0)