Skip to content

Commit 4ad89b4

Browse files
committed
fix: replace schema factories with pipe
1 parent 1c3e83e commit 4ad89b4

File tree

2 files changed

+30
-39
lines changed

2 files changed

+30
-39
lines changed

src/__tests__/zod.test.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as module from 'node:module';
33
import { describe, expect, it } from 'vitest';
44
import { z } from 'zod';
55

6-
import { $$BooleanLike, $$NumberLike, isZodType } from '../zod.js';
6+
import { $BooleanLike, $NumberLike, isZodType } from '../zod.js';
77

88
const require = module.createRequire(import.meta.url);
99

@@ -35,38 +35,39 @@ describe('isZodType', () => {
3535

3636
describe('$BooleanLike', () => {
3737
it('should parse "true" correctly', () => {
38-
expect($$BooleanLike().safeParse('true').data).toBe(true);
38+
expect($BooleanLike.safeParse('true').data).toBe(true);
3939
});
4040
it('should parse "false" correctly', () => {
41-
expect($$BooleanLike().safeParse('false').data).toBe(false);
41+
expect($BooleanLike.safeParse('false').data).toBe(false);
4242
});
4343
it('should parse booleans correctly', () => {
44-
expect($$BooleanLike().safeParse(true).data).toBe(true);
45-
expect($$BooleanLike().safeParse(false).data).toBe(false);
44+
expect($BooleanLike.safeParse(true).data).toBe(true);
45+
expect($BooleanLike.safeParse(false).data).toBe(false);
4646
});
4747
it('should fail to parse undefined', () => {
48-
expect($$BooleanLike().safeParse(undefined).success).toBe(false);
48+
expect($BooleanLike.safeParse(undefined).success).toBe(false);
4949
});
5050
it('should fail to parse an empty string', () => {
51-
expect($$BooleanLike().safeParse('').success).toBe(false);
51+
expect($BooleanLike.safeParse('').success).toBe(false);
5252
});
5353
it('should parse undefined, if set to optional', () => {
54-
const result = $$BooleanLike().optional().safeParse(undefined);
54+
const result = $BooleanLike.optional().safeParse(undefined);
5555
expect(result.success).toBe(true);
5656
expect(result.data).toBe(undefined);
5757
});
5858
});
5959

6060
describe('$NumberLike', () => {
6161
it('should parse a number', () => {
62-
expect($$NumberLike().safeParse(1).data).toBe(1);
62+
expect($NumberLike.safeParse(1).data).toBe(1);
6363
});
6464
it('should fail to parse non-numbers', () => {
65-
expect($$NumberLike().safeParse(NaN).success).toBe(false);
66-
expect($$NumberLike().safeParse('').success).toBe(false);
65+
expect($NumberLike.safeParse(NaN).success).toBe(false);
66+
expect($NumberLike.safeParse('').success).toBe(false);
6767
});
68-
it('should allow restricting the number', () => {
69-
expect($$NumberLike((base) => base.int()).safeParse('1.1').success).toBe(false);
70-
expect($$NumberLike((base) => base.int()).safeParse('1').data).toBe(1);
68+
it('should allow restricting the number through a pipe', () => {
69+
const $IntLike = $NumberLike.pipe(z.number().int());
70+
expect($IntLike.safeParse('1.1').success).toBe(false);
71+
expect($IntLike.safeParse('1').data).toBe(1);
7172
});
7273
});

src/zod.ts

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,6 @@ import { z } from 'zod';
33
import { isNumberLike, parseNumber } from './number.js';
44
import { isObject } from './object.js';
55

6-
const defaultExtend = <TSchema extends z.ZodTypeAny>(schema: TSchema): TSchema => schema;
7-
8-
type SchemaFactory<TSchema extends z.ZodTypeAny, TInput = any> = (
9-
extend?: (base: TSchema) => TSchema
10-
) => z.ZodEffects<TSchema, z.TypeOf<TSchema>, TInput>;
11-
126
/** Used to determine if object is of type `ZodType` independent of specific instances or library versions */
137
export function isZodType(arg: unknown): arg is z.ZodTypeAny {
148
let prototype: null | object = null;
@@ -18,24 +12,20 @@ export function isZodType(arg: unknown): arg is z.ZodTypeAny {
1812
return Boolean(prototype && Reflect.get(prototype, 'name') === 'ZodType');
1913
}
2014

21-
export const $$BooleanLike: SchemaFactory<z.ZodBoolean> = (extend = defaultExtend) => {
22-
return z.preprocess((arg) => {
23-
if (typeof arg === 'string') {
24-
if (arg.trim().toLowerCase() === 'true') {
25-
return true;
26-
} else if (arg.trim().toLowerCase() === 'false') {
27-
return false;
28-
}
15+
export const $BooleanLike = z.preprocess((arg) => {
16+
if (typeof arg === 'string') {
17+
if (arg.trim().toLowerCase() === 'true') {
18+
return true;
19+
} else if (arg.trim().toLowerCase() === 'false') {
20+
return false;
2921
}
30-
return arg;
31-
}, extend(z.boolean()));
32-
};
22+
}
23+
return arg;
24+
}, z.boolean());
3325

34-
export const $$NumberLike: SchemaFactory<z.ZodNumber> = (extend = defaultExtend) => {
35-
return z.preprocess((arg) => {
36-
if (isNumberLike(arg)) {
37-
return parseNumber(arg);
38-
}
39-
return arg;
40-
}, extend(z.number()));
41-
};
26+
export const $NumberLike = z.preprocess((arg) => {
27+
if (isNumberLike(arg)) {
28+
return parseNumber(arg);
29+
}
30+
return arg;
31+
}, z.number());

0 commit comments

Comments
 (0)