Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exclude enum types from being flagged by ResourceNameRestriction #738

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/rulesets/generated/spectral/az-arm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})