Skip to content

Commit

Permalink
Allow @pattern to be provided on a scalar (#751)
Browse files Browse the repository at this point in the history
fix [#732](#732)
  • Loading branch information
timotheeguerin authored Apr 29, 2024
1 parent 74a3d6c commit 4f8caae
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
8 changes: 8 additions & 0 deletions .chronus/changes/fix-allow-scalars-2024-3-26-23-13-19.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
DiagnosticTarget,
ModelProperty,
Program,
SourceLocation,
createRule,
Expand Down Expand Up @@ -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)],
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
});

0 comments on commit 4f8caae

Please sign in to comment.