@form-engine-ts/zod 2.3.0 → 2.5.1
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 +12 -0
- package/dist/index.cjs +32 -0
- package/dist/index.d.cts +7 -1
- package/dist/index.d.ts +7 -1
- package/dist/index.js +32 -0
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -32,3 +32,15 @@ const firstPageResult = createZodFormSchema(schema, { pageIndex: 0 }).safeParse(
|
|
|
32
32
|
|
|
33
33
|
Zod is a `^4.0.0` peer dependency. Schema metadata is checked by Core before validator creation, and parsed answer
|
|
34
34
|
objects retain passthrough values. Required, choice, visibility, and page-scoped issue codes match Core validation.
|
|
35
|
+
|
|
36
|
+
Use `createZodFormCodec(schema, options)` for a validation-and-normalization pipeline. It can trim strings, remove answers
|
|
37
|
+
for currently hidden fields, and strip fields that are not defined by the schema:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
const codec = createZodFormCodec(schema, {
|
|
41
|
+
trimStrings: true,
|
|
42
|
+
stripHiddenFields: true,
|
|
43
|
+
stripUnknownFields: true
|
|
44
|
+
});
|
|
45
|
+
const normalized = codec.parse(values);
|
|
46
|
+
```
|
package/dist/index.cjs
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
createZodFormCodec: () => createZodFormCodec,
|
|
23
24
|
createZodFormSchema: () => createZodFormSchema
|
|
24
25
|
});
|
|
25
26
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -74,7 +75,38 @@ function createZodFormSchema(schema, options = {}) {
|
|
|
74
75
|
}
|
|
75
76
|
});
|
|
76
77
|
}
|
|
78
|
+
function normalizeCodecValue(value) {
|
|
79
|
+
if (typeof value === "string") {
|
|
80
|
+
const trimmed = value.trim();
|
|
81
|
+
return trimmed.length === 0 ? void 0 : trimmed;
|
|
82
|
+
}
|
|
83
|
+
if (Array.isArray(value)) {
|
|
84
|
+
return value.map((item) => typeof item === "string" ? item.trim() : item).filter((item) => item !== "");
|
|
85
|
+
}
|
|
86
|
+
return value;
|
|
87
|
+
}
|
|
88
|
+
function createZodFormCodec(schema, options = {}) {
|
|
89
|
+
(0, import_core.assertValidFormSchema)(schema);
|
|
90
|
+
const stableSchema = cloneSchema(schema);
|
|
91
|
+
const fieldIds = new Set(stableSchema.fields.map((field) => field.id));
|
|
92
|
+
const recognizedExtensionKeys = /* @__PURE__ */ new Set(["metadata", "translationMetadata"]);
|
|
93
|
+
return import_zod.z.preprocess((input) => {
|
|
94
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) return input;
|
|
95
|
+
const entries = Object.entries(input);
|
|
96
|
+
const normalized = Object.fromEntries(
|
|
97
|
+
entries.filter(([key]) => options.stripUnknownFields !== true || fieldIds.has(key) || recognizedExtensionKeys.has(key)).map(([key, value]) => [key, options.trimStrings === true ? normalizeCodecValue(value) : value])
|
|
98
|
+
);
|
|
99
|
+
if (options.stripHiddenFields === true) {
|
|
100
|
+
const visibility = (0, import_core.calculateFieldVisibility)(stableSchema, normalized);
|
|
101
|
+
for (const field of stableSchema.fields) {
|
|
102
|
+
if (visibility[field.id] !== true) delete normalized[field.id];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return normalized;
|
|
106
|
+
}, createZodFormSchema(stableSchema));
|
|
107
|
+
}
|
|
77
108
|
// Annotate the CommonJS export names for ESM import in node:
|
|
78
109
|
0 && (module.exports = {
|
|
110
|
+
createZodFormCodec,
|
|
79
111
|
createZodFormSchema
|
|
80
112
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -5,5 +5,11 @@ interface CreateZodFormSchemaOptions {
|
|
|
5
5
|
readonly pageIndex?: number;
|
|
6
6
|
}
|
|
7
7
|
declare function createZodFormSchema(schema: FormSchema, options?: CreateZodFormSchemaOptions): z.ZodType<Record<string, unknown>>;
|
|
8
|
+
interface ZodTransformOptions {
|
|
9
|
+
readonly stripHiddenFields?: boolean;
|
|
10
|
+
readonly stripUnknownFields?: boolean;
|
|
11
|
+
readonly trimStrings?: boolean;
|
|
12
|
+
}
|
|
13
|
+
declare function createZodFormCodec(schema: FormSchema, options?: ZodTransformOptions): z.ZodPreprocess<z.ZodType<Record<string, unknown>, unknown, z.core.$ZodTypeInternals<Record<string, unknown>, unknown>>>;
|
|
8
14
|
|
|
9
|
-
export { type CreateZodFormSchemaOptions, createZodFormSchema };
|
|
15
|
+
export { type CreateZodFormSchemaOptions, type ZodTransformOptions, createZodFormCodec, createZodFormSchema };
|
package/dist/index.d.ts
CHANGED
|
@@ -5,5 +5,11 @@ interface CreateZodFormSchemaOptions {
|
|
|
5
5
|
readonly pageIndex?: number;
|
|
6
6
|
}
|
|
7
7
|
declare function createZodFormSchema(schema: FormSchema, options?: CreateZodFormSchemaOptions): z.ZodType<Record<string, unknown>>;
|
|
8
|
+
interface ZodTransformOptions {
|
|
9
|
+
readonly stripHiddenFields?: boolean;
|
|
10
|
+
readonly stripUnknownFields?: boolean;
|
|
11
|
+
readonly trimStrings?: boolean;
|
|
12
|
+
}
|
|
13
|
+
declare function createZodFormCodec(schema: FormSchema, options?: ZodTransformOptions): z.ZodPreprocess<z.ZodType<Record<string, unknown>, unknown, z.core.$ZodTypeInternals<Record<string, unknown>, unknown>>>;
|
|
8
14
|
|
|
9
|
-
export { type CreateZodFormSchemaOptions, createZodFormSchema };
|
|
15
|
+
export { type CreateZodFormSchemaOptions, type ZodTransformOptions, createZodFormCodec, createZodFormSchema };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import {
|
|
3
3
|
assertValidFormSchema,
|
|
4
|
+
calculateFieldVisibility,
|
|
4
5
|
validateAnswers,
|
|
5
6
|
validatePageAnswers
|
|
6
7
|
} from "@form-engine-ts/core";
|
|
@@ -54,6 +55,37 @@ function createZodFormSchema(schema, options = {}) {
|
|
|
54
55
|
}
|
|
55
56
|
});
|
|
56
57
|
}
|
|
58
|
+
function normalizeCodecValue(value) {
|
|
59
|
+
if (typeof value === "string") {
|
|
60
|
+
const trimmed = value.trim();
|
|
61
|
+
return trimmed.length === 0 ? void 0 : trimmed;
|
|
62
|
+
}
|
|
63
|
+
if (Array.isArray(value)) {
|
|
64
|
+
return value.map((item) => typeof item === "string" ? item.trim() : item).filter((item) => item !== "");
|
|
65
|
+
}
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
function createZodFormCodec(schema, options = {}) {
|
|
69
|
+
assertValidFormSchema(schema);
|
|
70
|
+
const stableSchema = cloneSchema(schema);
|
|
71
|
+
const fieldIds = new Set(stableSchema.fields.map((field) => field.id));
|
|
72
|
+
const recognizedExtensionKeys = /* @__PURE__ */ new Set(["metadata", "translationMetadata"]);
|
|
73
|
+
return z.preprocess((input) => {
|
|
74
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) return input;
|
|
75
|
+
const entries = Object.entries(input);
|
|
76
|
+
const normalized = Object.fromEntries(
|
|
77
|
+
entries.filter(([key]) => options.stripUnknownFields !== true || fieldIds.has(key) || recognizedExtensionKeys.has(key)).map(([key, value]) => [key, options.trimStrings === true ? normalizeCodecValue(value) : value])
|
|
78
|
+
);
|
|
79
|
+
if (options.stripHiddenFields === true) {
|
|
80
|
+
const visibility = calculateFieldVisibility(stableSchema, normalized);
|
|
81
|
+
for (const field of stableSchema.fields) {
|
|
82
|
+
if (visibility[field.id] !== true) delete normalized[field.id];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return normalized;
|
|
86
|
+
}, createZodFormSchema(stableSchema));
|
|
87
|
+
}
|
|
57
88
|
export {
|
|
89
|
+
createZodFormCodec,
|
|
58
90
|
createZodFormSchema
|
|
59
91
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@form-engine-ts/zod",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.5.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"typescript"
|
|
40
40
|
],
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@form-engine-ts/core": "2.
|
|
42
|
+
"@form-engine-ts/core": "2.5.1"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
45
|
"zod": "^4.0.0"
|