Skip to content

Commit 75269c8

Browse files
committed
feat(new tool): JSON Linter
Fix part of CorentinTh#605
1 parent 9eac9cb commit 75269c8

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { tool as asciiTextDrawer } from './ascii-text-drawer';
66

77
import { tool as textToUnicode } from './text-to-unicode';
88
import { tool as safelinkDecoder } from './safelink-decoder';
9+
import { tool as jsonLinter } from './json-linter';
910
import { tool as pdfSignatureChecker } from './pdf-signature-checker';
1011
import { tool as numeronymGenerator } from './numeronym-generator';
1112
import { tool as macAddressGenerator } from './mac-address-generator';
@@ -142,6 +143,7 @@ export const toolsByCategory: ToolCategory[] = [
142143
crontabGenerator,
143144
jsonViewer,
144145
jsonMinify,
146+
jsonLinter,
145147
jsonToCsv,
146148
sqlPrettify,
147149
chmodCalculator,

src/tools/json-linter/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ArrowsShuffle } from '@vicons/tabler';
2+
import { defineTool } from '../tool';
3+
4+
export const tool = defineTool({
5+
name: 'Json linter',
6+
path: '/json-linter',
7+
description: '',
8+
keywords: ['json', 'linter'],
9+
component: () => import('./json-linter.vue'),
10+
icon: ArrowsShuffle,
11+
createdAt: new Date('2024-03-27'),
12+
});

src/tools/json-linter/json-linter.vue

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<script setup lang="ts">
2+
import linter from 'jsonlint-mod';
3+
4+
const jsonContent = ref(
5+
`{
6+
a: True;
7+
b=5
8+
}`,
9+
);
10+
11+
const conversionResult = computed(() => {
12+
try {
13+
linter.parse(jsonContent.value);
14+
return JSON.stringify(JSON.parse(jsonContent.value), null, 2);
15+
}
16+
catch (e: any) {
17+
return e.toString().split('\n').map((err: string) => ({ line: -1, message: err, helpLink: '' }));
18+
}
19+
});
20+
21+
const errors = computed(() => conversionResult.value);
22+
23+
const MONACO_EDITOR_OPTIONS = {
24+
automaticLayout: true,
25+
formatOnType: true,
26+
formatOnPaste: true,
27+
};
28+
</script>
29+
30+
<template>
31+
<div>
32+
<c-label label="Paste your JSON file content:">
33+
<div relative w-full>
34+
<c-monaco-editor
35+
v-model:value="jsonContent"
36+
theme="vs-dark"
37+
language="yaml"
38+
height="250px"
39+
:options="MONACO_EDITOR_OPTIONS"
40+
/>
41+
</div>
42+
</c-label>
43+
44+
<div v-if="errors.length > 0">
45+
<n-alert title="The following errors occured" type="error" mt-5>
46+
<ul>
47+
<li v-for="(message, index) of errors" :key="index">
48+
{{ message.message }} (<n-a v-if="message.helpLink" target="_blank" rel="noreferer noopener">
49+
See JSON help
50+
</n-a>)
51+
</li>
52+
</ul>
53+
</n-alert>
54+
</div>
55+
<div v-else>
56+
<n-alert type="success" mt-5>
57+
Validation successful!
58+
</n-alert>
59+
</div>
60+
</div>
61+
</template>

0 commit comments

Comments
 (0)