Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
25 changes: 24 additions & 1 deletion packages/zod/src/zod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4078,7 +4078,30 @@
);
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', "'Subject identifier.\\n\\nMust be normalized first.'"],

Check failure on line 4104 in packages/zod/src/zod.test.ts

View workflow job for this annotation

GitHub Actions / pr-checks (ubuntu-latest, 22.x)

`String.raw` should be used to avoid escaping `\`
]);
});

Expand Down
Loading