-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #727 from DenisFrezzato/improve-refinement-type
Improve refine with type guards
- Loading branch information
Showing
4 changed files
with
106 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
import { expect } from "https://deno.land/x/[email protected]/mod.ts"; | ||
const test = Deno.test; | ||
|
||
import { util } from "../helpers/util.ts"; | ||
import * as z from "../index.ts"; | ||
import { ZodIssueCode } from "../ZodError.ts"; | ||
|
||
|
@@ -49,6 +50,38 @@ test("refinement 2", () => { | |
).toThrow(); | ||
}); | ||
|
||
test("refinement type guard", () => { | ||
const validationSchema = z.object({ | ||
a: z.string().refine((s): s is "a" => s === "a"), | ||
}); | ||
type Schema = z.infer<typeof validationSchema>; | ||
|
||
const f1: util.AssertEqual<"a", Schema["a"]> = true; | ||
f1; | ||
const f2: util.AssertEqual<"string", Schema["a"]> = false; | ||
f2; | ||
}); | ||
|
||
test("refinement Promise", async () => { | ||
const validationSchema = z | ||
.object({ | ||
email: z.string().email(), | ||
password: z.string(), | ||
confirmPassword: z.string(), | ||
}) | ||
.refine( | ||
(data) => | ||
Promise.resolve().then(() => data.password === data.confirmPassword), | ||
"Both password and confirmation must match" | ||
); | ||
|
||
await validationSchema.parseAsync({ | ||
email: "[email protected]", | ||
password: "password", | ||
confirmPassword: "password", | ||
}); | ||
}); | ||
|
||
test("custom path", async () => { | ||
const result = await z | ||
.object({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// @ts-ignore TS6133 | ||
import { expect, test } from "@jest/globals"; | ||
|
||
import { util } from "../helpers/util"; | ||
import * as z from "../index"; | ||
import { ZodIssueCode } from "../ZodError"; | ||
|
||
|
@@ -48,6 +49,38 @@ test("refinement 2", () => { | |
).toThrow(); | ||
}); | ||
|
||
test("refinement type guard", () => { | ||
const validationSchema = z.object({ | ||
a: z.string().refine((s): s is "a" => s === "a"), | ||
}); | ||
type Schema = z.infer<typeof validationSchema>; | ||
|
||
const f1: util.AssertEqual<"a", Schema["a"]> = true; | ||
f1; | ||
const f2: util.AssertEqual<"string", Schema["a"]> = false; | ||
f2; | ||
}); | ||
|
||
test("refinement Promise", async () => { | ||
const validationSchema = z | ||
.object({ | ||
email: z.string().email(), | ||
password: z.string(), | ||
confirmPassword: z.string(), | ||
}) | ||
.refine( | ||
(data) => | ||
Promise.resolve().then(() => data.password === data.confirmPassword), | ||
"Both password and confirmation must match" | ||
); | ||
|
||
await validationSchema.parseAsync({ | ||
email: "[email protected]", | ||
password: "password", | ||
confirmPassword: "password", | ||
}); | ||
}); | ||
|
||
test("custom path", async () => { | ||
const result = await z | ||
.object({ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters