Skip to content

Commit

Permalink
feat: add primitive lazy support
Browse files Browse the repository at this point in the history
  • Loading branch information
ItMaga committed May 2, 2023
1 parent b63b909 commit 800e9a5
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const api = {
## TODO

- ZodNever
- ZodLazy
- ZodLazy (recursive types)

## License

Expand Down
2 changes: 2 additions & 0 deletions lib/MockGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import RecordGenerator from './generators/RecordGenerator';
import PipelineGenerator from './generators/PipelineGenerator';
import BrandedGenerator from './generators/BrandedGenerator';
import FunctionGenerator from './generators/FunctionGenerator';
import LazyGenerator from './generators/LazyGenerator';

export default class MockGenerator<T extends z.ZodTypeAny> {
private generator: BaseGenerator<TypeOf<T>>;
Expand Down Expand Up @@ -73,6 +74,7 @@ export default class MockGenerator<T extends z.ZodTypeAny> {
ZodPipeline: PipelineGenerator,
ZodBranded: BrandedGenerator,
ZodFunction: FunctionGenerator,
ZodLazy: LazyGenerator,
};

if (this.schema._def.typeName in generatorMap) {
Expand Down
10 changes: 10 additions & 0 deletions lib/generators/LazyGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { z } from 'zod';
import MockGenerator from '../MockGenerator';
import type BaseGenerator from './BaseGenerator';

export default class LazyGenerator<T extends z.ZodLazy<z.ZodTypeAny>> implements BaseGenerator<T> {
public generate(schema: T): z.infer<typeof schema> {
const mockGenerator = new MockGenerator(schema._def.getter());
return mockGenerator.generate();
}
}
11 changes: 11 additions & 0 deletions tests/lazy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { describe, test } from 'vitest';
import { z } from 'zod';
import { expect } from './utils/expect';

describe('Lazy', () => {
test('primitive', () => {
const schema = z.lazy(() => z.string());

expect(schema);
});
});

0 comments on commit 800e9a5

Please sign in to comment.