feat(JSON Linter): add auto repair option

Fix #215
This commit is contained in:
ShareVB committed 2025-12-06 18:47:38 +01:00
1 parent 99f4be60ec
commit f0d2347ee2
3 files changed
+19 -3

No files matched your search

+2
View File
@@ -2407,6 +2407,8 @@ tools:
title-schema-validation-errors: Schema Validation Errors
title-formatted-version: Formatted version
label-indent-size-0-compact: 'Indent size (0 = Compact):'
title-repaired-version: Auto Repaired and Formatted version
label-auto-repair-json: 'Auto Repair JSON:'
json-size-analyzer:
title: Json Size Analyzer
description: Measure JSON nodes relative weights
+1 -1
View File
@@ -6,7 +6,7 @@ export const tool = defineTool({
name: t('tools.json-linter.title'),
path: '/json-linter',
description: t('tools.json-linter.description'),
keywords: ['json', 'linter', 'check', 'validator'],
keywords: ['json', 'linter', 'check', 'validator', 'repair'],
component: () => import('./json-linter.vue'),
icon: Braces,
createdAt: new Date('2024-03-20'),
+16 -2
View File
@@ -1,6 +1,7 @@
<script setup lang="ts">
import { useI18n } from 'vue-i18n';
import linter from 'jsonlint-mod';
import { jsonrepair } from 'jsonrepair';
import { useJsonSchemaValidation } from '../json-viewer/useJsonSchemaValidation';
import { useITStorage, useQueryParamOrStorage } from '@/composable/queryParams';
@@ -36,9 +37,16 @@ const schemaUrl = useQueryParamOrStorage<string>({ name: 'schema', storageName:
const { schemas, errors: validationErrors } = useJsonSchemaValidation({ json: jsonContent, schemaUrl, schemaData });
const indentSize = useITStorage('json-linter:indent-size', 3);
const autoRepair = useITStorage('json-linter:repair', false);
const cleanJson = computed(() => {
try {
return JSON.stringify(JSON.parseBigInt(jsonContent.value), null, indentSize.value);
let jsonContentValue = jsonContent.value;
if (autoRepair.value) {
jsonContentValue = jsonrepair(jsonContentValue);
}
return JSON.stringify(
JSON.parseBigInt(jsonContentValue),
null, indentSize.value);
}
catch (e: any) {
return e.toString();
@@ -60,6 +68,12 @@ const cleanJson = computed(() => {
</div>
</c-label>
<n-space justify="center" mb-2>
<n-form-item :label="t('tools.json-linter.texts.label-auto-repair-json')" label-placement="left">
<n-switch v-model:value="autoRepair" />
</n-form-item>
</n-space>
<n-form-item :label="t('tools.json-linter.texts.label-json-schema')" label-placement="left" label-width="130px" label-align="right">
<n-select
v-model:value="schemaUrl"
@@ -109,7 +123,7 @@ const cleanJson = computed(() => {
</n-alert>
</div>
<c-card v-if="!conversionError" :title="t('tools.json-linter.texts.title-formatted-version')" mt-5>
<c-card v-if="!conversionError || autoRepair" :title="autoRepair ? t('tools.json-linter.texts.title-repaired-version') : t('tools.json-linter.texts.title-formatted-version')" mt-5>
<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>