diff --git a/README.md b/README.md index 318703a..d06a68d 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ export const api = { ## TODO - ZodNever -- ZodLazy +- ZodLazy (recursive types) ## License diff --git a/lib/MockGenerator.ts b/lib/MockGenerator.ts index f00e3ae..2bc5cfc 100644 --- a/lib/MockGenerator.ts +++ b/lib/MockGenerator.ts @@ -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 { private generator: BaseGenerator>; @@ -73,6 +74,7 @@ export default class MockGenerator { ZodPipeline: PipelineGenerator, ZodBranded: BrandedGenerator, ZodFunction: FunctionGenerator, + ZodLazy: LazyGenerator, }; if (this.schema._def.typeName in generatorMap) { diff --git a/lib/generators/LazyGenerator.ts b/lib/generators/LazyGenerator.ts new file mode 100644 index 0000000..3a9cdcf --- /dev/null +++ b/lib/generators/LazyGenerator.ts @@ -0,0 +1,10 @@ +import type { z } from 'zod'; +import MockGenerator from '../MockGenerator'; +import type BaseGenerator from './BaseGenerator'; + +export default class LazyGenerator> implements BaseGenerator { + public generate(schema: T): z.infer { + const mockGenerator = new MockGenerator(schema._def.getter()); + return mockGenerator.generate(); + } +} diff --git a/tests/lazy.test.ts b/tests/lazy.test.ts new file mode 100644 index 0000000..2fba932 --- /dev/null +++ b/tests/lazy.test.ts @@ -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); + }); +});