Skip to content

Commit

Permalink
feat: improve oas converter error output (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
bzp2010 authored Sep 6, 2024
1 parent 79edaa7 commit 8bd118d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
25 changes: 22 additions & 3 deletions libs/converter-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Listr } from 'listr2';
import { isEmpty } from 'lodash';
import { OpenAPIV3 } from 'openapi-types';
import slugify from 'slugify';
import { ZodError } from 'zod';

import { ExtKey, parseExtPlugins } from './extension';
import { parseSeprateService, parseUpstream } from './parser';
Expand All @@ -17,9 +18,27 @@ export class OpenAPIConverter implements ADCSDK.Converter {
return new Listr<{ local: ADCSDK.Configuration }>([
{
title: 'Validate OpenAPI document',
task: () => {
const res = schema.safeParse(oas);
if (!res.success) throw new Error(res.error.toString()); //TODO optimize it
task: (ctx, task) => {
const result = schema.safeParse(oas);

if (!result.success) {
let err =
'Validate OpenAPI document\nThe following errors were found in OpenAPI document:\n';

if ('error' in result) {
(result.error as ZodError).errors.forEach((error, idx) => {
err += `#${idx + 1} ${error.message}, field: "${(
error.path ?? []
).join('.')}"\n`;
task.output = this.buildDebugOutput([
`#${idx + 1} raw error: `,
JSON.stringify(error),
]);
});
}

throw new Error(err);
}
},
},
{
Expand Down
7 changes: 6 additions & 1 deletion libs/converter-openapi/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ export const schema = z.object({
servers: z
.array(
z.object({
url: z.string().regex(/https?:\/\//g),
url: z
.string()
.regex(
/https?:\/\//g,
'The URL must be start with "https://" or "http://"',
),
variables: z.optional(
z.record(
z.string(),
Expand Down
1 change: 1 addition & 0 deletions libs/converter-openapi/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"declaration": true,
"esModuleInterop": true,
"types": [
"node"
],
Expand Down

0 comments on commit 8bd118d

Please sign in to comment.