diff --git a/src/tools/json-viewer/index.ts b/src/tools/json-viewer/index.ts
index a527d709..6283b686 100644
--- a/src/tools/json-viewer/index.ts
+++ b/src/tools/json-viewer/index.ts
@@ -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'],
diff --git a/src/tools/json-viewer/json-viewer.vue b/src/tools/json-viewer/json-viewer.vue
index 595801fd..28c9ca5e 100644
--- a/src/tools/json-viewer/json-viewer.vue
+++ b/src/tools/json-viewer/json-viewer.vue
@@ -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();
+const jsonSchemaInputElement = ref();
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({ name: 'schema', storageName: 'json-prettify:schema', defaultValue: '' });
+const { schemas, errors: validationErrors } = useJsonSchemaValidation({ json: rawJson, schemaUrl, schemaData });
@@ -52,6 +57,31 @@ const rawJsonValidation = useValidation({
+
+
+
+
+
+
+
+
-
+
diff --git a/src/tools/json-viewer/useJsonSchemaValidation.ts b/src/tools/json-viewer/useJsonSchemaValidation.ts
new file mode 100644
index 00000000..14460848
--- /dev/null
+++ b/src/tools/json-viewer/useJsonSchemaValidation.ts
@@ -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; schemaUrl: MaybeRef; schemaData: MaybeRef }) {
+ const schemas = ref([]);
+ const schema = ref(null);
+ const errors = ref([]);
+
+ 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 };
+}