Skip to content

Commit

Permalink
WIP rename rule
Browse files Browse the repository at this point in the history
  • Loading branch information
bdefoy committed May 22, 2023
1 parent b70f43e commit 3b9c803
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 31 deletions.
8 changes: 5 additions & 3 deletions docs/get-collection-only-has-value-nextlink.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# GetCollectionOnlyHasValueAndNextLink
# GetCollectionResponseSchema

## Category

Expand All @@ -18,8 +18,10 @@ List GET endpoints (collection GET) must only have `value` and `nextLink` in `pr

## Why the rule is important

This rule is important to retain consistency between ARM APIs. Furthermore, not adhering to this rule will cause issues with Azure Resource Graph (ARG) integration.
This rule is important to retain consistency between ARM APIs. Furthermore, not adhering to this rule will cause issues
with Azure Resource Graph (ARG) integration.

## How to fix the violation

Under `properties` in the schema for GET endpoints, ensure the fields `value` and `nextLink` are present, and no other fields are present.
Under `properties` in the schema for GET endpoints, ensure the fields `value` and `nextLink` are present, and no other
fields are present.
2 changes: 1 addition & 1 deletion docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ Instead, consider defining a `body` parameter with `type: string, format: binary

Please refer to [formdata.md](./formdata.md) for details.

### GetCollectionOnlyHasValueAndNextLink
### GetCollectionResponseSchema

List GET endpoints (collection GET) must only have `value` and `nextLink` in `properties`.

Expand Down
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 @@ -2682,7 +2682,7 @@ const ruleset = {
function: falsy,
},
},
GetCollectionOnlyHasValueAndNextLink: {
GetCollectionResponseSchema: {
description: "Get endpoints for collections of resources must only have the `value` and `nextLink` properties in their model.",
message: "{{description}}",
severity: "error",
Expand Down
9 changes: 5 additions & 4 deletions packages/rulesets/src/spectral/az-arm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import collectionObjectPropertiesNaming from "./functions/collection-object-prop
import { consistentPatchProperties } from "./functions/consistent-patch-properties"
import { DeleteResponseCodes } from "./functions/delete-response-codes"
import { longRunningResponseStatusCodeArm } from "./functions/Extensions/long-running-response-status-code"
import { getCollectionOnlyHasValueAndNextLink } from "./functions/get-collection-only-has-value-nextlink"
import { getCollectionResponseSchema } from "./functions/get-collection-response-schema"
import hasApiVersionParameter from "./functions/has-api-version-parameter"
import hasheader from "./functions/has-header"
import httpsSupportedScheme from "./functions/https-supported-scheme"
Expand Down Expand Up @@ -290,15 +290,16 @@ const ruleset: any = {
},
},
// RPC Codes: RPC-Get-V1-09, RPC-Arg-V1-01, RPC-Get-V1-06
GetCollectionOnlyHasValueAndNextLink: {
GetCollectionResponseSchema: {
description: "Get endpoints for collections of resources must only have the `value` and `nextLink` properties in their model.",
message: "{{description}}",
severity: "error",
resolved: true,
formats: [oas2],
given: "$[paths,'x-ms-paths'][?([email protected]('}') && [email protected]('operations'))][get].responses.200.schema.properties",
given:
"$[paths,'x-ms-paths'][?([email protected]('}') && @property.endsWith('s') && [email protected]('operations'))][get].responses.200.schema.properties",
then: {
function: getCollectionOnlyHasValueAndNextLink,
function: getCollectionResponseSchema,
},
},

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Verifies that collection get schemas have only the `value` and `nextLink` properties in their models.

const ALLOWED_KEYS = ["value", "nextLink"]
const ERROR_MESSAGE = "Get endpoints for collections of resources must only have the `value` and `nextLink` properties in their model."

// TODO: might need to use something like getCollectionApiInfo() in arm-helper.ts
// TODO: can't call it this. there already is a rule fo this GetCollectionResponseSchema.ts

export const getCollectionResponseSchema = (properties: any, _opts: any, ctx: any) => {
if (!properties || typeof properties !== "object") {
return []
}
const keys = Object.keys(properties)

if (keys.length > 2 || keys.some((key) => !ALLOWED_KEYS.includes(key))) {
return [
{
message: ERROR_MESSAGE,
},
]
}
return []
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import linterForRule from "./utils"
let linter: Spectral

beforeAll(async () => {
linter = await linterForRule("GetCollectionOnlyHasValueAndNextLink")
linter = await linterForRule("GetCollectionResponseSchema")
return linter
})

test("GetCollectionOnlyHasValueAndNextLink should find no errors when get collection schema has only value and nextLink properties", () => {
test("GetCollectionResponseSchema should find no errors when get collection schema has only value and nextLink properties", () => {
const myOpenApiDocument = {
swagger: "2.0",
paths: {
Expand Down Expand Up @@ -66,7 +66,7 @@ test("GetCollectionOnlyHasValueAndNextLink should find no errors when get collec
})
})

test("GetCollectionOnlyHasValueAndNextLink should find errors when get collection schema has properties other than value and nextLink", () => {
test("GetCollectionResponseSchema should find errors when get collection schema has properties other than value and nextLink", () => {
const myOpenApiDocument = {
swagger: "2.0",
paths: {
Expand Down Expand Up @@ -131,7 +131,7 @@ test("GetCollectionOnlyHasValueAndNextLink should find errors when get collectio
})
})

test("GetCollectionOnlyHasValueAndNextLink should find errors when nextLink is missing", () => {
test("GetCollectionResponseSchema should find errors when nextLink is missing", () => {
const myOpenApiDocument = {
swagger: "2.0",
paths: {
Expand Down Expand Up @@ -188,7 +188,7 @@ test("GetCollectionOnlyHasValueAndNextLink should find errors when nextLink is m
})
})

test("GetCollectionOnlyHasValueAndNextLink should find errors when value is missing", () => {
test("GetCollectionResponseSchema should find errors when value is missing", () => {
const myOpenApiDocument = {
swagger: "2.0",
paths: {
Expand Down

0 comments on commit 3b9c803

Please sign in to comment.