From 8561857d146ede24dbffa4d183f35e10e19e3458 Mon Sep 17 00:00:00 2001 From: Kevin Chappell Date: Mon, 18 Nov 2024 21:30:23 -0800 Subject: [PATCH] feat: add to formData json new schema key added to json BREAKING CHANGE: adding a new key to formData could potentially break existing software that assumes only the old keys --- package.json | 1 + src/lib/js/components/index.js | 7 +++-- src/lib/js/constants.js | 2 +- tools/generate-json-schema.ts | 53 +++++++++++++--------------------- 4 files changed, 27 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index faffccbc..fa54dea8 100644 --- a/package.json +++ b/package.json @@ -67,6 +67,7 @@ "build:lib": "vite build --config vite.config.lib.mjs", "build": "npm-run-all -p build:icons build:demo", "prebuild": "npm run build:lib", + "postbuild": "npm run generate:jsonSchema", "build:demo": "vite build --mode demo", "build:demo:watch": "vite build --mode demo --watch", "build:icons": "node ./tools/generate-sprite", diff --git a/src/lib/js/components/index.js b/src/lib/js/components/index.js index 988472b8..bc8e72bf 100644 --- a/src/lib/js/components/index.js +++ b/src/lib/js/components/index.js @@ -7,7 +7,7 @@ import RowsData from './rows/index.js' import ColumnsData from './columns/index.js' import FieldsData from './fields/index.js' import ExternalsData from './externals.js' -import { SESSION_FORMDATA_KEY } from '../constants.js' +import { SESSION_FORMDATA_KEY, version } from '../constants.js' export const Stages = StagesData export const Rows = RowsData @@ -78,7 +78,10 @@ export class Components extends Data { } get json() { - return window.JSON.stringify(this.formData) + return window.JSON.stringify({ + $schema: `https://cdn.jsdelivr.net/npm/formeo@${version}/dist/formData_schema.json`, + ...this.formData, + }) } get formData() { diff --git a/src/lib/js/constants.js b/src/lib/js/constants.js index ac187a80..d2ec07d6 100644 --- a/src/lib/js/constants.js +++ b/src/lib/js/constants.js @@ -3,7 +3,7 @@ import pkg from '../../../package.json' with { type: 'json' } const isProd = import.meta.env?.PROD const name = pkg.name -const version = pkg.version +export const version = pkg.version export const PACKAGE_NAME = name export const formeoSpriteId = 'formeo-sprite' diff --git a/tools/generate-json-schema.ts b/tools/generate-json-schema.ts index 305e4173..1ce67d7a 100644 --- a/tools/generate-json-schema.ts +++ b/tools/generate-json-schema.ts @@ -82,26 +82,28 @@ const formDataSchema = z id: z.string().optional(), }) .optional(), - content: z.string().optional(), + content: z.any().optional(), action: z.object({}).optional(), options: z .array( - z.object({ - label: z.string(), - value: z.string().optional(), - selected: z.boolean().optional(), - checked: z.boolean().optional(), - type: z - .array( - z.object({ - type: z.string(), - label: z.string(), - // value: z.string().optional(), - selected: z.boolean().optional(), - }), - ) - .optional(), - }), + z + .object({ + label: z.string(), + value: z.string().optional(), + selected: z.boolean().optional(), + checked: z.boolean().optional(), + type: z + .array( + z.object({ + type: z.string(), + label: z.string(), + // value: z.string().optional(), + selected: z.boolean().optional(), + }), + ) + .optional(), + }) + .catchall(z.any()), ) .optional(), conditions: z @@ -155,22 +157,7 @@ const reorderSchema = (schema: object) => { } } -function deepRemoveKeys(obj, exclude) { - if (Array.isArray(obj)) { - return obj.map(i => deepRemoveKeys(i, exclude)) - } - if (typeof obj === 'object') { - return Object.fromEntries( - Object.entries(obj) - .filter(([k, v]) => !(k in exclude && (exclude[k] === undefined || exclude[k] === v))) - .map(([k, v]) => [k, deepRemoveKeys(v, exclude)]), - ) - } - return obj -} - const jsonSchema = zodToJsonSchema(formDataSchema, { name: 'formData', nameStrategy: 'title' }) const orderedJsonSchema = reorderSchema(jsonSchema) -const filteredJsonSchema = deepRemoveKeys(orderedJsonSchema, { additionalProperties: false }) const distDir = join(__dirname, '../dist') -writeFileSync(join(distDir, 'formData_schema.json'), JSON.stringify(filteredJsonSchema, null, 2)) +writeFileSync(join(distDir, 'formData_schema.json'), JSON.stringify(orderedJsonSchema, null, 2))