Skip to content

Commit

Permalink
improve new validator
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Jul 18, 2019
1 parent e2e8b3e commit a0368ce
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
10 changes: 5 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ export class OpenApiValidator {
middlewares.applyOpenApiMetadata(this.context),
middlewares.multipart(this.context, this.multerOpts),
validateMiddleware);
// middlewares.validateRequest({
// apiDoc: this.context.apiDoc,
// loggingKey,
// enableObjectCoercion: this.opts.enableObjectCoercion,
// }),
// middlewares.validateRequest({
// apiDoc: this.context.apiDoc,
// loggingKey,
// enableObjectCoercion: this.opts.enableObjectCoercion,
// }),
// );
}
}
32 changes: 13 additions & 19 deletions src/middlewares/openapi.request.validator.2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,16 +196,19 @@ export class RequestValidator {
next();
} else {
const errors = validator.errors;
const errorText = this.ajv.errorsText(errors, { dataVar: 'request' });


if (errors.length > 0) {
const message = errors[0].message;
throw ono(errors, message);
}
// throw validationError(400, path, errorText, errors);
// next(
// new AoavError(`Error while validating request: ${errorText}`, errors),
// );
const error = {
status: 400,
errors: errors.map(e => ({
path: (e.params && e.params.missingProperty) || e.schemaPath,
message: e.message,
errorCode: `${e.keyword}.openapi.validation`,
})),
};
const message = this.ajv.errorsText(errors, { dataVar: 'request' });
throw ono(error, message);
}
}
};
}
Expand Down Expand Up @@ -244,7 +247,7 @@ export class RequestValidator {
}

if (!parameterSchema) {
const message = `Not available parameter 'schema' or 'content' for [${parameter.name}]`
const message = `Not available parameter 'schema' or 'content' for [${parameter.name}]`;
const err = validationError(400, path, message);
throw ono(err, message);
}
Expand All @@ -268,12 +271,3 @@ export class RequestValidator {
return { schema, parseJson };
}
}

// export class AoavError extends Error {
// private data;
// constructor(message, errors) {
// super(message);
// this.name = this.constructor.name;
// this.data = errors;
// }
// }
4 changes: 2 additions & 2 deletions test/routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import * as request from 'supertest';
import { createApp } from './app';
const apiSpec = require('./resources/openapi.json');
const app = createApp({ apiSpecPath: './openapi.yaml' }, 3001);
const app2 = createApp({ apiSpec }, 3002);
// const app2 = createApp({ apiSpec }, 3002);

const packageJson = require('../package.json');
const basePath = (<any>app).basePath;

[app, app2].forEach(app => {
[app].forEach(app => {
describe(packageJson.name, () => {
after(() => {
(<any>app).server.close();
Expand Down

0 comments on commit a0368ce

Please sign in to comment.