Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/zod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,14 @@ export const generateZodValidationSchemaDefinition = (
}

if (typeof siblingSchema.description === 'string') {
functions.push(['describe', `"${escape(siblingSchema.description)}"`]);
// Use the same single-quoted, fully JS-escaped form as the primitive
// description path (see `pushDescriptionOrMeta`). `escape` only escapes
// quote chars, so a multi-line description would emit raw newlines and
// break the generated string literal (TS1002).
functions.push([
'describe',
`'${jsStringEscape(siblingSchema.description)}'`,
]);
}
Comment on lines 283 to 292
};

Expand Down
28 changes: 27 additions & 1 deletion packages/zod/src/zod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4078,7 +4078,33 @@ describe('generateZodValidationSchemaDefinition`', () => {
);
expect(result.functions).toEqual([
['namedRef', { name: 'Pet', sourceRef: '#/components/schemas/Pet' }],
['describe', '"optional pet"'],
['describe', "'optional pet'"],
]);
});

it('escapes newlines in a multi-line description sibling on a $ref (#3517)', () => {
const context = makeContextSpec({
spec: { components: { schemas: { Pet: { type: 'object' } } } },
});
const result = generateZodValidationSchemaDefinition(
{
$ref: '#/components/schemas/Pet',
description: 'Subject identifier.\n\nMust be normalized first.',
} as never,
context,
'someField',
false,
false,
{ required: true, useReusableSchemas: true },
);
// Same single-quoted, \n-escaped form as a primitive sibling — no raw
// newlines that would break the generated string literal (TS1002).
expect(result.functions).toEqual([
['namedRef', { name: 'Pet', sourceRef: '#/components/schemas/Pet' }],
[
'describe',
String.raw`'Subject identifier.\n\nMust be normalized first.'`,
],
]);
});

Expand Down
Loading