Skip to content

Commit

Permalink
Ignore ZodNever and ZodUndefined (#202)
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy authored Jan 7, 2024
1 parent a02b929 commit 976ad5a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ For example in `z.string().nullable()` will be rendered differently
- ZodLiteral
- ZodNativeEnum
- supporting `string`, `number` and combined enums.
- ZodNever
- ZodNull
- ZodNullable
- ZodNumber
Expand All @@ -543,6 +544,7 @@ For example in `z.string().nullable()` will be rendered differently
- ZodTuple
- `items` mapping for `.rest()`
- `prefixItems` mapping for OpenAPI 3.1.0+
- ZodUndefined
- ZodUnion
- By default it outputs an `allOf` schema. Use `unionOneOf` to change this to output `oneOf` instead.
- ZodUnknown
Expand Down
15 changes: 15 additions & 0 deletions src/create/schema/parsers/object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,19 @@ describe('extend', () => {

expect(result).toStrictEqual(expected);
});

it('ignores ZodNever and ZodUndefined schemas', () => {
const expected: oas31.SchemaObject = {
type: 'object',
properties: {
a: { type: 'string' },
},
required: ['a'],
};
const schema = z.object({ a: z.string(), b: z.undefined(), c: z.never() });

const result = createObjectSchema(schema, createOutputState());

expect(result).toStrictEqual(expected);
});
});
7 changes: 7 additions & 0 deletions src/create/schema/parsers/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ export const mapProperties = (
): oas31.SchemaObject['properties'] =>
Object.entries(shape).reduce<NonNullable<oas31.SchemaObject['properties']>>(
(acc, [key, zodSchema]): NonNullable<oas31.SchemaObject['properties']> => {
if (
isZodType(zodSchema, 'ZodNever') ||
isZodType(zodSchema, 'ZodUndefined')
) {
return acc;
}

acc[key] = createSchemaObject(zodSchema, state, [`property: ${key}`]);
return acc;
},
Expand Down
6 changes: 5 additions & 1 deletion src/create/schema/parsers/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ export const isOptionalSchema = (
zodSchema: ZodTypeAny,
state: SchemaState,
): boolean => {
if (isZodType(zodSchema, 'ZodOptional')) {
if (
isZodType(zodSchema, 'ZodOptional') ||
isZodType(zodSchema, 'ZodNever') ||
isZodType(zodSchema, 'ZodUndefined')
) {
return true;
}

Expand Down

0 comments on commit 976ad5a

Please sign in to comment.