@form-engine-ts/zod 1.0.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 +11 -2
- package/dist/index.cjs +28 -3
- package/dist/index.d.cts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +33 -4
- 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
|
|
@@ -23,3 +23,12 @@ const schema: FormSchema = {
|
|
|
23
23
|
|
|
24
24
|
const result = createZodFormSchema(schema).safeParse({ name: "Ada" });
|
|
25
25
|
```
|
|
26
|
+
|
|
27
|
+
Generate a validator for one wizard page while preserving passthrough values from other pages:
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
const firstPageResult = createZodFormSchema(schema, { pageIndex: 0 }).safeParse(values);
|
|
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,15 +25,40 @@ __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
|
}
|
|
31
|
-
function createZodFormSchema(schema) {
|
|
41
|
+
function createZodFormSchema(schema, options = {}) {
|
|
32
42
|
(0, import_core.assertValidFormSchema)(schema);
|
|
33
43
|
const stableSchema = cloneSchema(schema);
|
|
34
|
-
const
|
|
44
|
+
const page = options.pageIndex === void 0 || stableSchema.pages === void 0 ? void 0 : stableSchema.pages[options.pageIndex];
|
|
45
|
+
const pageIds = page === void 0 ? void 0 : new Set(page.questionIds);
|
|
46
|
+
const fields = options.pageIndex === void 0 || stableSchema.pages === void 0 ? stableSchema.fields : stableSchema.fields.filter((field) => pageIds?.has(field.id) === true);
|
|
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
|
+
};
|
|
35
55
|
return import_zod.z.object(shape).passthrough().superRefine((values, context) => {
|
|
36
|
-
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);
|
|
37
62
|
if (result.valid) return;
|
|
38
63
|
for (const issue of result.issues) {
|
|
39
64
|
context.addIssue({
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { FormSchema } from '@form-engine-ts/core';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
interface CreateZodFormSchemaOptions {
|
|
5
|
+
readonly pageIndex?: number;
|
|
6
|
+
}
|
|
7
|
+
declare function createZodFormSchema(schema: FormSchema, options?: CreateZodFormSchemaOptions): z.ZodType<Record<string, unknown>>;
|
|
5
8
|
|
|
6
|
-
export { createZodFormSchema };
|
|
9
|
+
export { type CreateZodFormSchemaOptions, createZodFormSchema };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { FormSchema } from '@form-engine-ts/core';
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
interface CreateZodFormSchemaOptions {
|
|
5
|
+
readonly pageIndex?: number;
|
|
6
|
+
}
|
|
7
|
+
declare function createZodFormSchema(schema: FormSchema, options?: CreateZodFormSchemaOptions): z.ZodType<Record<string, unknown>>;
|
|
5
8
|
|
|
6
|
-
export { createZodFormSchema };
|
|
9
|
+
export { type CreateZodFormSchemaOptions, createZodFormSchema };
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,44 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
assertValidFormSchema,
|
|
4
|
+
validateAnswers,
|
|
5
|
+
validatePageAnswers
|
|
6
|
+
} from "@form-engine-ts/core";
|
|
3
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
|
+
);
|
|
4
18
|
function cloneSchema(schema) {
|
|
5
19
|
return JSON.parse(JSON.stringify(schema));
|
|
6
20
|
}
|
|
7
|
-
function createZodFormSchema(schema) {
|
|
21
|
+
function createZodFormSchema(schema, options = {}) {
|
|
8
22
|
assertValidFormSchema(schema);
|
|
9
23
|
const stableSchema = cloneSchema(schema);
|
|
10
|
-
const
|
|
24
|
+
const page = options.pageIndex === void 0 || stableSchema.pages === void 0 ? void 0 : stableSchema.pages[options.pageIndex];
|
|
25
|
+
const pageIds = page === void 0 ? void 0 : new Set(page.questionIds);
|
|
26
|
+
const fields = options.pageIndex === void 0 || stableSchema.pages === void 0 ? stableSchema.fields : stableSchema.fields.filter((field) => pageIds?.has(field.id) === true);
|
|
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
|
+
};
|
|
11
35
|
return z.object(shape).passthrough().superRefine((values, context) => {
|
|
12
|
-
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);
|
|
13
42
|
if (result.valid) return;
|
|
14
43
|
for (const issue of result.issues) {
|
|
15
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",
|