-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
75 additions
and
9 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 |
---|---|---|
|
@@ -95,7 +95,6 @@ export const api = { | |
## TODO | ||
|
||
- ZodNever | ||
- ZodLazy (recursive types) | ||
|
||
## License | ||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export class DepthLimitError extends Error { | ||
constructor() { | ||
super('Depth limit reached'); | ||
this.name = 'DepthLimitError'; | ||
} | ||
} |
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,13 +1,24 @@ | ||
import type { ZodRawShape, z } from 'zod'; | ||
import MockGenerator from '../MockGenerator'; | ||
import { DepthLimitError } from '../errors/DepthLimitError'; | ||
import type BaseGenerator from './BaseGenerator'; | ||
|
||
export default class ObjectGenerator<T extends ZodRawShape, U extends z.ZodObject<T>> implements BaseGenerator<U> { | ||
public generate(schema: U) { | ||
return Object.entries(schema._def.shape()).reduce((acc, [key, value]) => { | ||
const mockGenerator = new MockGenerator(value); | ||
acc[key as keyof z.infer<U>] = mockGenerator.generate(); | ||
return acc; | ||
}, {} as z.infer<U>); | ||
const generated: z.infer<U> = {} as z.infer<U>; | ||
Object.entries(schema._def.shape()).forEach(([key, value]) => { | ||
try { | ||
const mockGenerator = new MockGenerator(value); | ||
generated[key as keyof z.infer<U>] = mockGenerator.generate(); | ||
} | ||
catch (e) { | ||
if (e instanceof DepthLimitError) { | ||
return; | ||
} | ||
throw e; | ||
} | ||
}); | ||
|
||
return generated; | ||
} | ||
} |
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