From 2ee02742e7f9c20567156fc7279c2b48d5c8a8c7 Mon Sep 17 00:00:00 2001 From: ShareVB Date: Sun, 24 Aug 2025 14:50:17 +0200 Subject: [PATCH] feat(Yaml Prettifier): add a JSON Schema Validator Allow pasting JSON Schema or fetching from schemastore.org Fix #540 --- package.json | 2 + pnpm-lock.yaml | 6 + src/tools/yaml-viewer/index.ts | 2 +- .../yaml-viewer/useYamlSchemaValidation.ts | 105 ++++++++++++++++++ src/tools/yaml-viewer/yaml-viewer.vue | 47 +++++++- 5 files changed, 159 insertions(+), 3 deletions(-) create mode 100644 src/tools/yaml-viewer/useYamlSchemaValidation.ts diff --git a/package.json b/package.json index d24bcd5c..f4243299 100644 --- a/package.json +++ b/package.json @@ -92,6 +92,8 @@ "@zxing/library": "^0.21.3", "a11y-color-contrast": "^0.2.0", "aircodes": "^1.3.16", + "ajv": "^8.17.1", + "ajv-errors": "^3.0.0", "ansi-styles": "^6.2.1", "ansible-vault": "^1.3.0", "apache-md5": "^1.1.8", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5f2e67ec..48035eaa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -164,6 +164,12 @@ dependencies: aircodes: specifier: ^1.3.16 version: 1.3.16 + ajv: + specifier: ^8.17.1 + version: 8.17.1 + ajv-errors: + specifier: ^3.0.0 + version: 3.0.0(ajv@8.17.1) ansi-styles: specifier: ^6.2.1 version: 6.2.1 diff --git a/src/tools/yaml-viewer/index.ts b/src/tools/yaml-viewer/index.ts index 30a12bfd..9eb202ad 100644 --- a/src/tools/yaml-viewer/index.ts +++ b/src/tools/yaml-viewer/index.ts @@ -6,7 +6,7 @@ export const tool = defineTool({ name: t('tools.yaml-prettify.title'), path: '/yaml-prettify', description: t('tools.yaml-prettify.description'), - keywords: ['yaml', 'viewer', 'prettify', 'format', 'lint'], + keywords: ['yaml', 'viewer', 'prettify', 'format', 'lint', 'validator', 'schema'], component: () => import('./yaml-viewer.vue'), icon: AlignJustified, createdAt: new Date('2024-01-31'), diff --git a/src/tools/yaml-viewer/useYamlSchemaValidation.ts b/src/tools/yaml-viewer/useYamlSchemaValidation.ts new file mode 100644 index 00000000..2467bfac --- /dev/null +++ b/src/tools/yaml-viewer/useYamlSchemaValidation.ts @@ -0,0 +1,105 @@ +import { type Schema } from 'jsonschema'; +import { isRef, onBeforeMount, ref, watch } from 'vue'; +import { type MaybeRef, get } from '@vueuse/core'; +import YAML from 'yaml'; +import Ajv from 'ajv'; +import AjvErrors from 'ajv-errors'; +import { yamlParse } from 'composeverter'; + +export interface SchemaStore { + name: string + description: string + url: string + fileMatch: string[] + versions?: string[] +} + +interface NodeWithRange { + range?: number[] +} + +export function useYamlSchemaValidation({ yaml, schemaUrl, schemaData }: { yaml: 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([yaml, schema].filter(isRef), () => { + const schemaValue = get(schema); + const yamlValue = get(yaml); + if (!schemaValue) { + errors.value = []; + return; + } + if (!yamlValue) { + errors.value = []; + return; + } + const parsedYaml = yamlParse(yamlValue); + const ajv = new Ajv({ allErrors: true, strict: false }); + AjvErrors(ajv); + const validate = ajv.compile(schemaValue); + const valid = validate(parsedYaml); + if (valid || validate.errors === null) { + errors.value = []; + } + else { + const yamlDoc = YAML.parseDocument(yamlValue, { prettyErrors: true }); + errors.value = validate.errors!.map((err) => { + let { instancePath } = err; + if (err.keyword === 'additionalProperties') { + instancePath += `/${err.params.additionalProperty}`; + } + const parsedPath = instancePath.split('/').slice(1); + let node = yamlDoc.getIn(parsedPath, true); + if (node === null) { + node = yamlDoc.getIn(parsedPath.slice(0, -1), true); + } + const nodeWithRange = node as NodeWithRange; + const pos = nodeWithRange?.range ? nodeWithRange.range[0] + 1 : 0; + const line = (yamlValue.substring(0, pos).match(/\n/g) || []).length + 1; + let message; + if (err.keyword === 'additionalProperties') { + message = `Line ${line}(${instancePath}): '${err.params.additionalProperty}' is unknown for '${instancePath}'`; + } + else if (err.keyword === 'format') { + message = `Line ${line}(${instancePath}): must have a valid syntax for '${err.params.format}'`; + } + else if (err.keyword === 'oneOf') { + message = `Line ${line}(${instancePath}): must be either a Short Syntax (string(s)) or a Long Syntax (object(s))`; + } + else { message = `Line ${line}(${instancePath}): ${err.message} (${err.keyword}: ${JSON.stringify(err.params)})`; } + return message; + }); + } + }, { immediate: true }); + + return { schemas, errors }; +} diff --git a/src/tools/yaml-viewer/yaml-viewer.vue b/src/tools/yaml-viewer/yaml-viewer.vue index f9606ea9..3df22325 100644 --- a/src/tools/yaml-viewer/yaml-viewer.vue +++ b/src/tools/yaml-viewer/yaml-viewer.vue @@ -1,12 +1,16 @@