mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-22 14:01:42 +03:00
feat(JSON Prettifier/Linter): add a JSON Schema Validator
Allow pasting JSON Schema or fetching from schemastore.org Fix #368, #801
This commit is contained in:
1 parent
454b347fac
commit
5f8aea2d09
7 files changed
+163
-6
No files matched your search
@@ -195,6 +195,7 @@
|
|||||||
"jsonlint-mod": "^1.7.6",
|
"jsonlint-mod": "^1.7.6",
|
||||||
"jsonpath": "^1.1.1",
|
"jsonpath": "^1.1.1",
|
||||||
"jsonrepair": "^3.13.0",
|
"jsonrepair": "^3.13.0",
|
||||||
|
"jsonschema": "^1.5.0",
|
||||||
"jsonschema-protobuf": "^1.0.2",
|
"jsonschema-protobuf": "^1.0.2",
|
||||||
"jsstack.js": "^2.0.0",
|
"jsstack.js": "^2.0.0",
|
||||||
"jsvat-next": "^3.0.4",
|
"jsvat-next": "^3.0.4",
|
||||||
|
|||||||
Generated
+7
@@ -473,6 +473,9 @@ dependencies:
|
|||||||
jsonrepair:
|
jsonrepair:
|
||||||
specifier: ^3.13.0
|
specifier: ^3.13.0
|
||||||
version: 3.13.0
|
version: 3.13.0
|
||||||
|
jsonschema:
|
||||||
|
specifier: ^1.5.0
|
||||||
|
version: 1.5.0
|
||||||
jsonschema-protobuf:
|
jsonschema-protobuf:
|
||||||
specifier: ^1.0.2
|
specifier: ^1.0.2
|
||||||
version: 1.0.2
|
version: 1.0.2
|
||||||
@@ -13919,6 +13922,10 @@ packages:
|
|||||||
tape: 4.17.0
|
tape: 4.17.0
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
|
/jsonschema@1.5.0:
|
||||||
|
resolution: {integrity: sha512-K+A9hhqbn0f3pJX17Q/7H6yQfD/5OXgdrR5UE12gMXCiN9D5Xq2o5mddV2QEcX/bjla99ASsAAQUyMCCRWAEhw==}
|
||||||
|
dev: false
|
||||||
|
|
||||||
/jsqr@1.4.0:
|
/jsqr@1.4.0:
|
||||||
resolution: {integrity: sha512-dxLob7q65Xg2DvstYkRpkYtmKm2sPJ9oFhrhmudT1dZvNFFTlroai3AWSpLey/w5vMcLBXRgOJsbXpdN9HzU/A==}
|
resolution: {integrity: sha512-dxLob7q65Xg2DvstYkRpkYtmKm2sPJ9oFhrhmudT1dZvNFFTlroai3AWSpLey/w5vMcLBXRgOJsbXpdN9HzU/A==}
|
||||||
dev: false
|
dev: false
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ export const tool = defineTool({
|
|||||||
name: t('tools.json-linter.title'),
|
name: t('tools.json-linter.title'),
|
||||||
path: '/json-linter',
|
path: '/json-linter',
|
||||||
description: t('tools.json-linter.description'),
|
description: t('tools.json-linter.description'),
|
||||||
keywords: ['json', 'linter', 'check'],
|
keywords: ['json', 'linter', 'check', 'validator'],
|
||||||
component: () => import('./json-linter.vue'),
|
component: () => import('./json-linter.vue'),
|
||||||
icon: Braces,
|
icon: Braces,
|
||||||
createdAt: new Date('2024-03-20'),
|
createdAt: new Date('2024-03-20'),
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { useI18n } from 'vue-i18n';
|
import { useI18n } from 'vue-i18n';
|
||||||
import linter from 'jsonlint-mod';
|
import linter from 'jsonlint-mod';
|
||||||
|
import { useJsonSchemaValidation } from '../json-viewer/useJsonSchemaValidation';
|
||||||
|
import { useITStorage, useQueryParamOrStorage } from '@/composable/queryParams';
|
||||||
|
|
||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
|
|
||||||
|
const jsonSchemaInputElement = ref<HTMLElement>();
|
||||||
|
const schemaData = useITStorage('json-linter:schema-data', '');
|
||||||
|
|
||||||
const jsonContent = ref(
|
const jsonContent = ref(
|
||||||
`{
|
`{
|
||||||
a: True;
|
a: True;
|
||||||
@@ -26,11 +31,14 @@ const MONACO_EDITOR_OPTIONS = {
|
|||||||
formatOnType: true,
|
formatOnType: true,
|
||||||
formatOnPaste: true,
|
formatOnPaste: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const schemaUrl = useQueryParamOrStorage<string>({ name: 'schema', storageName: 'json-linter:schema', defaultValue: '' });
|
||||||
|
const { schemas, errors: validationErrors } = useJsonSchemaValidation({ json: jsonContent, schemaUrl, schemaData });
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div>
|
<div>
|
||||||
<c-label :label="t('tools.json-linter.texts.label-paste-your-json-file-content')">
|
<c-label :label="t('tools.json-linter.texts.label-paste-your-json-file-content')" mb-2>
|
||||||
<div relative w-full>
|
<div relative w-full>
|
||||||
<c-monaco-editor
|
<c-monaco-editor
|
||||||
v-model:value="jsonContent"
|
v-model:value="jsonContent"
|
||||||
@@ -42,6 +50,42 @@ const MONACO_EDITOR_OPTIONS = {
|
|||||||
</div>
|
</div>
|
||||||
</c-label>
|
</c-label>
|
||||||
|
|
||||||
|
<n-form-item label="JSON schema:" label-placement="left" label-width="130px" label-align="right">
|
||||||
|
<n-select
|
||||||
|
v-model:value="schemaUrl"
|
||||||
|
:options="[
|
||||||
|
{ label: 'No validation', value: '' },
|
||||||
|
{ 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="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="Schema Validation Errors" type="error">
|
||||||
|
<ul
|
||||||
|
v-for="error in validationErrors"
|
||||||
|
:key="error"
|
||||||
|
>
|
||||||
|
<li>{{ error }}</li>
|
||||||
|
</ul>
|
||||||
|
</n-alert>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div v-if="conversionError">
|
<div v-if="conversionError">
|
||||||
<n-alert :title="t('tools.json-linter.texts.title-the-following-errors-occured')" type="error" mt-5>
|
<n-alert :title="t('tools.json-linter.texts.title-the-following-errors-occured')" type="error" mt-5>
|
||||||
<pre>
|
<pre>
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ export const tool = defineTool({
|
|||||||
name: t('tools.json-prettify.title'),
|
name: t('tools.json-prettify.title'),
|
||||||
path: '/json-prettify',
|
path: '/json-prettify',
|
||||||
description: t('tools.json-prettify.description'),
|
description: t('tools.json-prettify.description'),
|
||||||
keywords: ['json', 'viewer', 'prettify', 'format'],
|
keywords: ['json', 'viewer', 'prettify', 'format', 'lint', 'validator', 'schema'],
|
||||||
component: () => import('./json-viewer.vue'),
|
component: () => import('./json-viewer.vue'),
|
||||||
icon: Braces,
|
icon: Braces,
|
||||||
redirectFrom: ['/json-viewer'],
|
redirectFrom: ['/json-viewer'],
|
||||||
|
|||||||
@@ -5,17 +5,19 @@ import {
|
|||||||
get,
|
get,
|
||||||
} from '@vueuse/core';
|
} from '@vueuse/core';
|
||||||
import { formatJson } from './json.models';
|
import { formatJson } from './json.models';
|
||||||
|
import { useJsonSchemaValidation } from './useJsonSchemaValidation';
|
||||||
import { withDefaultOnError } from '@/utils/defaults';
|
import { withDefaultOnError } from '@/utils/defaults';
|
||||||
import { useValidation } from '@/composable/validation';
|
import { useValidation } from '@/composable/validation';
|
||||||
import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
import { useITStorage, useQueryParamOrStorage } from '@/composable/queryParams';
|
||||||
import { useITStorage } from '@/composable/queryParams';
|
|
||||||
|
|
||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
|
|
||||||
const inputElement = ref<HTMLElement>();
|
const inputElement = ref<HTMLElement>();
|
||||||
|
const jsonSchemaInputElement = ref<HTMLElement>();
|
||||||
const repairJsonLabel = t('tools.json-viewer.text.repair-json');
|
const repairJsonLabel = t('tools.json-viewer.text.repair-json');
|
||||||
|
|
||||||
const rawJson = useITStorage('json-prettify:raw-json', '{"hello": "world", "foo": "bar"}');
|
const rawJson = useITStorage('json-prettify:raw-json', '{"hello": "world", "foo": "bar"}');
|
||||||
|
const schemaData = useITStorage('json-prettify:schema-data', '');
|
||||||
const indentSize = useITStorage('json-prettify:indent-size', 3);
|
const indentSize = useITStorage('json-prettify:indent-size', 3);
|
||||||
const sortKeys = useITStorage('json-prettify:sort-keys', true);
|
const sortKeys = useITStorage('json-prettify:sort-keys', true);
|
||||||
const unescapeUnicode = useITStorage('json-prettify:unescape-unicode', false);
|
const unescapeUnicode = useITStorage('json-prettify:unescape-unicode', false);
|
||||||
@@ -34,6 +36,9 @@ const rawJsonValidation = useValidation({
|
|||||||
],
|
],
|
||||||
watch: [repairJson],
|
watch: [repairJson],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const schemaUrl = useQueryParamOrStorage<string>({ name: 'schema', storageName: 'json-prettify:schema', defaultValue: '' });
|
||||||
|
const { schemas, errors: validationErrors } = useJsonSchemaValidation({ json: rawJson, schemaUrl, schemaData });
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -52,6 +57,31 @@ const rawJsonValidation = useValidation({
|
|||||||
</n-form-item>
|
</n-form-item>
|
||||||
</n-space>
|
</n-space>
|
||||||
|
|
||||||
|
<n-form-item label="JSON schema:" label-placement="left" label-width="130px" label-align="right">
|
||||||
|
<n-select
|
||||||
|
v-model:value="schemaUrl"
|
||||||
|
:options="[
|
||||||
|
{ label: 'No validation', value: '' },
|
||||||
|
{ 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="Paste your JSON Schema here..."
|
||||||
|
rows="20"
|
||||||
|
multiline
|
||||||
|
autocomplete="off"
|
||||||
|
autocorrect="off"
|
||||||
|
autocapitalize="off"
|
||||||
|
spellcheck="false"
|
||||||
|
monospace
|
||||||
|
/>
|
||||||
|
|
||||||
<n-form-item
|
<n-form-item
|
||||||
:label="t('tools.json-viewer.texts.label-your-raw-json')"
|
:label="t('tools.json-viewer.texts.label-your-raw-json')"
|
||||||
:feedback="rawJsonValidation.message"
|
:feedback="rawJsonValidation.message"
|
||||||
@@ -70,8 +100,20 @@ const rawJsonValidation = useValidation({
|
|||||||
monospace
|
monospace
|
||||||
/>
|
/>
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
|
|
||||||
|
<div v-if="validationErrors.length > 0" mb-2 mt-2>
|
||||||
|
<n-alert title="Schema Validation Errors" type="error">
|
||||||
|
<ul
|
||||||
|
v-for="error in validationErrors"
|
||||||
|
:key="error"
|
||||||
|
>
|
||||||
|
<li>{{ error }}</li>
|
||||||
|
</ul>
|
||||||
|
</n-alert>
|
||||||
|
</div>
|
||||||
|
|
||||||
<n-form-item :label="t('tools.json-viewer.texts.label-prettified-version-of-your-json')">
|
<n-form-item :label="t('tools.json-viewer.texts.label-prettified-version-of-your-json')">
|
||||||
<TextareaCopyable :value="cleanJson" language="json" :follow-height-of="inputElement" />
|
<textarea-copyable :value="cleanJson" language="json" :follow-height-of="inputElement" />
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,63 @@
|
|||||||
|
import { type Schema, Validator } from 'jsonschema';
|
||||||
|
import { isRef, onBeforeMount, ref, watch } from 'vue';
|
||||||
|
import { type MaybeRef, get } from '@vueuse/core';
|
||||||
|
|
||||||
|
export interface SchemaStore {
|
||||||
|
name: string
|
||||||
|
description: string
|
||||||
|
url: string
|
||||||
|
fileMatch: string[]
|
||||||
|
versions?: string[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useJsonSchemaValidation({ json, schemaUrl, schemaData }: { json: MaybeRef<string>; schemaUrl: MaybeRef<string>; schemaData: MaybeRef<string> }) {
|
||||||
|
const schemas = ref<SchemaStore[]>([]);
|
||||||
|
const schema = ref<Schema | null>(null);
|
||||||
|
const errors = ref<string[]>([]);
|
||||||
|
|
||||||
|
onBeforeMount(async () => {
|
||||||
|
const catalog = await fetch('https://www.schemastore.org/api/json/catalog.json');
|
||||||
|
const catalogJson: { $schemaUrl: string; schemas: SchemaStore[]; version: number } = await catalog.json();
|
||||||
|
schemas.value = catalogJson.schemas;
|
||||||
|
});
|
||||||
|
|
||||||
|
watch([schemaUrl, schemaData].filter(isRef), async () => {
|
||||||
|
if (get(schemaUrl) === 'custom') {
|
||||||
|
try {
|
||||||
|
schema.value = JSON.parse(get(schemaData)) as Schema;
|
||||||
|
}
|
||||||
|
catch (e: any) {
|
||||||
|
errors.value = [`Schema parsing error:${e.toString()}`];
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (get(schemaUrl)) {
|
||||||
|
try {
|
||||||
|
const response = await fetch(get(schemaUrl));
|
||||||
|
const schemaJson = await response.json();
|
||||||
|
schema.value = schemaJson;
|
||||||
|
}
|
||||||
|
catch (e: any) {
|
||||||
|
errors.value = [`Schema fetching error:${e.toString()}`];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, { immediate: true });
|
||||||
|
|
||||||
|
watch([json, schema].filter(isRef), () => {
|
||||||
|
const schemaValue = get(schema);
|
||||||
|
const jsonValue = get(json);
|
||||||
|
if (!schemaValue) {
|
||||||
|
errors.value = [];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!jsonValue) {
|
||||||
|
errors.value = [];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const validator = new Validator();
|
||||||
|
const validationResult = validator.validate(JSON.parse(jsonValue), schemaValue);
|
||||||
|
errors.value = validationResult.errors.map(error => error.stack ?? '');
|
||||||
|
}, { immediate: true });
|
||||||
|
|
||||||
|
return { schemas, errors };
|
||||||
|
}
|
||||||
Reference in new issue
Block a user