Skip to content

Commit 789d929

Browse files
authored
fix: type (#577)
* docs: update * fix: type * test: transform
1 parent fc793b6 commit 789d929

File tree

7 files changed

+35
-26
lines changed

7 files changed

+35
-26
lines changed

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ Built-in zod types:
2525
```ts
2626
import * as z from 'zod' // or import * as z from 'zod/mini'
2727
import { faker } from '@faker-js/faker'
28-
import { fake, fakeSchema, setFaker } from 'zod-schema-faker'
28+
import { fake, setFaker } from 'zod-schema-faker'
2929

30-
const User = z.interface({
31-
name: z.string(),
32-
age: z.uint32(),
30+
const Player = z.object({
31+
username: z.string(),
32+
xp: z.number(),
3333
})
3434

3535
// enable tree shaking
3636
if (process.env.NODE_ENV === 'development') {
37-
setFaker(faker) // or setFaker(your faker instance)
38-
fake(z.string()) // { name: 'lorem', age: 42 }
37+
setFaker(faker)
38+
const data = fake(Player)
39+
console.log(data) // { username: "billie", xp: 100 }
3940
}
4041
```
4142

@@ -88,11 +89,9 @@ fake(px) // '100px'
8889
## Unsupported
8990

9091
- .file 🚧
91-
- .function 🚧
9292
- .intersection 🚧
9393
- .refine ❌
9494
- .superRefine ❌
95-
- .transform 🚧
9695

9796
## About
9897

e2e/issue-189/src/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@ import { expectType, TypeEqual } from 'ts-expect'
33
import { z } from 'zod'
44
import { fake, setFaker } from 'zod-schema-faker'
55

6-
const schema = z.number()
6+
const Player = z.object({
7+
username: z.string(),
8+
xp: z.number(),
9+
})
710

811
// enable tree shaking
912
if (process.env.NODE_ENV === 'development') {
1013
setFaker(faker)
11-
const data = fake(schema)
12-
13-
console.log(data) // => -2556.9
14+
const data = fake(Player)
15+
console.log(data) // { username: "billie", xp: 100 }
1416

1517
expectType<TypeEqual<typeof data, any>>(false)
16-
expectType<TypeEqual<typeof data, number>>(true)
18+
expectType<TypeEqual<typeof data, { username: string; xp: number }>>(true)
1719
}

src/fake.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
import * as core from 'zod/v4/core'
12
import { rootFake } from './internals/fake'
2-
import { ZodType } from './internals/type'
33

4-
export function fake<T extends ZodType>(schema: T): T['_zod']['output'] {
4+
export function fake<T extends core.$ZodType>(schema: T): core.infer<T> {
55
return rootFake(schema, { depth: 0 })
66
}

src/internals/schemas/custom.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as core from 'zod/v4/core'
22
import { Context } from '../context'
3-
import { Fake, Infer, RootFake, ZodType } from '../type'
3+
import { Fake, Infer, RootFake } from '../type'
44

5-
const customs = new Map<ZodType, Fake<ZodType>>()
6-
export function custom<T extends ZodType>(schema: T, fake: Fake<T>): void {
5+
const customs = new Map<core.$ZodType, Fake<core.$ZodType>>()
6+
export function custom<T extends core.$ZodType>(schema: T, fake: Fake<T>): void {
77
customs.set(schema, fake as any)
88
}
99

src/internals/type.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
import * as core from 'zod/v4/core'
12
import { Context } from './context'
23

3-
export type ZodType<O = unknown> = { _zod: { output: O } } // TODO: use core.$ZodType
4+
export type Infer<T extends core.$ZodType> = T['_zod']['output']
45

5-
export type Infer<T extends ZodType> = T['_zod']['output']
6+
export type Fake<T extends core.$ZodType> = (schema: T, context: Context, fake: RootFake) => core.infer<T>
67

7-
export type Fake<T extends ZodType> = (schema: T, context: Context, fake: RootFake) => Infer<T>
8-
9-
export type RootFake = <T extends ZodType>(schema: T, context: Context) => Infer<T>
8+
export type RootFake = <T extends core.$ZodType>(schema: T, context: Context) => core.infer<T>

tests/mini.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,8 +596,14 @@ const validSuits: { schema: z.ZodMiniType; description?: string; only?: boolean;
596596
description: 'refinement',
597597
},
598598

599-
// TODO: transform
600-
// { schema: z.transform(val => String(val)) },
599+
// transform
600+
{
601+
schema: z.pipe(
602+
z.string(),
603+
z.transform(val => val.length),
604+
),
605+
description: 'transform',
606+
},
601607

602608
// tuple
603609
{

tests/zod.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,10 @@ const validSuits: { schema: z.ZodType; description?: string; only?: boolean; asy
598598
},
599599

600600
// TODO: transform
601-
// { schema: z.transform(val => String(val)) },
601+
{
602+
schema: z.string().pipe(z.transform(val => val.length)),
603+
description: 'transform',
604+
},
602605

603606
// tuple
604607
{

0 commit comments

Comments
 (0)