Skip to content

Commit 4dda8d6

Browse files
Add more descriptive message for @pattern decorator when applied to union type (#7586)
issue: Azure/typespec-azure#2759
1 parent b2694ea commit 4dda8d6

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/compiler"
5+
---
6+
7+
Improved the error message for the `@pattern` decorator when applied to a `union` type. The new message is more descriptive and helps users understand how to correctly define string-compatible union types.

packages/compiler/src/lib/decorators.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,17 @@ function validateTargetingAString(
294294
target: Scalar | ModelProperty,
295295
decoratorName: string,
296296
) {
297-
const valid = isTypeIn(getPropertyType(target), (x) => isStringType(context.program, x));
297+
const propertyType = getPropertyType(target);
298+
const valid = isTypeIn(propertyType, (x) => isStringType(context.program, x));
298299
if (!valid) {
299300
reportDiagnostic(context.program, {
300301
code: "decorator-wrong-target",
301302
format: {
302303
decorator: decoratorName,
303-
to: `type it is not a string`,
304+
to:
305+
propertyType.kind === "Union"
306+
? `a union type that is not string compatible. The union must explicitly include a string type, and all union values should be strings. For example: union Test { string, "A", "B" }`
307+
: `type it is not a string`,
304308
},
305309
target: context.decoratorTarget,
306310
});

packages/compiler/test/decorators/decorators.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,34 @@ describe("compiler: built-in decorators", () => {
272272
});
273273
});
274274

275+
it("emit diagnostic if pattern targe is a non-string union", async () => {
276+
const diagnostics = await runner.diagnose(`
277+
model Employee {
278+
@pattern("^[a-zA-Z0-9-]{3,24}$")
279+
name: Test;
280+
}
281+
union Test { 1, int32 }
282+
`);
283+
284+
expectDiagnostics(diagnostics, {
285+
code: "decorator-wrong-target",
286+
message:
287+
'Cannot apply @pattern decorator to a union type that is not string compatible. The union must explicitly include a string type, and all union values should be strings. For example: union Test { string, "A", "B" }',
288+
});
289+
});
290+
291+
it("allows string union on @pattern", async () => {
292+
const diagnostics = await runner.diagnose(`
293+
model Employee {
294+
@pattern("^[a-zA-Z0-9-]{3,24}$")
295+
name: Test;
296+
}
297+
union Test { "A", "B", string }
298+
`);
299+
300+
expectDiagnosticEmpty(diagnostics);
301+
});
302+
275303
it("emit diagnostic if pattern is not a valid RegEx", async () => {
276304
const diagnostics = await runner.diagnose(`
277305
model A {

0 commit comments

Comments
 (0)