avancement planning
This commit is contained in:
+48
-2
@@ -162,13 +162,13 @@ test("catchall overrides strict", () => {
|
||||
});
|
||||
});
|
||||
|
||||
test("optional keys are unset", async () => {
|
||||
test("optional keys are unset", () => {
|
||||
const SNamedEntity = z.object({
|
||||
id: z.string(),
|
||||
set: z.string().optional(),
|
||||
unset: z.string().optional(),
|
||||
});
|
||||
const result = await SNamedEntity.parse({
|
||||
const result = SNamedEntity.parse({
|
||||
id: "asdf",
|
||||
set: undefined,
|
||||
});
|
||||
@@ -261,6 +261,21 @@ test("inferred enum type", async () => {
|
||||
expectTypeOf<Enum>().toEqualTypeOf<"a" | "b">();
|
||||
});
|
||||
|
||||
test("z.keyof returns enum", () => {
|
||||
const User = z.object({ name: z.string(), age: z.number() });
|
||||
const keysSchema = z.keyof(User);
|
||||
expect(keysSchema.enum).toEqual({
|
||||
name: "name",
|
||||
age: "age",
|
||||
});
|
||||
expect(keysSchema._zod.def.entries).toEqual({
|
||||
name: "name",
|
||||
age: "age",
|
||||
});
|
||||
type Keys = z.infer<typeof keysSchema>;
|
||||
expectTypeOf<Keys>().toEqualTypeOf<"name" | "age">();
|
||||
});
|
||||
|
||||
test("inferred partial object type with optional properties", async () => {
|
||||
const Partial = z.object({ a: z.string(), b: z.string().optional() }).partial();
|
||||
type Partial = z.infer<typeof Partial>;
|
||||
@@ -420,6 +435,17 @@ test("extend() should have power to override existing key", () => {
|
||||
expectTypeOf<PersonWithNumberAsLastName>().toEqualTypeOf<{ firstName: string; lastName: number }>();
|
||||
});
|
||||
|
||||
test("safeExtend() maintains refinements", () => {
|
||||
const schema = z.object({ name: z.string().min(1) });
|
||||
const extended = schema.safeExtend({ name: z.string().min(2) });
|
||||
expect(() => extended.parse({ name: "" })).toThrow();
|
||||
expect(extended.parse({ name: "ab" })).toEqual({ name: "ab" });
|
||||
type Extended = z.infer<typeof extended>;
|
||||
expectTypeOf<Extended>().toEqualTypeOf<{ name: string }>();
|
||||
// @ts-expect-error
|
||||
schema.safeExtend({ name: z.number() });
|
||||
});
|
||||
|
||||
test("passthrough index signature", () => {
|
||||
const a = z.object({ a: z.string() });
|
||||
type a = z.infer<typeof a>;
|
||||
@@ -561,3 +587,23 @@ test("index signature in shape", () => {
|
||||
|
||||
expectTypeOf<schema>().toEqualTypeOf<Record<string, string>>();
|
||||
});
|
||||
|
||||
test("extent() on object with refinements should throw", () => {
|
||||
const schema = z
|
||||
.object({
|
||||
a: z.string(),
|
||||
})
|
||||
.refine(() => true);
|
||||
|
||||
expect(() => schema.extend({ b: z.string() })).toThrow();
|
||||
});
|
||||
|
||||
test("safeExtend() on object with refinements should not throw", () => {
|
||||
const schema = z
|
||||
.object({
|
||||
a: z.string(),
|
||||
})
|
||||
.refine(() => true);
|
||||
|
||||
expect(() => schema.safeExtend({ b: z.string() })).not.toThrow();
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user