-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathschema.ts
More file actions
128 lines (108 loc) · 3.33 KB
/
schema.ts
File metadata and controls
128 lines (108 loc) · 3.33 KB
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
/* ============================================================================
* Copyright (c) Cloud Annotations
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* ========================================================================== */
import { SchemaObject, PrimitiveSchemaObjectTypes } from "../openapi/types";
function typeToString(
type: PrimitiveSchemaObjectTypes | PrimitiveSchemaObjectTypes[] | undefined
): string {
if (type === undefined) {
return "schema.type is not defined";
}
if (type instanceof Array) {
return type.reduce((acc, cur) => (acc ? `${acc} | ${cur}` : cur), "");
}
return type;
}
function prettyName(schema: SchemaObject, circular?: boolean) {
if (schema.$ref) {
return schema.$ref.replace("#/components/schemas/", "") + circular
? " (circular)"
: "";
}
if (schema.format) {
return schema.format;
}
if (schema.allOf) {
return "object";
}
if (schema.type === "object") {
return schema.xml?.name ?? schema.type;
}
return schema.title ?? typeToString(schema.type);
}
export function getSchemaName(
schema: SchemaObject,
circular?: boolean
): string {
if (schema.items) {
return prettyName(schema.items, circular) + "[]";
}
return prettyName(schema, circular) ?? "";
}
export function getQualifierMessage(schema?: SchemaObject): string | undefined {
// TODO:
// - maxItems
// - minItems
// - uniqueItems
// - maxProperties
// - minProperties
// - multipleOf
if (!schema) {
return undefined;
}
if (schema.items) {
return getQualifierMessage(schema.items);
}
let message = "**Possible values:** ";
let qualifierGroups = [];
if (schema.minLength || schema.maxLength) {
let lengthQualifier = "";
if (schema.minLength) {
lengthQualifier += `${schema.minLength} ≤ `;
}
lengthQualifier += "length";
if (schema.maxLength) {
lengthQualifier += ` ≤ ${schema.maxLength}`;
}
qualifierGroups.push(lengthQualifier);
}
if (
schema.minimum ||
schema.maximum ||
typeof schema.exclusiveMinimum === "number" ||
typeof schema.exclusiveMaximum === "number"
) {
let minmaxQualifier = "";
if (typeof schema.exclusiveMinimum === "number") {
minmaxQualifier += `${schema.exclusiveMinimum} < `;
} else if (schema.minimum && !schema.exclusiveMinimum) {
minmaxQualifier += `${schema.minimum} ≤ `;
} else if (schema.minimum && schema.exclusiveMinimum === true) {
minmaxQualifier += `${schema.minimum} < `;
}
minmaxQualifier += "value";
if (typeof schema.exclusiveMaximum === "number") {
minmaxQualifier += ` < ${schema.exclusiveMaximum}`;
} else if (schema.maximum && !schema.exclusiveMaximum) {
minmaxQualifier += ` ≤ ${schema.maximum}`;
} else if (schema.maximum && schema.exclusiveMaximum === true) {
minmaxQualifier += ` < ${schema.maximum}`;
}
qualifierGroups.push(minmaxQualifier);
}
if (schema.pattern) {
qualifierGroups.push(
`Value must match regular expression \`${schema.pattern}\``
);
}
if (schema.enum) {
qualifierGroups.push(`[${schema.enum.map((e) => `\`${e}\``).join(", ")}]`);
}
if (qualifierGroups.length === 0) {
return undefined;
}
return message + qualifierGroups.join(", ");
}