Skip to content

Commit c5f03f6

Browse files
committed
refactor: add isZodType
1 parent 67c88a3 commit c5f03f6

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

src/__tests__/zod4.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { describe, expect, expectTypeOf, it, test } from 'vitest';
33
import { z as z3 } from 'zod/v3';
44
import { z as z4 } from 'zod/v4';
55

6-
import { isZodTypeLike, isZodV3Type } from '../zod4.js';
6+
import { isZodType, isZodTypeLike } from '../zod4.js';
77

88
import type {
99
ZodErrorLike,
@@ -71,28 +71,28 @@ describe('isZodTypeLike', () => {
7171
});
7272
});
7373

74-
describe('isZodV3Type', () => {
74+
describe('isZodType', () => {
7575
// since we use require here, the prototype chain in the cjs module will be different than the esm build
7676
const { z: z3_2 } = require('zod/v3') as typeof import('zod/v3');
7777

7878
it('should return true for an instance of ZodNumber', () => {
79-
expect(isZodV3Type(z3.number())).toBe(true);
79+
expect(isZodType(z3.number(), { version: 3 })).toBe(true);
8080
});
8181
it('should return true for an instance of ZodObject', () => {
82-
expect(isZodV3Type(z3.object({}))).toBe(true);
82+
expect(isZodType(z3.object({}), { version: 3 })).toBe(true);
8383
});
8484
it('should return false for null', () => {
85-
expect(isZodV3Type(null)).toBe(false);
85+
expect(isZodType(null, { version: 3 })).toBe(false);
8686
});
8787
it('should return false for any empty object', () => {
88-
expect(isZodV3Type({})).toBe(false);
88+
expect(isZodType({}, { version: 3 })).toBe(false);
8989
});
9090
it('should return false for an object with a null prototype', () => {
91-
expect(isZodV3Type(Object.create(null))).toBe(false);
91+
expect(isZodType(Object.create(null), { version: 3 })).toBe(false);
9292
});
9393
it('should return true for a ZodObject created in a different context', () => {
9494
const input = z3_2.object({});
9595
expect(input).not.toBeInstanceOf(z3.ZodType);
96-
expect(isZodV3Type(input)).toBe(true);
96+
expect(isZodType(input, { version: 3 })).toBe(true);
9797
});
9898
});

src/zod4.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,34 @@ import type { z as z3 } from 'zod/v3';
22

33
import { isObject, isObjectLike, isPlainObject } from './object.js';
44

5-
export type ZodIssueLike = {
5+
type ZodIssueLike = {
66
[key: string]: any;
77
readonly code: string;
88
readonly message: string;
99
readonly path: PropertyKey[];
1010
};
1111

12-
export type ZodErrorLike = {
12+
type ZodErrorLike = {
1313
cause?: unknown;
1414
issues: ZodIssueLike[];
1515
name: string;
1616
};
1717

18-
export type ZodSafeParseSuccessLike<TOutput> = {
18+
type ZodSafeParseSuccessLike<TOutput> = {
1919
data: TOutput;
2020
error?: never;
2121
success: true;
2222
};
2323

24-
export type ZodSafeParseErrorLike = {
24+
type ZodSafeParseErrorLike = {
2525
data?: never;
2626
error: ZodErrorLike;
2727
success: false;
2828
};
2929

30-
export type ZodSafeParseResultLike<T> = ZodSafeParseErrorLike | ZodSafeParseSuccessLike<T>;
30+
type ZodSafeParseResultLike<T> = ZodSafeParseErrorLike | ZodSafeParseSuccessLike<T>;
3131

32-
export type ZodTypeLike<TOutput, TInput = unknown> = {
32+
type ZodTypeLike<TOutput, TInput = unknown> = {
3333
readonly _input: TInput;
3434
readonly _output: TOutput;
3535
safeParseAsync: (data: unknown) => Promise<ZodSafeParseResultLike<TOutput>>;
@@ -39,18 +39,32 @@ export type ZodTypeLike<TOutput, TInput = unknown> = {
3939
};
4040
};
4141

42-
export function isZodTypeLike(arg: unknown): arg is ZodTypeLike<unknown> {
42+
function isZodTypeLike(arg: unknown): arg is ZodTypeLike<unknown> {
4343
if (!isObjectLike(arg)) {
4444
return false;
4545
}
4646
const standardSchema: unknown = Reflect.get(arg, '~standard');
4747
return isPlainObject(standardSchema) && standardSchema.vendor === 'zod';
4848
}
4949

50-
export function isZodV3Type(arg: unknown): arg is z3.ZodTypeAny {
50+
function isZodV3Type(arg: unknown): arg is z3.ZodTypeAny {
5151
let prototype: null | object = null;
5252
if (isObject(arg) && isObject(arg.constructor)) {
5353
prototype = Reflect.getPrototypeOf(arg.constructor);
5454
}
5555
return Boolean(prototype && Reflect.get(prototype, 'name') === 'ZodType');
5656
}
57+
58+
function isZodType(arg: unknown, _options: { version: 3 }): arg is z3.ZodTypeAny {
59+
return isZodV3Type(arg);
60+
}
61+
62+
export { isZodType, isZodTypeLike };
63+
export type {
64+
ZodErrorLike,
65+
ZodIssueLike,
66+
ZodSafeParseErrorLike,
67+
ZodSafeParseResultLike,
68+
ZodSafeParseSuccessLike,
69+
ZodTypeLike
70+
};

0 commit comments

Comments
 (0)