Skip to content

Commit

Permalink
feat: pipeline support
Browse files Browse the repository at this point in the history
  • Loading branch information
ItMaga committed Apr 11, 2023
1 parent 43bc568 commit dad0d3f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/MockGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import PromiseGenerator from './generators/PromiseGenerator';
import MapGenerator from './generators/MapGenerator';
import SetGenerator from './generators/SetGenerator';
import RecordGenerator from './generators/RecordGenerator';
import PipelineGenerator from './generators/PipelineGenerator';

export default class MockGenerator<T extends z.ZodTypeAny> {
private generator: BaseGenerator<TypeOf<T>>;
Expand Down Expand Up @@ -67,6 +68,7 @@ export default class MockGenerator<T extends z.ZodTypeAny> {
ZodMap: MapGenerator,
ZodSet: SetGenerator,
ZodRecord: RecordGenerator,
ZodPipeline: PipelineGenerator,
};
if (this.schema._def.typeName in generatorMap) {
this.generator = new generatorMap[this.schema._def.typeName as ZodFirstPartyTypeKind]();
Expand Down
11 changes: 11 additions & 0 deletions lib/generators/PipelineGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type { z } from 'zod';
import MockGenerator from '../MockGenerator';
import type BaseGenerator from './BaseGenerator';

export default class PipelineGenerator<T extends z.ZodPipeline<z.ZodTypeAny, z.ZodTypeAny>> implements BaseGenerator<T> {
public generate(schema: T): z.TypeOf<T> {
const { out: outSchema } = schema._def;
const outGenerated = new MockGenerator(outSchema).generate();
return outGenerated;
}
}
25 changes: 25 additions & 0 deletions tests/pipeline.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, test } from 'vitest';
import { z } from 'zod';
import { expect } from './utils/expect';

describe('Pipeline', () => {
test('with transform', () => {
const schema = z.string()
.transform(val => val.length)
.pipe(z.number().min(10));

expect(schema._def.out);
});
test('with coerse', () => {
const dateLike = z.union([z.number(), z.string(), z.date()]);
const schema = dateLike.pipe(z.coerce.date());

expect(schema._def.out);
});
test('with chain coerse', () => {
const toNumber = z.number().or(z.string()).pipe(z.coerce.number());
const toBigInt = z.bigint().or(toNumber).pipe(z.coerce.bigint());

expect(toBigInt._def.out);
});
});

0 comments on commit dad0d3f

Please sign in to comment.