Capítulo 24 de 61
z.instanceof(Class) validates that a value is an instance of a given class (built-in or user-defined), and z.property()/z.properties() let you further check specific properties on that instance.
z.instanceof(Class): passes instanceof Class checks; works for user classes and built-ins alike (RegExp, URL, Error, etc.).z.property(name, schema): used inside .check() to validate one property of the value (most useful paired with z.instanceof(), but works on any type).z.properties({ ... }): declares several property checks at once from an object literal; spread the result into .check().class Test { name: string; }
const TestSchema = z.instanceof(Test);
TestSchema.parse(new Test()); // ok
TestSchema.parse("whatever"); // throws
z.instanceof(URL);
z.instanceof(Error);
const httpsUrl = z.instanceof(URL).check(
...z.properties({
protocol: z.literal("https:" as string),
hostname: z.string().regex(z.regexes.domain),
})
);
httpsUrl.parse(new URL("https://example.com")); // ok
httpsUrl.parse(new URL("http://localhost")); // throws (wrong protocol)
z.instanceof() is the way to validate third-party or built-in class instances that don't have a native Zod schema.z.property()/z.properties() let you assert on specific fields of an instance without converting it to a plain object first..check() just like any other refinement-style check.z.property() is implemented as a check, the same mechanism .refine() uses.instanceof or property checks at all.