From 7bd8df40eeb88e08a92ddf759fbd506c7804cec1 Mon Sep 17 00:00:00 2001 From: akhilailla Date: Wed, 4 Sep 2024 15:42:52 -0500 Subject: [PATCH 1/2] exclude enum types from being flagged --- .../functions/resource-name-restriction.ts | 3 +- .../test/resource-names-restriction.test.ts | 36 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/packages/rulesets/src/spectral/functions/resource-name-restriction.ts b/packages/rulesets/src/spectral/functions/resource-name-restriction.ts index 727dd4756..1ce9b665c 100644 --- a/packages/rulesets/src/spectral/functions/resource-name-restriction.ts +++ b/packages/rulesets/src/spectral/functions/resource-name-restriction.ts @@ -31,7 +31,8 @@ export const resourceNameRestriction = (paths: any, _opts: any, ctx: any) => { // Get the preceding path segment if (param?.match(/^\w+Name+$/) && !EXCEPTION_LIST.includes(param)) { const paramDefinition = getPathParameter(paths[pathKey], param) - if (paramDefinition && !paramDefinition.pattern) { + // resource name param with enum doesnt need to explicitly have pattern specified + if (paramDefinition && !paramDefinition.enum && !paramDefinition.pattern) { errors.push({ message: `The resource name parameter '${param}' should be defined with a 'pattern' restriction.`, path: [...path, pathKey], diff --git a/packages/rulesets/src/spectral/test/resource-names-restriction.test.ts b/packages/rulesets/src/spectral/test/resource-names-restriction.test.ts index 8fdff22de..669348118 100644 --- a/packages/rulesets/src/spectral/test/resource-names-restriction.test.ts +++ b/packages/rulesets/src/spectral/test/resource-names-restriction.test.ts @@ -144,3 +144,39 @@ test("ResourceNameRestriction should find no errors for system-defined variables expect(results.length).toBe(0) }) }) + +test("ResourceNameRestriction should find no errors for type enums", () => { + const oasDoc = { + swagger: "2.0", + paths: { + "/subscriptions/{subscriptionId}/providers/Microsoft.AzurePlaywrightService/locations/{location}/quotas/{quotaName}": { + get: { + operationId: "Quotas_Get", + tags: ["Quotas"], + description: "Get quota by name.", + parameters: [ + { + name: "location", + in: "path", + description: "The location of quota in ARM Normalized format like eastus, southeastasia etc.", + required: true, + type: "string", + }, + { + name: "quotaName", + in: "path", + description: "The quota name.", + required: true, + type: "string", + enum: ["ScalableExecution", "Reporting"], + }, + ], + responses: {}, + }, + }, + }, + } + return linter.run(oasDoc).then((results) => { + expect(results.length).toBe(0) + }) +}) From 47570c775a99ed667810a6a0992d273cd039cdc5 Mon Sep 17 00:00:00 2001 From: akhilailla Date: Wed, 4 Sep 2024 15:49:41 -0500 Subject: [PATCH 2/2] Changes from rush prep --- packages/rulesets/generated/spectral/az-arm.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rulesets/generated/spectral/az-arm.js b/packages/rulesets/generated/spectral/az-arm.js index d8d773109..89f8591de 100644 --- a/packages/rulesets/generated/spectral/az-arm.js +++ b/packages/rulesets/generated/spectral/az-arm.js @@ -2612,7 +2612,7 @@ const resourceNameRestriction = (paths, _opts, ctx) => { const param = (_a = v.match(/[^{}]+(?=})/)) === null || _a === void 0 ? void 0 : _a[0]; if ((param === null || param === void 0 ? void 0 : param.match(/^\w+Name+$/)) && !EXCEPTION_LIST.includes(param)) { const paramDefinition = getPathParameter(paths[pathKey], param); - if (paramDefinition && !paramDefinition.pattern) { + if (paramDefinition && !paramDefinition.enum && !paramDefinition.pattern) { errors.push({ message: `The resource name parameter '${param}' should be defined with a 'pattern' restriction.`, path: [...path, pathKey],