Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: informative Stubs when document is invalid #1068

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions library/src/containers/Error/Error.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ const renderErrors = (errors: ValidationError[]): React.ReactNode => {

return errors
.map((singleError: ValidationError, index: number) => {
if (!singleError?.title || !singleError.location) {
if (!singleError?.title) {
return null;
}
return (
<div key={index} className="flex">
<span>{`${singleError.location.startLine}.`}</span>
<div key={index} className="flex gap-2">
{/* <span>{`${singleError?.location?.startLine}.${singleError?.location?.startColumn}`}</span> */}
{/* <span> </span> */}
catosaurusrex2003 marked this conversation as resolved.
Show resolved Hide resolved
<code className="whitespace-pre-wrap break-all ml-2">
{singleError.title}
</code>
Expand Down
41 changes: 38 additions & 3 deletions library/src/helpers/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import { OpenAPISchemaParser } from '@asyncapi/openapi-schema-parser';
import { ProtoBuffSchemaParser } from '@asyncapi/protobuf-schema-parser';
import { AvroSchemaParser } from '@asyncapi/avro-schema-parser';

import { ErrorObject, ParserReturn, FetchingSchemaInterface } from '../types';
import {
ErrorObject,
ParserReturn,
FetchingSchemaInterface,
ValidationError,
} from '../types';

import { VALIDATION_ERRORS_TYPE } from '../constants';

Expand All @@ -22,8 +27,38 @@ export class Parser {
): Promise<ParserReturn> {
try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const { document } = await asyncapiParser.parse(content, parserOptions);
return { asyncapi: document };
const parseResult = await asyncapiParser.parse(content, parserOptions);

let error: {
title: string | undefined;
validationErrors: ValidationError[] | undefined;
} = {
title: 'There are errors in your Asyncapi document',
validationErrors: [],
};

if (parseResult.document === undefined) {
parseResult.diagnostics.forEach((diagnostic) => {
if (diagnostic.severity == 0) {
const tempObj = {
title: diagnostic.message,
location: {
jsonPointer: 'json pointer',
startLine: diagnostic.range.start.line,
startColumn: diagnostic.range.start.character,
startOffset: 0,
endLine: diagnostic.range.end.line,
endColumn: diagnostic.range.end.character,
endOffset: 0,
},
};
error.validationErrors?.push(tempObj as unknown as ValidationError);
}
});
throw error;
}

return { asyncapi: parseResult.document };
} catch (err) {
return this.handleError(err as ErrorObject);
}
Expand Down
Loading