Skip to content

Commit 2cef7af

Browse files
committed
add support for branded types [closes #72]
1 parent 7f11f67 commit 2cef7af

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/modifiers/branded.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { z } from 'zod';
2+
import { expectSchema, registerSchema } from '../lib/helpers';
3+
4+
describe('branded', () => {
5+
it('generates OpenAPI schema for branded type', () => {
6+
expectSchema(
7+
[registerSchema('SimpleStringBranded', z.string().brand<'color'>())],
8+
{
9+
SimpleStringBranded: { type: 'string' },
10+
}
11+
);
12+
});
13+
});

src/lib/zod-is-type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { z } from 'zod';
33
type ZodTypes = {
44
ZodArray: z.ZodArray<any>;
55
ZodBoolean: z.ZodBoolean;
6+
ZodBranded: z.ZodBranded<any, any>;
67
ZodDefault: z.ZodDefault<any>;
78
ZodEffects: z.ZodEffects<any>;
89
ZodEnum: z.ZodEnum<any>;

src/openapi-generator.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,11 @@ export class OpenAPIGenerator {
10031003
}
10041004

10051005
private unwrapChained(schema: ZodSchema<any>): ZodSchema<any> {
1006-
if (isZodType(schema, 'ZodOptional') || isZodType(schema, 'ZodNullable')) {
1006+
if (
1007+
isZodType(schema, 'ZodOptional') ||
1008+
isZodType(schema, 'ZodNullable') ||
1009+
isZodType(schema, 'ZodBranded')
1010+
) {
10071011
return this.unwrapChained(schema.unwrap());
10081012
}
10091013

0 commit comments

Comments
 (0)