From 4f8caae8fc59816163178b8c786792acbd5bef11 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 29 Apr 2024 13:29:55 -0700 Subject: [PATCH] Allow @pattern to be provided on a scalar (#751) fix [#732](https://github.com/Azure/typespec-azure/issues/732) --- .../fix-allow-scalars-2024-3-26-23-13-19.md | 8 +++++++ .../src/rules/arm-resource-name-pattern.ts | 19 +++++++++++++-- .../rules/arm-resource-name-pattern.test.ts | 23 ++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 .chronus/changes/fix-allow-scalars-2024-3-26-23-13-19.md diff --git a/.chronus/changes/fix-allow-scalars-2024-3-26-23-13-19.md b/.chronus/changes/fix-allow-scalars-2024-3-26-23-13-19.md new file mode 100644 index 0000000000..d963bf1ba4 --- /dev/null +++ b/.chronus/changes/fix-allow-scalars-2024-3-26-23-13-19.md @@ -0,0 +1,8 @@ +--- +# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking +changeKind: fix +packages: + - "@azure-tools/typespec-azure-resource-manager" +--- + +Allow `@pattern` to be provided on a scalar diff --git a/packages/typespec-azure-resource-manager/src/rules/arm-resource-name-pattern.ts b/packages/typespec-azure-resource-manager/src/rules/arm-resource-name-pattern.ts index 804a2e221d..4db6f20be1 100644 --- a/packages/typespec-azure-resource-manager/src/rules/arm-resource-name-pattern.ts +++ b/packages/typespec-azure-resource-manager/src/rules/arm-resource-name-pattern.ts @@ -1,5 +1,6 @@ import { DiagnosticTarget, + ModelProperty, Program, SourceLocation, createRule, @@ -59,8 +60,7 @@ export const armResourceNamePatternRule = createRule({ // find the name property const nameProperty = resource.typespecType.properties.get("name"); if (nameProperty !== undefined) { - const pattern = getPattern(program, nameProperty); - if (pattern === undefined) { + if (!hasPattern(program, nameProperty)) { context.reportDiagnostic({ target: nameProperty, codefixes: [createPatternCodeFix(nameProperty)], @@ -72,3 +72,18 @@ export const armResourceNamePatternRule = createRule({ }; }, }); + +function hasPattern(program: Program, property: ModelProperty): boolean { + const pattern = getPattern(program, property); + if (pattern !== undefined) { + return true; + } + + if (property.type.kind === "Scalar") { + const pattern = getPattern(program, property.type); + if (pattern !== undefined) { + return true; + } + } + return false; +} diff --git a/packages/typespec-azure-resource-manager/test/rules/arm-resource-name-pattern.test.ts b/packages/typespec-azure-resource-manager/test/rules/arm-resource-name-pattern.test.ts index f5a96a860c..bbd416ce78 100644 --- a/packages/typespec-azure-resource-manager/test/rules/arm-resource-name-pattern.test.ts +++ b/packages/typespec-azure-resource-manager/test/rules/arm-resource-name-pattern.test.ts @@ -94,7 +94,6 @@ it("Does not emit a warning for an ARM resource that specifies `@pattern` on the namespace Microsoft.Contoso; model Employee is ProxyResource<{}> { - @doc("Name of employee") @pattern("^[a-zA-Z0-9-]{3,24}$") @key("employeeName") @path @@ -114,3 +113,25 @@ it("Does not emit a warning for an ARM resource that specifies `@pattern` on the ) .toBeValid(); }); + +it("Does not emit a warning for an ARM resource that specifies `@pattern` on the on the scalar used", async () => { + await tester + .expect( + ` + @armProviderNamespace + @useDependency(Azure.ResourceManager.Versions.v1_0_Preview_1) + namespace Microsoft.Contoso; + + @pattern("^[a-zA-Z0-9][a-zA-Z0-9-]{1,58}[a-zA-Z0-9]$") + scalar stringResourceName extends string; + + model Employee is ProxyResource<{}> { + @key("employeeName") + @path + @segment("employees") + name: stringResourceName; + } + ` + ) + .toBeValid(); +});