Skip to content

Commit

Permalink
fix type coercion test
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Jul 19, 2019
1 parent e934e37 commit dc41ae4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
39 changes: 18 additions & 21 deletions src/middlewares/openapi.request.validator.2.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Ajv from 'ajv';

import { validationError } from '../errors';
import ono from 'ono';

Expand Down Expand Up @@ -101,34 +102,20 @@ export class RequestValidator {
);
}

// if (this._apiDocs.components.parameters) {
// Object.entries(this._apiDocs.components.requestBodies).forEach(
// ([id, schema]: any[]) => {
// ajv.addSchema(schema, `#/components/schemas/${id}`);
// },
// );
// }

// if (this._apiDocs.components.responses) {
// Object.entries(this._apiDocs.components[type]).forEach(([id, schema]) => {
// ajv.addSchema(schema, `#/components/${type}/${id}`);
// });
// }

return ajv;
}

validate(req, res, next) {
const cacheKey = `${req.method}-${req.path}`;
const key = `${req.method}-${req.path}`;

if (!this._middlewareCache[cacheKey]) {
this._middlewareCache[cacheKey] = this.buildMiddleware(req, res, next);
if (!this._middlewareCache[key]) {
this._middlewareCache[key] = this.buildMiddleware(req, res, next);
}

return this._middlewareCache[cacheKey](req, res, next);
return this._middlewareCache[key](req, res, next);
}

buildMiddleware(req, res, next) {
private buildMiddleware(req, res, next) {
// const method = req.method.toLowerCase();
// const path = req.route.path.replace(/:(\w+)/gi, '{$1}');
// const pathSchema = this._apiDocs.paths[path][method];
Expand All @@ -154,6 +141,13 @@ export class RequestValidator {
throw ono(err, message);
}

const shouldUpdatePathParams =
Object.keys(req.openapi.pathParams).length > 0;

if (shouldUpdatePathParams) {
req.params = req.openapi.pathParams || req.params;
}

const parameters = this.parametersToSchema(path, pathSchema.parameters);
let body = pathSchema.requestBody || {};

Expand Down Expand Up @@ -201,7 +195,10 @@ export class RequestValidator {
const error = {
status: 400,
errors: errors.map(e => ({
path: (e.params && e.params.missingProperty) || e.dataPath || e.schemaPath,
path:
(e.params && e.params.missingProperty) ||
e.dataPath ||
e.schemaPath,
message: e.message,
errorCode: `${e.keyword}.openapi.validation`,
})),
Expand All @@ -213,7 +210,7 @@ export class RequestValidator {
};
}

parametersToSchema(path, parameters = []) {
private parametersToSchema(path, parameters = []) {
const schema = { query: {}, headers: {}, params: {}, cookies: {} };
const reqFields = {
query: 'query',
Expand Down
2 changes: 1 addition & 1 deletion test/routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ const basePath = (<any>app).basePath;
.expect(400)
.then(r => {
const e = r.body.errors;
expect(e[0].path).equals('id');
expect(e[0].path).contains('id');
expect(e[0].message).equals('should be integer');
});
});
Expand Down

0 comments on commit dc41ae4

Please sign in to comment.