-
Notifications
You must be signed in to change notification settings - Fork 43
/
rules-helper.ts
139 lines (126 loc) · 4.42 KB
/
rules-helper.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
const $RefParser = require("@apidevtools/json-schema-ref-parser")
import { apply, nodes, stringify } from "jsonpath"
import { JsonPath } from "../../../jsonrpc/types"
import { resolveNestedSchema } from "./resolveNestedSchema"
const matchAll = require("string.prototype.matchall")
export function getSuccessfulResponseSchema(node, doc): any {
const responses = Object.keys(node.responses)
const response = getMostSuccessfulResponseKey(responses)
return getResponseSchema(node.responses[response], doc)
}
export function getMostSuccessfulResponseKey(responses: string[]): string {
let response: string = "default"
if (responses.includes("200")) {
response = "200"
} else {
const twoHundreds = []
responses.forEach(function(value) {
if (value.startsWith("2")) {
twoHundreds.push(value)
}
})
if (twoHundreds.length > 0) {
response = twoHundreds[0]
}
}
return response
}
export function getResponseSchema(response: object, doc): any {
const schema = (response as any).schema
if (schema === undefined || schema === null) {
return
}
if ("$ref" in schema) {
const schemaRef = schema.$ref
const schemaPath: string[] = (schemaRef as string).split("/")
const schemaProperties = doc.definitions[schemaPath[2]].properties
return schemaProperties
}
return schema.properties
}
/**
* return a dereferenced json, also will resolve the circular $refs
*/
export async function getResolvedJson(doc: any): Promise<object | undefined> {
try {
const parser = new $RefParser()
const docCopy = JSON.parse(JSON.stringify(doc))
/*
* remove x-ms-examples
*/
apply(docCopy, `$..['x-ms-examples']`, e => null)
return await parser.dereference(docCopy)
} catch (err) {
return
}
}
export async function getResolvedSchemaByPath(schemaPath: JsonPath, doc: any): Promise<object | undefined> {
const resolvedJson = await getResolvedJson(doc)
if (!resolvedJson) {
return undefined
}
const pathExpression = stringify(schemaPath)
const result = nodes(resolvedJson, pathExpression)
if (!result) {
return undefined
}
const schema = result[0].value
return getResolvedResponseSchema(schema)
}
/*
* resolve the `allOf` and `properties` keywords in the schema
* return a flatted json
*/
export async function getResolvedResponseSchema(schema: object): Promise<object | undefined> {
if (!schema) {
return
}
try {
return resolveNestedSchema(schema)
} catch (err) {
return
}
}
export function getAllResourceProvidersFromPath(path: string): string[] {
const resourceProviderRegex: RegExp = new RegExp(/providers\/([\w\.]+)/, "g")
return Array.from(matchAll(path, resourceProviderRegex), m => m[1])
}
export function getAllWordsFromPath(path: string): string[] {
const wordRegex: RegExp = new RegExp(/([\w\.]+)/, "g")
return Array.from(matchAll(path, wordRegex), m => m[1])
}
export function resourceProviderMustPascalCase(resourceProvider: string): boolean {
if (resourceProvider.length === 0) {
return false
}
// refer https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-1.1/141e06ef(v=vs.71)?redirectedfrom=MSDN
const pascalCase: RegExp = new RegExp(`^[A-Z][a-z0-9]+(\.([A-Z]{1,3}[a-z0-9]+)+[A-Z]{0,2})+$`)
return pascalCase.test(resourceProvider)
}
export function resourceTypeMustCamelCase(resourceType: string): boolean {
if (resourceType.length === 0) {
return true
}
const pascalCase: RegExp = new RegExp("^[a-z][a-z0-9]+([A-Z]+[a-z0-9]*)*$")
return pascalCase.test(resourceType)
}
export function isValidOperation(operation: string): boolean {
const validOperations = ["put", "get", "patch", "post", "head", "options", "delete"]
return validOperations.indexOf(operation.toLowerCase()) !== -1
}
export function isValidEnum(node) {
if (!node || !node.type || typeof node.type !== "string") {
return false
}
return ["boolean", "integer", "number", "string"].indexOf(node.type) !== -1
}
export function transformEnum(type: string, enumEntries) {
if (type === "string") {
return enumEntries
}
return enumEntries.map(v => v.toString())
}