feat(JSON Editor): add Repair and schema validation

Fix #226
This commit is contained in:
ShareVB committed 2025-12-14 20:06:49 +01:00
1 parent 20c0791fc6
commit fbe9c58cc4
3 files changed
+1536 -1359

No files matched your search

+1459 -1356
View File
File diff suppressed because it is too large. Load diff
+1 -1
View File
@@ -6,7 +6,7 @@ export const tool = defineTool({
name: t('tools.json-editor.title'),
path: '/json-editor',
description: t('tools.json-editor.description'),
keywords: ['json', 'editor'],
keywords: ['json', 'editor', 'validator', 'repair'],
component: () => import('./json-editor.vue'),
icon: Braces,
createdAt: new Date('2024-05-11'),
+76 -2
View File
@@ -3,6 +3,9 @@ import { useI18n } from 'vue-i18n';
import JsonEditorVue from 'json-editor-vue';
import { type AfterSelection, type InsideSelection, type JSONEditorSelection, type KeySelection, type MultiSelection, type ValueSelection, isAfterSelection, isInsideSelection, isKeySelection, isMultiSelection, isValueSelection, stringifyJSONPath } from 'vanilla-jsoneditor';
import 'vanilla-jsoneditor/themes/jse-theme-dark.css';
import { jsonrepair } from 'jsonrepair';
import { useJsonSchemaValidation } from '../json-viewer/useJsonSchemaValidation';
import { useITStorage, useQueryParamOrStorage } from '@/composable/queryParams';
import { useStyleStore } from '@/stores/style.store';
const { t } = useI18n();
@@ -32,6 +35,32 @@ function updateJsonPath(selection: JSONEditorSelection) {
jsonPath.value = 'No available in this mode';
}
}
const jsonSchemaInputElement = ref<HTMLElement>();
const schemaData = useITStorage('json-linter:schema-data', '');
const schemaUrl = useQueryParamOrStorage<string>({ name: 'schema', storageName: 'json-editor:schema', defaultValue: '' });
const { schemas, errors: validationErrors } = useJsonSchemaValidation({ json: jsonText, schemaUrl, schemaData });
function tryAutoRepair() {
try {
jsonText.value = jsonrepair(jsonText.value);
}
catch {}
}
const indentSize = useITStorage('json-editor:indent-size', 3);
const formattedJson = computed(() => {
const jsonTextValue = jsonText.value;
try {
return JSON.stringify(
JSON.parseBigInt(jsonTextValue),
null, indentSize.value);
}
catch {
return jsonTextValue;
}
});
</script>
<template>
@@ -44,14 +73,59 @@ function updateJsonPath(selection: JSONEditorSelection) {
mb-2
/>
<n-space justify="center" mb-2>
<n-button @click="tryAutoRepair">
{{ t('tools.json-editor.texts.repair-json') }}
</n-button>
</n-space>
<n-form-item :label="t('tools.json-editor.texts.label-current-selected-node-jsonpath')">
<textarea-copyable :value="jsonPath" />
</n-form-item>
<n-divider />
<n-form-item :label="t('tools.json-editor.texts.label-your-edited-json')">
<textarea-copyable :value="jsonText" language="json" />
<n-form-item :label="t('tools.json-linter.texts.label-indent-size-0-compact')" label-placement="left">
<n-input-number-i18n v-model:value="indentSize" min="0" max="10" style="width: 100px" />
</n-form-item>
<n-form-item :label="t('tools.json-editor.texts.label-your-edited-json')">
<textarea-copyable :value="formattedJson" language="json" />
</n-form-item>
<n-form-item :label="t('tools.json-editor.texts.label-json-schema')" label-placement="left" label-width="130px" label-align="right">
<n-select
v-model:value="schemaUrl"
:options="[
{ label: t('tools.json-editor.texts.label-no-validation'), value: '' },
{ label: t('tools.json-editor.texts.label-custom'), value: 'custom' },
...schemas.map(s => ({ label: `${s.name} / ${s.description}`, value: s.url })),
]"
filterable mb-4
/>
</n-form-item>
<c-input-text
v-if="schemaUrl === 'custom'"
ref="jsonSchemaInputElement"
v-model:value="schemaData"
:placeholder="t('tools.json-editor.texts.placeholder-paste-your-json-schema-here')"
rows="20"
multiline
autocomplete="off"
autocorrect="off"
autocapitalize="off"
spellcheck="false"
monospace
/>
<div v-if="validationErrors.length > 0" mb-2 mt-2>
<n-alert :title="t('tools.json-editor.texts.title-schema-validation-errors')" type="error">
<ul
v-for="error in validationErrors"
:key="error"
>
<li>{{ error }}</li>
</ul>
</n-alert>
</div>
</div>
</template>