-
Notifications
You must be signed in to change notification settings - Fork 311
Expand file tree
/
Copy pathschema.ts
More file actions
194 lines (164 loc) · 4.83 KB
/
Copy pathschema.ts
File metadata and controls
194 lines (164 loc) · 4.83 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/* ============================================================================
* Copyright (c) Palo Alto Networks
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* ========================================================================== */
import { SchemaObject } from "../openapi/types";
function prettyName(schema: SchemaObject, circular?: boolean) {
// Handle enum-only schemas (valid in JSON Schema)
// When enum is present without explicit type, treat as string
if (schema.enum && !schema.type) {
return "string";
}
if (schema.format) {
if (schema.type) {
return `${schema.type}<${schema.format}>`;
}
return schema.format;
}
if (schema.allOf) {
if (typeof schema.allOf[0] === "string") {
// @ts-ignore
if (schema.allOf[0].includes("circular")) {
return schema.allOf[0];
}
}
return "object";
}
if (schema.oneOf) {
return "object";
}
if (schema.anyOf) {
return "object";
}
if (schema.type === "object") {
return schema.xml?.name ?? schema.type;
// return schema.type;
}
if (schema.type === "array") {
return schema.xml?.name ?? schema.type;
// return schema.type;
}
if (schema.title && schema.type) {
return `${schema.title} (${schema.type})`;
}
return schema.title ?? 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:
// - uniqueItems
// - maxProperties
// - minProperties
// - multipleOf
if (!schema) {
return undefined;
}
if (
schema.items &&
schema.minItems === undefined &&
schema.maxItems === undefined
) {
return getQualifierMessage(schema.items);
}
let message = "**Possible values:** ";
let qualifierGroups = [];
if (schema.items && schema.items.enum) {
if (schema.items.enum) {
qualifierGroups.push(
`[${schema.items.enum.map((e) => `\`${e}\``).join(", ")}]`
);
}
}
if (schema.minLength || schema.maxLength) {
let lengthQualifier = "";
let minLength;
let maxLength;
if (schema.minLength && schema.minLength > 1) {
minLength = `\`>= ${schema.minLength} characters\``;
}
if (schema.minLength && schema.minLength === 1) {
minLength = `\`non-empty\``;
}
if (schema.maxLength) {
maxLength = `\`<= ${schema.maxLength} characters\``;
}
if (minLength && !maxLength) {
lengthQualifier += minLength;
}
if (maxLength && !minLength) {
lengthQualifier += maxLength;
}
if (minLength && maxLength) {
lengthQualifier += `${minLength} and ${maxLength}`;
}
qualifierGroups.push(lengthQualifier);
}
if (
schema.minimum != null ||
schema.maximum != null ||
typeof schema.exclusiveMinimum === "number" ||
typeof schema.exclusiveMaximum === "number"
) {
let minmaxQualifier = "";
let minimum;
let maximum;
if (typeof schema.exclusiveMinimum === "number") {
minimum = `\`> ${schema.exclusiveMinimum}\``;
} else if (schema.minimum != null && !schema.exclusiveMinimum) {
minimum = `\`>= ${schema.minimum}\``;
} else if (schema.minimum != null && schema.exclusiveMinimum === true) {
minimum = `\`> ${schema.minimum}\``;
}
if (typeof schema.exclusiveMaximum === "number") {
maximum = `\`< ${schema.exclusiveMaximum}\``;
} else if (schema.maximum != null && !schema.exclusiveMaximum) {
maximum = `\`<= ${schema.maximum}\``;
} else if (schema.maximum != null && schema.exclusiveMaximum === true) {
maximum = `\`< ${schema.maximum}\``;
}
if (minimum && !maximum) {
minmaxQualifier += minimum;
}
if (maximum && !minimum) {
minmaxQualifier += maximum;
}
if (minimum && maximum) {
minmaxQualifier += `${minimum} and ${maximum}`;
}
qualifierGroups.push(minmaxQualifier);
}
if (schema.pattern) {
qualifierGroups.push(
`Value must match regular expression \`${schema.pattern}\``
);
}
// Check if discriminator mapping
const discriminator = schema as any;
if (discriminator.mapping) {
const values = Object.keys(discriminator.mapping);
qualifierGroups.push(`[${values.map((e) => `\`${e}\``).join(", ")}]`);
}
if (schema.enum) {
qualifierGroups.push(`[${schema.enum.map((e) => `\`${e}\``).join(", ")}]`);
}
if (schema.minItems) {
qualifierGroups.push(`\`>= ${schema.minItems}\``);
}
if (schema.maxItems) {
qualifierGroups.push(`\`<= ${schema.maxItems}\``);
}
if (qualifierGroups.length === 0) {
return undefined;
}
return message + qualifierGroups.join(", ");
}