Skip to content

Commit 420761c

Browse files
authored
Merge pull request #34 from esamattis/esamattis/fix-validation-narrowing-33
2 parents 0b4355a + 9b9e116 commit 420761c

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

packages/react-zorm/__tests__/use-zorm.test.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -878,3 +878,29 @@ test("can bound to lazily created form", () => {
878878

879879
expect(spy).toHaveBeenCalledTimes(1);
880880
});
881+
882+
test.skip("[TYPE ONLY] can narrow validation type to success", () => {
883+
const Schema = z.object({
884+
thing: z.string(),
885+
});
886+
887+
function Test() {
888+
const zo = useZorm("form", Schema);
889+
890+
const customValidation = zo.validate();
891+
892+
if (customValidation.success) {
893+
customValidation.data.thing;
894+
895+
// @ts-expect-error
896+
customValidation.data.bad;
897+
}
898+
899+
if (zo.validation?.success) {
900+
zo.validation.data.thing;
901+
902+
// @ts-expect-error
903+
zo.validation.data.bad;
904+
}
905+
}
906+
});

packages/react-zorm/src/types.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ZodCustomIssue, ZodIssue, ZodType } from "zod";
1+
import { SafeParseReturnType, ZodCustomIssue, ZodIssue, ZodType } from "zod";
22

33
type Primitive = string | number | boolean | bigint | symbol | undefined | null;
44

@@ -96,8 +96,9 @@ export type ErrorChainFromSchema<T extends GenericSchema> = ErrorChain<
9696
DeepNonNullable<ReturnType<T["parse"]>>
9797
>;
9898

99-
export type SafeParseResult<Schema extends GenericSchema> = ReturnType<
100-
ZodType<Schema>["safeParse"]
99+
export type SafeParseResult<Schema extends GenericSchema> = SafeParseReturnType<
100+
any,
101+
ReturnType<Schema["parse"]>
101102
>;
102103

103104
export interface Zorm<Schema extends GenericSchema> {

0 commit comments

Comments
 (0)