Skip to content

Commit

Permalink
specify path
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Sep 18, 2019
1 parent d098141 commit 112b5e1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/middlewares/openapi.response.validator.ts
Expand Up @@ -2,7 +2,11 @@ import ono from 'ono';
import * as Ajv from 'ajv';
import * as mung from 'express-mung';
import { createResponseAjv } from './ajv';
import { extractContentType, ajvErrorsToValidatorError } from './util';
import {
extractContentType,
ajvErrorsToValidatorError,
validationError,
} from './util';

const TYPE_JSON = 'application/json';

Expand All @@ -26,7 +30,8 @@ export class ResponseValidator {
const responses = req.openapi.schema && req.openapi.schema.responses;
const validators = this._getOrBuildValidator(req, responses);
const statusCode = res.statusCode;
return this._validate({ validators, body, statusCode });
const path = req.path;
return this._validate({ validators, body, statusCode, path });
}
return body;
});
Expand All @@ -49,7 +54,7 @@ export class ResponseValidator {
return validators;
}

_validate({ validators, body, statusCode }) {
_validate({ validators, body, statusCode, path }) {
// find the validator for the 'status code' e.g 200, 2XX or 'default'
let validator;
const status = statusCode;
Expand All @@ -59,8 +64,11 @@ export class ResponseValidator {
else if (statusXX in validators) validator = validators[statusXX];
else if (validators.default) validator = validator.default;
else {
// TODO
throw new Error('unknown status code - TODO fix me ');
throw validationError(
500,
path,
`no schema defined for status code '${status}' in the openapi spec`,
);
}
}

Expand Down
8 changes: 7 additions & 1 deletion test/response.validator.spec.ts
Expand Up @@ -23,6 +23,7 @@ describe(packageJson.name, () => {
validators,
body: { message: 'some error message', code: 400 },
statusCode: 400,
path: '/some-path',
}),
).to.not.exist;
} catch (e) {
Expand All @@ -43,6 +44,7 @@ describe(packageJson.name, () => {
validators,
body: { message, code },
statusCode: code,
path: '/some-path',
}),
).to.not.exist;
} catch (e) {
Expand All @@ -62,6 +64,7 @@ describe(packageJson.name, () => {
validators,
body: [{ id: 'bad-id', name: 'test', tag: 'tag' }],
statusCode: 200,
path: '/some-path',
});
} catch (e) {
expect(e).to.be.not.null;
Expand All @@ -74,6 +77,7 @@ describe(packageJson.name, () => {
validators,
body: { id: 1, name: 'test', tag: 'tag' },
statusCode: 200,
path: '/some-path',
});
} catch (e) {
expect(e).to.be.not.null;
Expand All @@ -85,6 +89,7 @@ describe(packageJson.name, () => {
validators,
body: [{ id: 1, name: [], tag: 'tag' }],
statusCode: 200,
path: '/some-path',
});
} catch (e) {
expect(e).to.be.not.null;
Expand All @@ -99,6 +104,7 @@ describe(packageJson.name, () => {
const responses = petsResponseSchema();
const validators = v._getOrBuildValidator(null, responses);
const statusCode = 200;
const path = '/some-path';
const body = [
{
id: 10,
Expand All @@ -108,7 +114,7 @@ describe(packageJson.name, () => {
},
];
try {
expect(v._validate({ validators, body, statusCode })).to.not.exist;
expect(v._validate({ validators, body, statusCode, path })).to.not.exist;
expect('here').to.be.null;
} catch (e) {
// TODO include params.additionalProperty: value in error message
Expand Down

0 comments on commit 112b5e1

Please sign in to comment.