@form-engine-ts/zod 1.1.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -2
- package/dist/index.cjs +24 -2
- package/dist/index.js +24 -2
- package/package.json +8 -3
package/README.md
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
# @form-engine-ts/zod
|
|
2
2
|
|
|
3
|
-
Generates a Zod
|
|
3
|
+
Generates a Zod 4 answer validator from a form-engine-ts `FormSchema` while preserving Core-compatible validation issues.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
7
7
|
```bash
|
|
8
|
-
pnpm add @form-engine-ts/core @form-engine-ts/zod zod@^
|
|
8
|
+
pnpm add @form-engine-ts/core @form-engine-ts/zod zod@^4
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
## Quick start
|
|
@@ -29,3 +29,6 @@ Generate a validator for one wizard page while preserving passthrough values fro
|
|
|
29
29
|
```ts
|
|
30
30
|
const firstPageResult = createZodFormSchema(schema, { pageIndex: 0 }).safeParse(values);
|
|
31
31
|
```
|
|
32
|
+
|
|
33
|
+
Zod is a `^4.0.0` peer dependency. Schema metadata is checked by Core before validator creation, and parsed answer
|
|
34
|
+
objects retain passthrough values. Required, choice, visibility, and page-scoped issue codes match Core validation.
|
package/dist/index.cjs
CHANGED
|
@@ -25,6 +25,16 @@ __export(index_exports, {
|
|
|
25
25
|
module.exports = __toCommonJS(index_exports);
|
|
26
26
|
var import_core = require("@form-engine-ts/core");
|
|
27
27
|
var import_zod = require("zod");
|
|
28
|
+
var jsonValueSchema = import_zod.z.lazy(
|
|
29
|
+
() => import_zod.z.union([
|
|
30
|
+
import_zod.z.string(),
|
|
31
|
+
import_zod.z.number().finite(),
|
|
32
|
+
import_zod.z.boolean(),
|
|
33
|
+
import_zod.z.null(),
|
|
34
|
+
import_zod.z.array(jsonValueSchema),
|
|
35
|
+
import_zod.z.record(import_zod.z.string(), jsonValueSchema)
|
|
36
|
+
])
|
|
37
|
+
);
|
|
28
38
|
function cloneSchema(schema) {
|
|
29
39
|
return JSON.parse(JSON.stringify(schema));
|
|
30
40
|
}
|
|
@@ -34,9 +44,21 @@ function createZodFormSchema(schema, options = {}) {
|
|
|
34
44
|
const page = options.pageIndex === void 0 || stableSchema.pages === void 0 ? void 0 : stableSchema.pages[options.pageIndex];
|
|
35
45
|
const pageIds = page === void 0 ? void 0 : new Set(page.questionIds);
|
|
36
46
|
const fields = options.pageIndex === void 0 || stableSchema.pages === void 0 ? stableSchema.fields : stableSchema.fields.filter((field) => pageIds?.has(field.id) === true);
|
|
37
|
-
const
|
|
47
|
+
const allFieldIds = new Set(stableSchema.fields.map((field) => field.id));
|
|
48
|
+
const acceptsMetadata = !allFieldIds.has("metadata");
|
|
49
|
+
const acceptsTranslationMetadata = !allFieldIds.has("translationMetadata");
|
|
50
|
+
const shape = {
|
|
51
|
+
...Object.fromEntries(fields.map((field) => [field.id, import_zod.z.unknown().optional()])),
|
|
52
|
+
...acceptsMetadata ? { metadata: import_zod.z.record(import_zod.z.string(), jsonValueSchema).optional() } : {},
|
|
53
|
+
...acceptsTranslationMetadata ? { translationMetadata: import_zod.z.record(import_zod.z.string(), jsonValueSchema).optional() } : {}
|
|
54
|
+
};
|
|
38
55
|
return import_zod.z.object(shape).passthrough().superRefine((values, context) => {
|
|
39
|
-
const
|
|
56
|
+
const answerValues = Object.fromEntries(
|
|
57
|
+
Object.entries(values).filter(
|
|
58
|
+
([key]) => !(acceptsMetadata && key === "metadata") && !(acceptsTranslationMetadata && key === "translationMetadata")
|
|
59
|
+
)
|
|
60
|
+
);
|
|
61
|
+
const result = options.pageIndex === void 0 ? (0, import_core.validateAnswers)(stableSchema, answerValues) : (0, import_core.validatePageAnswers)(stableSchema, options.pageIndex, answerValues);
|
|
40
62
|
if (result.valid) return;
|
|
41
63
|
for (const issue of result.issues) {
|
|
42
64
|
context.addIssue({
|
package/dist/index.js
CHANGED
|
@@ -5,6 +5,16 @@ import {
|
|
|
5
5
|
validatePageAnswers
|
|
6
6
|
} from "@form-engine-ts/core";
|
|
7
7
|
import { z } from "zod";
|
|
8
|
+
var jsonValueSchema = z.lazy(
|
|
9
|
+
() => z.union([
|
|
10
|
+
z.string(),
|
|
11
|
+
z.number().finite(),
|
|
12
|
+
z.boolean(),
|
|
13
|
+
z.null(),
|
|
14
|
+
z.array(jsonValueSchema),
|
|
15
|
+
z.record(z.string(), jsonValueSchema)
|
|
16
|
+
])
|
|
17
|
+
);
|
|
8
18
|
function cloneSchema(schema) {
|
|
9
19
|
return JSON.parse(JSON.stringify(schema));
|
|
10
20
|
}
|
|
@@ -14,9 +24,21 @@ function createZodFormSchema(schema, options = {}) {
|
|
|
14
24
|
const page = options.pageIndex === void 0 || stableSchema.pages === void 0 ? void 0 : stableSchema.pages[options.pageIndex];
|
|
15
25
|
const pageIds = page === void 0 ? void 0 : new Set(page.questionIds);
|
|
16
26
|
const fields = options.pageIndex === void 0 || stableSchema.pages === void 0 ? stableSchema.fields : stableSchema.fields.filter((field) => pageIds?.has(field.id) === true);
|
|
17
|
-
const
|
|
27
|
+
const allFieldIds = new Set(stableSchema.fields.map((field) => field.id));
|
|
28
|
+
const acceptsMetadata = !allFieldIds.has("metadata");
|
|
29
|
+
const acceptsTranslationMetadata = !allFieldIds.has("translationMetadata");
|
|
30
|
+
const shape = {
|
|
31
|
+
...Object.fromEntries(fields.map((field) => [field.id, z.unknown().optional()])),
|
|
32
|
+
...acceptsMetadata ? { metadata: z.record(z.string(), jsonValueSchema).optional() } : {},
|
|
33
|
+
...acceptsTranslationMetadata ? { translationMetadata: z.record(z.string(), jsonValueSchema).optional() } : {}
|
|
34
|
+
};
|
|
18
35
|
return z.object(shape).passthrough().superRefine((values, context) => {
|
|
19
|
-
const
|
|
36
|
+
const answerValues = Object.fromEntries(
|
|
37
|
+
Object.entries(values).filter(
|
|
38
|
+
([key]) => !(acceptsMetadata && key === "metadata") && !(acceptsTranslationMetadata && key === "translationMetadata")
|
|
39
|
+
)
|
|
40
|
+
);
|
|
41
|
+
const result = options.pageIndex === void 0 ? validateAnswers(stableSchema, answerValues) : validatePageAnswers(stableSchema, options.pageIndex, answerValues);
|
|
20
42
|
if (result.valid) return;
|
|
21
43
|
for (const issue of result.issues) {
|
|
22
44
|
context.addIssue({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@form-engine-ts/zod",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -39,8 +39,13 @@
|
|
|
39
39
|
"typescript"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"
|
|
43
|
-
|
|
42
|
+
"@form-engine-ts/core": "2.0.0"
|
|
43
|
+
},
|
|
44
|
+
"peerDependencies": {
|
|
45
|
+
"zod": "^4.0.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"zod": "^4.0.0"
|
|
44
49
|
},
|
|
45
50
|
"scripts": {
|
|
46
51
|
"build": "tsup src/index.ts --format esm,cjs --dts --clean --external @form-engine-ts/core --external zod",
|