zod 4.6.4 → 4.6.5
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/v4/classic/schemas.ts +2 -1
- package/src/v4/classic/tests/properties.test.ts +19 -0
- package/src/v4/core/versions.ts +1 -1
- package/v4/classic/schemas.d.cts +3 -1
- package/v4/classic/schemas.d.ts +3 -1
- package/v4/core/versions.cjs +1 -1
- package/v4/core/versions.js +1 -1
package/package.json
CHANGED
|
@@ -2841,7 +2841,8 @@ type ZodInstanceOfParams = core.Params<
|
|
|
2841
2841
|
|
|
2842
2842
|
// ZodInstanceOf
|
|
2843
2843
|
export interface ZodInstanceOf<T = unknown> extends ZodCustom<T, T> {
|
|
2844
|
-
|
|
2844
|
+
// the shape is keyed off the instance type, so keys autocomplete and a schema that can't accept the property's type is an error. Methods are excluded: every object literal inherits Object.prototype.toString, which would otherwise collide with the constraint's own toString entry and reject every shape.
|
|
2845
|
+
properties<Shape extends { [k in keyof T as T[k] extends Function ? never : k]?: core.$ZodType<unknown, T[k]> }>(
|
|
2845
2846
|
shape: Shape,
|
|
2846
2847
|
params?: string | core.$ZodCheckPropertiesParams
|
|
2847
2848
|
): ZodInstanceOf<T & core.$InferObjectInput<Shape, {}>>;
|
|
@@ -51,6 +51,25 @@ test("z.instanceof().properties()", () => {
|
|
|
51
51
|
expectTypeOf<z.infer<typeof W>>().toEqualTypeOf<URL & { search: string }>();
|
|
52
52
|
});
|
|
53
53
|
|
|
54
|
+
test("z.instanceof().properties() constrains the shape to the instance type", () => {
|
|
55
|
+
const file = z.instanceof(File).properties({ name: z.string(), size: z.number().max(1024) });
|
|
56
|
+
expectTypeOf<z.infer<typeof file>>().toEqualTypeOf<File & { name: string; size: number }>();
|
|
57
|
+
|
|
58
|
+
// @ts-expect-error no such property on File
|
|
59
|
+
z.instanceof(File).properties({ nmae: z.string() });
|
|
60
|
+
|
|
61
|
+
// @ts-expect-error File["size"] is a number
|
|
62
|
+
z.instanceof(File).properties({ size: z.string() });
|
|
63
|
+
|
|
64
|
+
// methods are out of the shape, so an object literal's inherited toString can't collide with the constraint
|
|
65
|
+
// @ts-expect-error
|
|
66
|
+
z.instanceof(File).properties({ text: z.any() });
|
|
67
|
+
|
|
68
|
+
// a literal still narrows a wider property
|
|
69
|
+
const png = z.instanceof(File).properties({ type: z.literal("image/png") });
|
|
70
|
+
expectTypeOf<z.infer<typeof png>>().toEqualTypeOf<File & { type: "image/png" }>();
|
|
71
|
+
});
|
|
72
|
+
|
|
54
73
|
test("z.properties spreads into .check()", () => {
|
|
55
74
|
// the 4.5 array call sites: the check yields itself from Symbol.iterator
|
|
56
75
|
const p = z.properties({ a: z.literal("x") });
|
package/src/v4/core/versions.ts
CHANGED
package/v4/classic/schemas.d.cts
CHANGED
|
@@ -788,7 +788,9 @@ export declare const describe: typeof core.describe;
|
|
|
788
788
|
export declare const meta: typeof core.meta;
|
|
789
789
|
type ZodInstanceOfParams = core.Params<ZodCustom, core.$ZodIssueCustom, "type" | "check" | "checks" | "fn" | "abort" | "error" | "params" | "path">;
|
|
790
790
|
export interface ZodInstanceOf<T = unknown> extends ZodCustom<T, T> {
|
|
791
|
-
properties<Shape extends
|
|
791
|
+
properties<Shape extends {
|
|
792
|
+
[k in keyof T as T[k] extends Function ? never : k]?: core.$ZodType<unknown, T[k]>;
|
|
793
|
+
}>(shape: Shape, params?: string | core.$ZodCheckPropertiesParams): ZodInstanceOf<T & core.$InferObjectInput<Shape, {}>>;
|
|
792
794
|
}
|
|
793
795
|
export declare const ZodInstanceOf: core.$constructor<ZodInstanceOf>;
|
|
794
796
|
declare function _instanceof<T extends typeof util.Class>(cls: T, params?: ZodInstanceOfParams): ZodInstanceOf<InstanceType<T>>;
|
package/v4/classic/schemas.d.ts
CHANGED
|
@@ -788,7 +788,9 @@ export declare const describe: typeof core.describe;
|
|
|
788
788
|
export declare const meta: typeof core.meta;
|
|
789
789
|
type ZodInstanceOfParams = core.Params<ZodCustom, core.$ZodIssueCustom, "type" | "check" | "checks" | "fn" | "abort" | "error" | "params" | "path">;
|
|
790
790
|
export interface ZodInstanceOf<T = unknown> extends ZodCustom<T, T> {
|
|
791
|
-
properties<Shape extends
|
|
791
|
+
properties<Shape extends {
|
|
792
|
+
[k in keyof T as T[k] extends Function ? never : k]?: core.$ZodType<unknown, T[k]>;
|
|
793
|
+
}>(shape: Shape, params?: string | core.$ZodCheckPropertiesParams): ZodInstanceOf<T & core.$InferObjectInput<Shape, {}>>;
|
|
792
794
|
}
|
|
793
795
|
export declare const ZodInstanceOf: core.$constructor<ZodInstanceOf>;
|
|
794
796
|
declare function _instanceof<T extends typeof util.Class>(cls: T, params?: ZodInstanceOfParams): ZodInstanceOf<InstanceType<T>>;
|
package/v4/core/versions.cjs
CHANGED
package/v4/core/versions.js
CHANGED