Skip to content

Commit

Permalink
feat: function support
Browse files Browse the repository at this point in the history
  • Loading branch information
ItMaga committed Apr 11, 2023
1 parent 142ec1d commit 7edb500
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 1 deletion.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ createMock(schema); // { name: string, age: number }
## TODO

- ZodNever
- ZodFunction
- ZodLazy
3 changes: 3 additions & 0 deletions lib/MockGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends z.ZodTypeAny> {
private generator: BaseGenerator<TypeOf<T>>;
Expand Down Expand Up @@ -71,7 +72,9 @@ export default class MockGenerator<T extends z.ZodTypeAny> {
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;
Expand Down
10 changes: 10 additions & 0 deletions lib/generators/FunctionGenerator.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 FunctionGenerator<T extends z.ZodFunction<z.ZodTuple<any, any>, z.ZodTypeAny>> implements BaseGenerator<T> {
public generate(schema: T): z.infer<typeof schema> {
const mockGenerator = new MockGenerator(schema._def.returns);
return () => mockGenerator.generate();
}
}
4 changes: 4 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import type { z } from 'zod';
import MockGenerator from './MockGenerator';

function createMock<Schema extends z.ZodTypeAny>(schema: Schema): z.infer<typeof schema> {
if (typeof schema === 'function') {
throw new TypeError('You must pass a schema to createMock');
}

const mockGenerator = new MockGenerator(schema);
return mockGenerator.generate();
}
Expand Down
31 changes: 31 additions & 0 deletions tests/function.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 7edb500

Please sign in to comment.