Skip to content
Open
Changes from all commits
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
25 changes: 24 additions & 1 deletion src/components/schema-editor/schema-editor.component.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState, useEffect, useCallback } from 'react';
import AceEditor from 'react-ace';
import 'ace-builds/webpack-resolver';
import 'ace-builds/src-noconflict/mode-json';
import 'ace-builds/src-noconflict/theme-textmate';
import { addCompleter } from 'ace-builds/src-noconflict/ext-language_tools';
import type { IMarker } from 'react-ace';
import { useTranslation } from 'react-i18next';
Expand Down Expand Up @@ -39,6 +40,7 @@ const SchemaEditor: React.FC<SchemaEditorProps> = ({
Array<{ name: string; type: string; path: string }>
>([]);
const [currentIndex, setCurrentIndex] = useState<number>(0);
const [annotations, setAnnotations] = useState<Array<any>>([]);

// Enable autocompletion in the schema
const generateAutocompleteSuggestions = useCallback(() => {
Expand Down Expand Up @@ -160,9 +162,29 @@ const SchemaEditor: React.FC<SchemaEditorProps> = ({
};
});

const errorAnnotations = validate.errors.map((error) => {
const schemaPath = error.schemaPath.replace(/^#\//, '');
const lineNumber = traverse(schemaPath);
const pathSegments = error.instancePath.split('.');
const errorPropertyName = pathSegments[pathSegments.length - 1];
const message =
error.keyword === 'type' || error.keyword === 'enum'
? `${errorPropertyName.charAt(0).toUpperCase() + errorPropertyName.slice(1)} ${error.message}`
: `${error.message.charAt(0).toUpperCase() + error.message.slice(1)}`;

return {
row: lineNumber,
column: 0,
text: message,
type: 'error',
};
});

setErrors(errorMarkers);
setAnnotations(errorAnnotations);
} else {
setErrors([]);
setAnnotations([]);
}
} catch (error) {
console.error('Error parsing or validating JSON:', error);
Expand Down Expand Up @@ -246,6 +268,7 @@ const SchemaEditor: React.FC<SchemaEditorProps> = ({
tabSize: 2,
}}
markers={errors}
annotations={annotations}
/>
</div>
);
Expand Down