diff --git a/README.md b/README.md index 79103e2..c7f53b3 100644 --- a/README.md +++ b/README.md @@ -36,5 +36,4 @@ createMock(schema); // { name: string, age: number } ## TODO - ZodNever -- ZodFunction - ZodLazy \ No newline at end of file diff --git a/lib/MockGenerator.ts b/lib/MockGenerator.ts index 2e626b7..f00e3ae 100644 --- a/lib/MockGenerator.ts +++ b/lib/MockGenerator.ts @@ -30,6 +30,7 @@ import SetGenerator from './generators/SetGenerator'; import RecordGenerator from './generators/RecordGenerator'; import PipelineGenerator from './generators/PipelineGenerator'; import BrandedGenerator from './generators/BrandedGenerator'; +import FunctionGenerator from './generators/FunctionGenerator'; export default class MockGenerator { private generator: BaseGenerator>; @@ -71,7 +72,9 @@ export default class MockGenerator { ZodRecord: RecordGenerator, ZodPipeline: PipelineGenerator, ZodBranded: BrandedGenerator, + ZodFunction: FunctionGenerator, }; + if (this.schema._def.typeName in generatorMap) { this.generator = new generatorMap[this.schema._def.typeName as ZodFirstPartyTypeKind](); return; diff --git a/lib/generators/FunctionGenerator.ts b/lib/generators/FunctionGenerator.ts new file mode 100644 index 0000000..29b0bc2 --- /dev/null +++ b/lib/generators/FunctionGenerator.ts @@ -0,0 +1,10 @@ +import type { z } from 'zod'; +import MockGenerator from '../MockGenerator'; +import type BaseGenerator from './BaseGenerator'; + +export default class FunctionGenerator, z.ZodTypeAny>> implements BaseGenerator { + public generate(schema: T): z.infer { + const mockGenerator = new MockGenerator(schema._def.returns); + return () => mockGenerator.generate(); + } +} diff --git a/lib/index.ts b/lib/index.ts index 5b87069..8c50218 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -2,6 +2,10 @@ import type { z } from 'zod'; import MockGenerator from './MockGenerator'; function createMock(schema: Schema): z.infer { + if (typeof schema === 'function') { + throw new TypeError('You must pass a schema to createMock'); + } + const mockGenerator = new MockGenerator(schema); return mockGenerator.generate(); } diff --git a/tests/function.test.ts b/tests/function.test.ts new file mode 100644 index 0000000..6f03890 --- /dev/null +++ b/tests/function.test.ts @@ -0,0 +1,31 @@ +import { describe, test } from 'vitest'; +import { z } from 'zod'; +import { expect } from './utils/expect'; + +describe('Function', () => { + test('base', () => { + const schema = z.function(); + + expect(schema); + }); + test('with args as param', () => { + const schema = z.function(z.tuple([z.string(), z.number()]), z.boolean()); + + expect(schema); + }); + test('with args as method', () => { + const schema = z.function().args(z.string(), z.number()); + + expect(schema); + }); + test('with returns as param', () => { + const schema = z.function(z.tuple([z.string(), z.number()]), z.boolean()); + + expect(schema); + }); + test('with returns as method', () => { + const schema = z.function().returns(z.boolean()); + + expect(schema); + }); +});