@form-engine-ts/zod 1.0.0 → 1.1.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 +6 -0
- package/dist/index.cjs +6 -3
- package/dist/index.d.cts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +11 -4
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -23,3 +23,9 @@ 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
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -28,12 +28,15 @@ var import_zod = require("zod");
|
|
|
28
28
|
function cloneSchema(schema) {
|
|
29
29
|
return JSON.parse(JSON.stringify(schema));
|
|
30
30
|
}
|
|
31
|
-
function createZodFormSchema(schema) {
|
|
31
|
+
function createZodFormSchema(schema, options = {}) {
|
|
32
32
|
(0, import_core.assertValidFormSchema)(schema);
|
|
33
33
|
const stableSchema = cloneSchema(schema);
|
|
34
|
-
const
|
|
34
|
+
const page = options.pageIndex === void 0 || stableSchema.pages === void 0 ? void 0 : stableSchema.pages[options.pageIndex];
|
|
35
|
+
const pageIds = page === void 0 ? void 0 : new Set(page.questionIds);
|
|
36
|
+
const fields = options.pageIndex === void 0 || stableSchema.pages === void 0 ? stableSchema.fields : stableSchema.fields.filter((field) => pageIds?.has(field.id) === true);
|
|
37
|
+
const shape = Object.fromEntries(fields.map((field) => [field.id, import_zod.z.unknown().optional()]));
|
|
35
38
|
return import_zod.z.object(shape).passthrough().superRefine((values, context) => {
|
|
36
|
-
const result = (0, import_core.validateAnswers)(stableSchema, values);
|
|
39
|
+
const result = options.pageIndex === void 0 ? (0, import_core.validateAnswers)(stableSchema, values) : (0, import_core.validatePageAnswers)(stableSchema, options.pageIndex, values);
|
|
37
40
|
if (result.valid) return;
|
|
38
41
|
for (const issue of result.issues) {
|
|
39
42
|
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,22 @@
|
|
|
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";
|
|
4
8
|
function cloneSchema(schema) {
|
|
5
9
|
return JSON.parse(JSON.stringify(schema));
|
|
6
10
|
}
|
|
7
|
-
function createZodFormSchema(schema) {
|
|
11
|
+
function createZodFormSchema(schema, options = {}) {
|
|
8
12
|
assertValidFormSchema(schema);
|
|
9
13
|
const stableSchema = cloneSchema(schema);
|
|
10
|
-
const
|
|
14
|
+
const page = options.pageIndex === void 0 || stableSchema.pages === void 0 ? void 0 : stableSchema.pages[options.pageIndex];
|
|
15
|
+
const pageIds = page === void 0 ? void 0 : new Set(page.questionIds);
|
|
16
|
+
const fields = options.pageIndex === void 0 || stableSchema.pages === void 0 ? stableSchema.fields : stableSchema.fields.filter((field) => pageIds?.has(field.id) === true);
|
|
17
|
+
const shape = Object.fromEntries(fields.map((field) => [field.id, z.unknown().optional()]));
|
|
11
18
|
return z.object(shape).passthrough().superRefine((values, context) => {
|
|
12
|
-
const result = validateAnswers(stableSchema, values);
|
|
19
|
+
const result = options.pageIndex === void 0 ? validateAnswers(stableSchema, values) : validatePageAnswers(stableSchema, options.pageIndex, values);
|
|
13
20
|
if (result.valid) return;
|
|
14
21
|
for (const issue of result.issues) {
|
|
15
22
|
context.addIssue({
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@form-engine-ts/zod",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
42
|
"zod": "^3.23.0",
|
|
43
|
-
"@form-engine-ts/core": "1.
|
|
43
|
+
"@form-engine-ts/core": "1.1.0"
|
|
44
44
|
},
|
|
45
45
|
"scripts": {
|
|
46
46
|
"build": "tsup src/index.ts --format esm,cjs --dts --clean --external @form-engine-ts/core --external zod",
|