Skip to content

Commit

Permalink
feat: add to formData json
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kevinchappell committed Nov 19, 2024
1 parent b118733 commit 8561857
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 36 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 5 additions & 2 deletions src/lib/js/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/js/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
53 changes: 20 additions & 33 deletions tools/generate-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

0 comments on commit 8561857

Please sign in to comment.