-
Notifications
You must be signed in to change notification settings - Fork 43
/
DefaultErrorResponseSchema.ts
52 lines (46 loc) · 1.92 KB
/
DefaultErrorResponseSchema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { nodes, stringify } from "jsonpath"
import { MergeStates, OpenApiTypes, rules } from "../rule"
import { getResolvedJson, getResolvedResponseSchema } from "./utilities/rules-helper"
export const DefaultErrorResponseSchema: string = "DefaultErrorResponseSchema"
rules.push({
id: "R4007",
name: DefaultErrorResponseSchema,
severity: "error",
category: "ARMViolation",
mergeState: MergeStates.individual,
openapiType: OpenApiTypes.arm,
appliesTo_JsonQuery: "$",
async *asyncRun(doc, node, path) {
const msg: string =
"the default error response schema does not correspond to the schema documented at https://github.com/Azure/azure-resource-manager-rpc/blob/master/v1.0/common-api-details.md#error-response-content."
const resolvedDoc = await getResolvedJson(doc)
if (!resolvedDoc) {
return
}
for (const n of nodes(doc, "$.paths.*.*.responses")) {
const response: any = n.value
if (response.default && response.default.schema) {
const paths = n.path.concat(["default"])
const pathExpression = stringify(paths.concat(["schema"]))
const schema: any = await getResolvedResponseSchema(nodes(resolvedDoc, pathExpression)[0].value)
/*
* the schema should match below structure:
{
"error":{
"code":"error code",
"message":"error message"
...
}
}
*/
if (!schema || !schema.error || !schema.error.code || !schema.error.message) {
yield { message: `${msg}`, location: paths }
}
}
}
}
})