Skip to content

Commit

Permalink
feat: brand support
Browse files Browse the repository at this point in the history
  • Loading branch information
ItMaga committed Apr 11, 2023
1 parent 5058527 commit 142ec1d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ createMock(schema); // { name: string, age: number }

- ZodNever
- ZodFunction
- ZodLazy
- ZodBranded
- ZodLazy
2 changes: 2 additions & 0 deletions lib/MockGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import MapGenerator from './generators/MapGenerator';
import SetGenerator from './generators/SetGenerator';
import RecordGenerator from './generators/RecordGenerator';
import PipelineGenerator from './generators/PipelineGenerator';
import BrandedGenerator from './generators/BrandedGenerator';

export default class MockGenerator<T extends z.ZodTypeAny> {
private generator: BaseGenerator<TypeOf<T>>;
Expand Down Expand Up @@ -69,6 +70,7 @@ export default class MockGenerator<T extends z.ZodTypeAny> {
ZodSet: SetGenerator,
ZodRecord: RecordGenerator,
ZodPipeline: PipelineGenerator,
ZodBranded: BrandedGenerator,
};
if (this.schema._def.typeName in generatorMap) {
this.generator = new generatorMap[this.schema._def.typeName as ZodFirstPartyTypeKind]();
Expand Down
10 changes: 10 additions & 0 deletions lib/generators/BrandedGenerator.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 BrandedGenerator<T extends z.ZodBranded<z.ZodTypeAny, string | number | symbol>> implements BaseGenerator<T> {
public generate(schema: T): z.infer<typeof schema> {
const mockGenerator = new MockGenerator(schema._def.type);
return mockGenerator.generate();
}
}
18 changes: 18 additions & 0 deletions tests/branded.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { describe, test } from 'vitest';
import { z } from 'zod';
import { expect } from './utils/expect';

describe('Branded', () => {
test('string', () => {
const schema = z.string().brand('myBrand');

expect(schema);
});
test('object', () => {
const schema = z.object({
foo: z.string().brand('brandKey'),
}).brand('brandObject');

expect(schema);
});
});

0 comments on commit 142ec1d

Please sign in to comment.