Skip to content

Commit 2758de7

Browse files
committed
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
1 parent bae812b commit 2758de7

File tree

4 files changed

+27
-36
lines changed

4 files changed

+27
-36
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"build:lib": "vite build --config vite.config.lib.mjs",
6868
"build": "npm-run-all -p build:icons build:demo",
6969
"prebuild": "npm run build:lib",
70+
"postbuild": "npm run generate:jsonSchema",
7071
"build:demo": "vite build --mode demo",
7172
"build:demo:watch": "vite build --mode demo --watch",
7273
"build:icons": "node ./tools/generate-sprite",

src/lib/js/components/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import RowsData from './rows/index.js'
77
import ColumnsData from './columns/index.js'
88
import FieldsData from './fields/index.js'
99
import ExternalsData from './externals.js'
10-
import { SESSION_FORMDATA_KEY } from '../constants.js'
10+
import { SESSION_FORMDATA_KEY, version } from '../constants.js'
1111

1212
export const Stages = StagesData
1313
export const Rows = RowsData
@@ -78,7 +78,10 @@ export class Components extends Data {
7878
}
7979

8080
get json() {
81-
return window.JSON.stringify(this.formData)
81+
return window.JSON.stringify({
82+
$schema: `https://cdn.jsdelivr.net/npm/formeo@${version}/dist/formData_schema.json`,
83+
...this.formData,
84+
})
8285
}
8386

8487
get formData() {

src/lib/js/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import pkg from '../../../package.json' with { type: 'json' }
33
const isProd = import.meta.env?.PROD
44

55
const name = pkg.name
6-
const version = pkg.version
6+
export const version = pkg.version
77
export const PACKAGE_NAME = name
88
export const formeoSpriteId = 'formeo-sprite'
99

tools/generate-json-schema.ts

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,28 @@ const formDataSchema = z
8282
id: z.string().optional(),
8383
})
8484
.optional(),
85-
content: z.string().optional(),
85+
content: z.any().optional(),
8686
action: z.object({}).optional(),
8787
options: z
8888
.array(
89-
z.object({
90-
label: z.string(),
91-
value: z.string().optional(),
92-
selected: z.boolean().optional(),
93-
checked: z.boolean().optional(),
94-
type: z
95-
.array(
96-
z.object({
97-
type: z.string(),
98-
label: z.string(),
99-
// value: z.string().optional(),
100-
selected: z.boolean().optional(),
101-
}),
102-
)
103-
.optional(),
104-
}),
89+
z
90+
.object({
91+
label: z.string(),
92+
value: z.string().optional(),
93+
selected: z.boolean().optional(),
94+
checked: z.boolean().optional(),
95+
type: z
96+
.array(
97+
z.object({
98+
type: z.string(),
99+
label: z.string(),
100+
// value: z.string().optional(),
101+
selected: z.boolean().optional(),
102+
}),
103+
)
104+
.optional(),
105+
})
106+
.catchall(z.any()),
105107
)
106108
.optional(),
107109
conditions: z
@@ -155,22 +157,7 @@ const reorderSchema = (schema: object) => {
155157
}
156158
}
157159

158-
function deepRemoveKeys(obj, exclude) {
159-
if (Array.isArray(obj)) {
160-
return obj.map(i => deepRemoveKeys(i, exclude))
161-
}
162-
if (typeof obj === 'object') {
163-
return Object.fromEntries(
164-
Object.entries(obj)
165-
.filter(([k, v]) => !(k in exclude && (exclude[k] === undefined || exclude[k] === v)))
166-
.map(([k, v]) => [k, deepRemoveKeys(v, exclude)]),
167-
)
168-
}
169-
return obj
170-
}
171-
172160
const jsonSchema = zodToJsonSchema(formDataSchema, { name: 'formData', nameStrategy: 'title' })
173161
const orderedJsonSchema = reorderSchema(jsonSchema)
174-
const filteredJsonSchema = deepRemoveKeys(orderedJsonSchema, { additionalProperties: false })
175162
const distDir = join(__dirname, '../dist')
176-
writeFileSync(join(distDir, 'formData_schema.json'), JSON.stringify(filteredJsonSchema, null, 2))
163+
writeFileSync(join(distDir, 'formData_schema.json'), JSON.stringify(orderedJsonSchema, null, 2))

0 commit comments

Comments
 (0)