mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-20 21:23:08 +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",
|
||||
"jsonpath": "^1.1.1",
|
||||
"jsonrepair": "^3.13.0",
|
||||
"jsonschema": "^1.5.0",
|
||||
"jsonschema-protobuf": "^1.0.2",
|
||||
"jsstack.js": "^2.0.0",
|
||||
"jsvat-next": "^3.0.4",
|
||||
|
||||
Generated
+7
@@ -473,6 +473,9 @@ dependencies:
|
||||
jsonrepair:
|
||||
specifier: ^3.13.0
|
||||
version: 3.13.0
|
||||
jsonschema:
|
||||
specifier: ^1.5.0
|
||||
version: 1.5.0
|
||||
jsonschema-protobuf:
|
||||
specifier: ^1.0.2
|
||||
version: 1.0.2
|
||||
@@ -13919,6 +13922,10 @@ packages:
|
||||
tape: 4.17.0
|
||||
dev: false
|
||||
|
||||
/jsonschema@1.5.0:
|
||||
resolution: {integrity: sha512-K+A9hhqbn0f3pJX17Q/7H6yQfD/5OXgdrR5UE12gMXCiN9D5Xq2o5mddV2QEcX/bjla99ASsAAQUyMCCRWAEhw==}
|
||||
dev: false
|
||||
|
||||
/jsqr@1.4.0:
|
||||
resolution: {integrity: sha512-dxLob7q65Xg2DvstYkRpkYtmKm2sPJ9oFhrhmudT1dZvNFFTlroai3AWSpLey/w5vMcLBXRgOJsbXpdN9HzU/A==}
|
||||
dev: false
|
||||
|
||||
@@ -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'],
|
||||
keywords: ['json', 'linter', 'check', 'validator'],
|
||||
component: () => import('./json-linter.vue'),
|
||||
icon: Braces,
|
||||
createdAt: new Date('2024-03-20'),
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
<script setup lang="ts">
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import linter from 'jsonlint-mod';
|
||||
import { useJsonSchemaValidation } from '../json-viewer/useJsonSchemaValidation';
|
||||
import { useITStorage, useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
const jsonSchemaInputElement = ref<HTMLElement>();
|
||||
const schemaData = useITStorage('json-linter:schema-data', '');
|
||||
|
||||
const jsonContent = ref(
|
||||
`{
|
||||
a: True;
|
||||
@@ -26,11 +31,14 @@ const MONACO_EDITOR_OPTIONS = {
|
||||
formatOnType: true,
|
||||
formatOnPaste: true,
|
||||
};
|
||||
|
||||
const schemaUrl = useQueryParamOrStorage<string>({ name: 'schema', storageName: 'json-linter:schema', defaultValue: '' });
|
||||
const { schemas, errors: validationErrors } = useJsonSchemaValidation({ json: jsonContent, schemaUrl, schemaData });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<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>
|
||||
<c-monaco-editor
|
||||
v-model:value="jsonContent"
|
||||
@@ -42,6 +50,42 @@ const MONACO_EDITOR_OPTIONS = {
|
||||
</div>
|
||||
</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">
|
||||
<n-alert :title="t('tools.json-linter.texts.title-the-following-errors-occured')" type="error" mt-5>
|
||||
<pre>
|
||||
|
||||
@@ -6,7 +6,7 @@ export const tool = defineTool({
|
||||
name: t('tools.json-prettify.title'),
|
||||
path: '/json-prettify',
|
||||
description: t('tools.json-prettify.description'),
|
||||
keywords: ['json', 'viewer', 'prettify', 'format'],
|
||||
keywords: ['json', 'viewer', 'prettify', 'format', 'lint', 'validator', 'schema'],
|
||||
component: () => import('./json-viewer.vue'),
|
||||
icon: Braces,
|
||||
redirectFrom: ['/json-viewer'],
|
||||
|
||||
@@ -5,17 +5,19 @@ import {
|
||||
get,
|
||||
} from '@vueuse/core';
|
||||
import { formatJson } from './json.models';
|
||||
import { useJsonSchemaValidation } from './useJsonSchemaValidation';
|
||||
import { withDefaultOnError } from '@/utils/defaults';
|
||||
import { useValidation } from '@/composable/validation';
|
||||
import TextareaCopyable from '@/components/TextareaCopyable.vue';
|
||||
import { useITStorage } from '@/composable/queryParams';
|
||||
import { useITStorage, useQueryParamOrStorage } from '@/composable/queryParams';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
const inputElement = ref<HTMLElement>();
|
||||
const jsonSchemaInputElement = ref<HTMLElement>();
|
||||
const repairJsonLabel = t('tools.json-viewer.text.repair-json');
|
||||
|
||||
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 sortKeys = useITStorage('json-prettify:sort-keys', true);
|
||||
const unescapeUnicode = useITStorage('json-prettify:unescape-unicode', false);
|
||||
@@ -34,6 +36,9 @@ const rawJsonValidation = useValidation({
|
||||
],
|
||||
watch: [repairJson],
|
||||
});
|
||||
|
||||
const schemaUrl = useQueryParamOrStorage<string>({ name: 'schema', storageName: 'json-prettify:schema', defaultValue: '' });
|
||||
const { schemas, errors: validationErrors } = useJsonSchemaValidation({ json: rawJson, schemaUrl, schemaData });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -52,6 +57,31 @@ const rawJsonValidation = useValidation({
|
||||
</n-form-item>
|
||||
</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
|
||||
:label="t('tools.json-viewer.texts.label-your-raw-json')"
|
||||
:feedback="rawJsonValidation.message"
|
||||
@@ -70,8 +100,20 @@ const rawJsonValidation = useValidation({
|
||||
monospace
|
||||
/>
|
||||
</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')">
|
||||
<TextareaCopyable :value="cleanJson" language="json" :follow-height-of="inputElement" />
|
||||
<textarea-copyable :value="cleanJson" language="json" :follow-height-of="inputElement" />
|
||||
</n-form-item>
|
||||
</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