-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |