zod 4.1.0-canary.20250813T051310 → 4.1.0-canary.20250821T014930
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/package.json +1 -1
- package/src/v3/tests/object.test.ts +2 -2
- package/src/v4/classic/schemas.ts +1 -0
- package/src/v4/classic/tests/array.test.ts +6 -6
- package/src/v4/classic/tests/index.test.ts +15 -0
- package/src/v4/classic/tests/object.test.ts +2 -2
- package/src/v4/classic/tests/refine.test.ts +2 -2
- package/src/v4/classic/tests/set.test.ts +1 -1
- package/src/v4/core/schemas.ts +1 -3
- package/src/v4/mini/schemas.ts +3 -0
- package/src/v4/mini/tests/index.test.ts +15 -0
- package/v4/classic/schemas.cjs +1 -0
- package/v4/classic/schemas.js +1 -0
- package/v4/mini/schemas.cjs +1 -0
- package/v4/mini/schemas.d.cts +1 -0
- package/v4/mini/schemas.d.ts +1 -0
- package/v4/mini/schemas.js +1 -0
package/package.json
CHANGED
|
@@ -152,13 +152,13 @@ test("catchall overrides strict", () => {
|
|
|
152
152
|
});
|
|
153
153
|
});
|
|
154
154
|
|
|
155
|
-
test("test that optional keys are unset",
|
|
155
|
+
test("test that optional keys are unset", () => {
|
|
156
156
|
const SNamedEntity = z.object({
|
|
157
157
|
id: z.string(),
|
|
158
158
|
set: z.string().optional(),
|
|
159
159
|
unset: z.string().optional(),
|
|
160
160
|
});
|
|
161
|
-
const result =
|
|
161
|
+
const result = SNamedEntity.parse({
|
|
162
162
|
id: "asdf",
|
|
163
163
|
set: undefined,
|
|
164
164
|
});
|
|
@@ -118,6 +118,7 @@ export interface _ZodType<out Internals extends core.$ZodTypeInternals = core.$Z
|
|
|
118
118
|
export const ZodType: core.$constructor<ZodType> = /*@__PURE__*/ core.$constructor("ZodType", (inst, def) => {
|
|
119
119
|
core.$ZodType.init(inst, def);
|
|
120
120
|
inst.def = def;
|
|
121
|
+
inst.type = def.type;
|
|
121
122
|
Object.defineProperty(inst, "_def", { value: def });
|
|
122
123
|
|
|
123
124
|
// base methods
|
|
@@ -6,9 +6,9 @@ test("type inference", () => {
|
|
|
6
6
|
expectTypeOf<z.infer<typeof schema>>().toEqualTypeOf<string[]>();
|
|
7
7
|
});
|
|
8
8
|
|
|
9
|
-
test("array min/max",
|
|
9
|
+
test("array min/max", () => {
|
|
10
10
|
const schema = z.array(z.string()).min(2).max(2);
|
|
11
|
-
const r1 =
|
|
11
|
+
const r1 = schema.safeParse(["asdf"]);
|
|
12
12
|
expect(r1.success).toEqual(false);
|
|
13
13
|
expect(r1.error!.issues).toMatchInlineSnapshot(`
|
|
14
14
|
[
|
|
@@ -23,7 +23,7 @@ test("array min/max", async () => {
|
|
|
23
23
|
]
|
|
24
24
|
`);
|
|
25
25
|
|
|
26
|
-
const r2 =
|
|
26
|
+
const r2 = schema.safeParse(["asdf", "asdf", "asdf"]);
|
|
27
27
|
expect(r2.success).toEqual(false);
|
|
28
28
|
expect(r2.error!.issues).toMatchInlineSnapshot(`
|
|
29
29
|
[
|
|
@@ -39,11 +39,11 @@ test("array min/max", async () => {
|
|
|
39
39
|
`);
|
|
40
40
|
});
|
|
41
41
|
|
|
42
|
-
test("array length",
|
|
42
|
+
test("array length", () => {
|
|
43
43
|
const schema = z.array(z.string()).length(2);
|
|
44
44
|
schema.parse(["asdf", "asdf"]);
|
|
45
45
|
|
|
46
|
-
const r1 =
|
|
46
|
+
const r1 = schema.safeParse(["asdf"]);
|
|
47
47
|
expect(r1.success).toEqual(false);
|
|
48
48
|
expect(r1.error!.issues).toMatchInlineSnapshot(`
|
|
49
49
|
[
|
|
@@ -59,7 +59,7 @@ test("array length", async () => {
|
|
|
59
59
|
]
|
|
60
60
|
`);
|
|
61
61
|
|
|
62
|
-
const r2 =
|
|
62
|
+
const r2 = schema.safeParse(["asdf", "asdf", "asdf"]);
|
|
63
63
|
expect(r2.success).toEqual(false);
|
|
64
64
|
expect(r2.error!.issues).toMatchInlineSnapshot(`
|
|
65
65
|
[
|
|
@@ -827,3 +827,18 @@ test("def typing", () => {
|
|
|
827
827
|
z.string().catch("fallback").def.type satisfies "catch";
|
|
828
828
|
z.file().def.type satisfies "file";
|
|
829
829
|
});
|
|
830
|
+
|
|
831
|
+
test("runtime type property exists and returns correct values", () => {
|
|
832
|
+
const stringSchema = z.string();
|
|
833
|
+
expect(stringSchema.type).toBe("string");
|
|
834
|
+
});
|
|
835
|
+
|
|
836
|
+
test("type narrowing works with type property", () => {
|
|
837
|
+
type ArrayOrRecord = z.ZodArray<z.ZodString> | z.ZodRecord<z.ZodString, z.ZodAny>;
|
|
838
|
+
const arraySchema = z.array(z.string()) as ArrayOrRecord;
|
|
839
|
+
|
|
840
|
+
if (arraySchema.type === "array") {
|
|
841
|
+
expectTypeOf(arraySchema).toEqualTypeOf<z.ZodArray<z.ZodString>>();
|
|
842
|
+
expect(arraySchema.element).toBeDefined();
|
|
843
|
+
}
|
|
844
|
+
});
|
|
@@ -162,13 +162,13 @@ test("catchall overrides strict", () => {
|
|
|
162
162
|
});
|
|
163
163
|
});
|
|
164
164
|
|
|
165
|
-
test("optional keys are unset",
|
|
165
|
+
test("optional keys are unset", () => {
|
|
166
166
|
const SNamedEntity = z.object({
|
|
167
167
|
id: z.string(),
|
|
168
168
|
set: z.string().optional(),
|
|
169
169
|
unset: z.string().optional(),
|
|
170
170
|
});
|
|
171
|
-
const result =
|
|
171
|
+
const result = SNamedEntity.parse({
|
|
172
172
|
id: "asdf",
|
|
173
173
|
set: undefined,
|
|
174
174
|
});
|
|
@@ -167,8 +167,8 @@ describe("early termination options", () => {
|
|
|
167
167
|
});
|
|
168
168
|
|
|
169
169
|
describe("custom error paths", () => {
|
|
170
|
-
test("should use custom path in error message",
|
|
171
|
-
const result =
|
|
170
|
+
test("should use custom path in error message", () => {
|
|
171
|
+
const result = z
|
|
172
172
|
.object({ password: z.string(), confirm: z.string() })
|
|
173
173
|
.refine((data) => data.confirm === data.password, { path: ["confirm"] })
|
|
174
174
|
.safeParse({ password: "asdf", confirm: "qewr" });
|
|
@@ -39,7 +39,7 @@ test("valid parse async", async () => {
|
|
|
39
39
|
expect(result.data!.has("second")).toEqual(true);
|
|
40
40
|
expect(result.data!.has("third")).toEqual(false);
|
|
41
41
|
|
|
42
|
-
const asyncResult =
|
|
42
|
+
const asyncResult = stringSet.safeParse(new Set(["first", "second"]));
|
|
43
43
|
expect(asyncResult.success).toEqual(true);
|
|
44
44
|
expect(asyncResult.data!.has("first")).toEqual(true);
|
|
45
45
|
expect(asyncResult.data!.has("second")).toEqual(true);
|
package/src/v4/core/schemas.ts
CHANGED
|
@@ -168,9 +168,7 @@ export interface $ZodType<
|
|
|
168
168
|
"~standard": $ZodStandardSchema<this>;
|
|
169
169
|
}
|
|
170
170
|
export interface _$ZodType<T extends $ZodTypeInternals = $ZodTypeInternals>
|
|
171
|
-
extends $ZodType<T["output"], T["input"], T> {
|
|
172
|
-
// _zod: T;
|
|
173
|
-
}
|
|
171
|
+
extends $ZodType<T["output"], T["input"], T> {}
|
|
174
172
|
|
|
175
173
|
export const $ZodType: core.$constructor<$ZodType> = /*@__PURE__*/ core.$constructor("$ZodType", (inst, def) => {
|
|
176
174
|
inst ??= {} as any;
|
package/src/v4/mini/schemas.ts
CHANGED
|
@@ -9,6 +9,7 @@ export interface ZodMiniType<
|
|
|
9
9
|
out Input = unknown,
|
|
10
10
|
out Internals extends core.$ZodTypeInternals<Output, Input> = core.$ZodTypeInternals<Output, Input>,
|
|
11
11
|
> extends core.$ZodType<Output, Input, Internals> {
|
|
12
|
+
type: Internals["def"]["type"];
|
|
12
13
|
check(...checks: (core.CheckFn<core.output<this>> | core.$ZodCheck<core.output<this>>)[]): this;
|
|
13
14
|
clone(def?: Internals["def"], params?: { parent: boolean }): this;
|
|
14
15
|
register<R extends core.$ZodRegistry>(
|
|
@@ -43,7 +44,9 @@ export const ZodMiniType: core.$constructor<ZodMiniType> = /*@__PURE__*/ core.$c
|
|
|
43
44
|
if (!inst._zod) throw new Error("Uninitialized schema in ZodMiniType.");
|
|
44
45
|
|
|
45
46
|
core.$ZodType.init(inst, def);
|
|
47
|
+
|
|
46
48
|
inst.def = def;
|
|
49
|
+
inst.type = def.type;
|
|
47
50
|
inst.parse = (data, params) => parse.parse(inst, data, params, { callee: inst.parse });
|
|
48
51
|
inst.safeParse = (data, params) => parse.safeParse(inst, data, params);
|
|
49
52
|
inst.parseAsync = async (data, params) => parse.parseAsync(inst, data, params, { callee: inst.parseAsync });
|
|
@@ -882,3 +882,18 @@ test("defaulted object schema returns shallow clone", () => {
|
|
|
882
882
|
expect(result1).not.toBe(result2);
|
|
883
883
|
expect(result1).toEqual(result2);
|
|
884
884
|
});
|
|
885
|
+
|
|
886
|
+
test("runtime type property exists and returns correct values", () => {
|
|
887
|
+
const stringSchema = z.string();
|
|
888
|
+
expect(stringSchema.type).toBe("string");
|
|
889
|
+
});
|
|
890
|
+
|
|
891
|
+
test("type narrowing works with type property", () => {
|
|
892
|
+
type ArrayOrRecord = z.ZodMiniArray<z.ZodMiniString> | z.ZodMiniRecord<z.ZodMiniString<string>, z.ZodMiniAny>;
|
|
893
|
+
const arraySchema = z.array(z.string()) as ArrayOrRecord;
|
|
894
|
+
|
|
895
|
+
if (arraySchema.type === "array") {
|
|
896
|
+
expectTypeOf(arraySchema).toEqualTypeOf<z.ZodMiniArray<z.ZodMiniString<unknown>>>();
|
|
897
|
+
expect(arraySchema.def.element).toBeDefined();
|
|
898
|
+
}
|
|
899
|
+
});
|
package/v4/classic/schemas.cjs
CHANGED
|
@@ -115,6 +115,7 @@ const parse = __importStar(require("./parse.cjs"));
|
|
|
115
115
|
exports.ZodType = core.$constructor("ZodType", (inst, def) => {
|
|
116
116
|
core.$ZodType.init(inst, def);
|
|
117
117
|
inst.def = def;
|
|
118
|
+
inst.type = def.type;
|
|
118
119
|
Object.defineProperty(inst, "_def", { value: def });
|
|
119
120
|
// base methods
|
|
120
121
|
inst.check = (...checks) => {
|
package/v4/classic/schemas.js
CHANGED
|
@@ -6,6 +6,7 @@ import * as parse from "./parse.js";
|
|
|
6
6
|
export const ZodType = /*@__PURE__*/ core.$constructor("ZodType", (inst, def) => {
|
|
7
7
|
core.$ZodType.init(inst, def);
|
|
8
8
|
inst.def = def;
|
|
9
|
+
inst.type = def.type;
|
|
9
10
|
Object.defineProperty(inst, "_def", { value: def });
|
|
10
11
|
// base methods
|
|
11
12
|
inst.check = (...checks) => {
|
package/v4/mini/schemas.cjs
CHANGED
|
@@ -121,6 +121,7 @@ exports.ZodMiniType = core.$constructor("ZodMiniType", (inst, def) => {
|
|
|
121
121
|
throw new Error("Uninitialized schema in ZodMiniType.");
|
|
122
122
|
core.$ZodType.init(inst, def);
|
|
123
123
|
inst.def = def;
|
|
124
|
+
inst.type = def.type;
|
|
124
125
|
inst.parse = (data, params) => parse.parse(inst, data, params, { callee: inst.parse });
|
|
125
126
|
inst.safeParse = (data, params) => parse.safeParse(inst, data, params);
|
|
126
127
|
inst.parseAsync = async (data, params) => parse.parseAsync(inst, data, params, { callee: inst.parseAsync });
|
package/v4/mini/schemas.d.cts
CHANGED
|
@@ -2,6 +2,7 @@ import * as core from "../core/index.cjs";
|
|
|
2
2
|
import { util } from "../core/index.cjs";
|
|
3
3
|
type SomeType = core.SomeType;
|
|
4
4
|
export interface ZodMiniType<out Output = unknown, out Input = unknown, out Internals extends core.$ZodTypeInternals<Output, Input> = core.$ZodTypeInternals<Output, Input>> extends core.$ZodType<Output, Input, Internals> {
|
|
5
|
+
type: Internals["def"]["type"];
|
|
5
6
|
check(...checks: (core.CheckFn<core.output<this>> | core.$ZodCheck<core.output<this>>)[]): this;
|
|
6
7
|
clone(def?: Internals["def"], params?: {
|
|
7
8
|
parent: boolean;
|
package/v4/mini/schemas.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import * as core from "../core/index.js";
|
|
|
2
2
|
import { util } from "../core/index.js";
|
|
3
3
|
type SomeType = core.SomeType;
|
|
4
4
|
export interface ZodMiniType<out Output = unknown, out Input = unknown, out Internals extends core.$ZodTypeInternals<Output, Input> = core.$ZodTypeInternals<Output, Input>> extends core.$ZodType<Output, Input, Internals> {
|
|
5
|
+
type: Internals["def"]["type"];
|
|
5
6
|
check(...checks: (core.CheckFn<core.output<this>> | core.$ZodCheck<core.output<this>>)[]): this;
|
|
6
7
|
clone(def?: Internals["def"], params?: {
|
|
7
8
|
parent: boolean;
|
package/v4/mini/schemas.js
CHANGED
|
@@ -6,6 +6,7 @@ export const ZodMiniType = /*@__PURE__*/ core.$constructor("ZodMiniType", (inst,
|
|
|
6
6
|
throw new Error("Uninitialized schema in ZodMiniType.");
|
|
7
7
|
core.$ZodType.init(inst, def);
|
|
8
8
|
inst.def = def;
|
|
9
|
+
inst.type = def.type;
|
|
9
10
|
inst.parse = (data, params) => parse.parse(inst, data, params, { callee: inst.parse });
|
|
10
11
|
inst.safeParse = (data, params) => parse.safeParse(inst, data, params);
|
|
11
12
|
inst.parseAsync = async (data, params) => parse.parseAsync(inst, data, params, { callee: inst.parseAsync });
|