Skip to content

Commit dbd56fb

Browse files
committed
Update logging for validation errors
1 parent 804a92d commit dbd56fb

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/DefinitionGenerator.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,14 @@ export class DefinitionGenerator {
6767
return this;
6868
}
6969

70-
public validate (): { valid: boolean, context: string[], warnings: any[] } {
70+
public validate (): { valid: boolean, context: string[], warnings: any[], error?: any[] } {
7171
const payload: any = {};
7272

73-
openApiValidator.validateSync(this.definition, payload);
73+
try {
74+
openApiValidator.validateSync(this.definition, payload);
75+
} catch (error) {
76+
payload.error = JSON.parse(error.message.replace(/^Failed OpenAPI3 schema validation: /, ''));
77+
}
7478

7579
return payload;
7680
}
@@ -301,7 +305,7 @@ export class DefinitionGenerator {
301305
merge(content, { [responseKey] : resModelConfig });
302306
}
303307
}
304-
// console.log(content);
308+
305309
return content;
306310
}
307311

src/ServerlessOpenApiDocumentation.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as c from 'chalk';
22
import * as fs from 'fs';
33
import * as YAML from 'js-yaml';
4+
import { inspect } from 'util';
45
import { DefinitionGenerator } from './DefinitionGenerator';
56
import { IDefinitionType, ILog } from './types';
67
import { merge } from './utils';
@@ -129,10 +130,15 @@ export class ServerlessOpenApiDocumentation {
129130
if (validation.valid) {
130131
this.log(`${ c.bold.green('[VALIDATION]') } OpenAPI valid: ${c.bold.green('true')}\n\n`);
131132
} else {
132-
this.log(
133-
`${c.bold.red('[VALIDATION]')} Failed to validate OpenAPI document: \n\n` +
134-
`${c.bold.green('Path:')} ${JSON.stringify(validation, null, 2)}\n`,
135-
);
133+
this.log(`${c.bold.red('[VALIDATION]')} Failed to validate OpenAPI document: \n\n`);
134+
this.log(`${c.bold.green('Path:')} ${JSON.stringify(validation.context, null, 2)}\n`);
135+
136+
for (const info of validation.error) {
137+
this.log(c.grey('\n\n--------\n\n'));
138+
this.log(' ', info.schemaPath, c.bold.yellow(info.message));
139+
this.log(c.grey('\n\n--------\n\n'));
140+
this.log(`${inspect(info, { colors: true, depth: 2 })}\n\n`);
141+
}
136142
}
137143

138144
const { definition } = generator;
@@ -152,6 +158,6 @@ export class ServerlessOpenApiDocumentation {
152158

153159
fs.writeFileSync(config.file, output);
154160

155-
this.log(`${ c.bold.green('[SUCCESS]') } Output file to "${c.bold.red(config.file)}"\n`);
161+
this.log(`${ c.bold.green('[OUTPUT]') } To "${c.bold.red(config.file)}"\n`);
156162
}
157163
}

test/project/openapi.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,4 @@ servers: []
217217
info:
218218
title: ''
219219
description: ''
220-
version: b563e19d-b6a2-4c35-ab1d-99c8d718119d
220+
version: a9cb0cf9-ab05-4a6b-9077-5818f1d1afb5

0 commit comments

Comments
 (0)