-
Notifications
You must be signed in to change notification settings - Fork 14
/
builder.ts
221 lines (181 loc) · 5.55 KB
/
builder.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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { FormatName } from "ajv-formats";
import { JSONSchema } from "json-schema-to-typescript";
import { JSONSchemaTypeName } from "json-schema-to-typescript/dist/src/types/JSONSchema";
type SchemaObject = JSONSchema;
type SchemaObjectOrRef = SchemaObject | string;
interface CustomSchemaObject {
kind: "custom";
object: SchemaObject;
optional?: boolean;
}
type PropertyValue = SchemaObject | CustomSchemaObject;
type PropertyBaseOptions = Pick<
JSONSchema,
"title" | "description" | "default"
>;
type ObjectBaseOptions = Pick<
JSONSchema,
"title" | "description" | "default"
>;
type ArrayOptions = PropertyBaseOptions;
type StringOptions = PropertyBaseOptions &
Pick<JSONSchema, "minLength" | "maxLength" | "pattern">;
type NumberOptions = PropertyBaseOptions &
Pick<
JSONSchema,
| "exclusiveMinimum"
| "exclusiveMaximum"
| "maximum"
| "minimum"
| "multipleOf"
>;
type BooleanOptions = PropertyBaseOptions;
// https://ajv.js.org/packages/ajv-formats.html#keywords-to-compare-values-formatmaximum-formatminimum-and-formatexclusivemaximum-formatexclusiveminimum
interface FormatOptions extends StringOptions {
formatMinimum?: string;
formatMaximum?: string;
formatExclusiveMinimum?: string;
formatExclusiveMaximum?: string;
}
export const string = (options: StringOptions = {}): PropertyValue => ({
type: "string",
...options,
});
export const number = (options: NumberOptions = {}): PropertyValue => ({
type: "number",
...options,
});
export const boolean = (options: BooleanOptions = {}): PropertyValue => ({
type: "boolean",
...options,
});
export const any = (options: JSONSchema = {}): PropertyValue => ({
...options
})
export const anonymousData = (options: JSONSchema): PropertyValue => ({
additionalProperties: { type: "string" },
...options,
});
const stringFormat = (format: FormatName) => (
options: FormatOptions = {}
): PropertyValue => {
return {
type: "string",
format,
...(options ?? {}),
};
};
export const date = stringFormat("date");
export const time = stringFormat("time");
export const dateTime = stringFormat("date-time");
export const duration = stringFormat("duration");
export const uri = stringFormat("uri");
export const uriReference = stringFormat("uri-reference");
export const uriTemplate = stringFormat("uri-template");
export const email = stringFormat("email");
export const hostname = stringFormat("hostname");
export const ipv4 = stringFormat("ipv4");
export const ipv6 = stringFormat("ipv6");
export const regex = stringFormat("regex");
export const uuid = stringFormat("uuid");
export const jsonPointer = stringFormat("json-pointer");
export const relativeJsonPointer = stringFormat("relative-json-pointer");
export const object = (
properties: Record<string, PropertyValue>,
options: ObjectBaseOptions = {},
): SchemaObject => {
const required: string[] = [];
const schemaProperties: Record<string, SchemaObject> = {};
Object.entries(properties).forEach(([key, property]) => {
if (property.kind === "custom") {
if (!property.optional) {
required.push("key");
}
schemaProperties[key] = property.object;
} else {
required.push(key);
schemaProperties[key] = property;
}
});
return {
type: "object",
properties: schemaProperties,
required: required.length === 0 ? undefined : required,
...options,
};
};
export const ref = (refName: string): SchemaObject => ({
$ref: `#/definitions/${refName}`,
});
const autoRef = (type: SchemaObjectOrRef): SchemaObject =>
typeof type === "string" ? ref(type) : type;
export const array = (itemType: SchemaObjectOrRef, options: ArrayOptions = {}): SchemaObject => ({
type: "array",
items: autoRef(itemType),
...options,
});
export const map = (itemType: SchemaObjectOrRef): SchemaObject => ({
type: "object",
patternProperties: {
".*": autoRef(itemType),
},
additionalProperties: false,
});
export const nullable = (type: SchemaObjectOrRef): SchemaObject => {
const obj = autoRef(type);
const types: JSONSchemaTypeName[] = [
"string",
"number",
"boolean",
"integer",
];
if (typeof obj.type === "string" && types.includes(obj.type)) {
return {
...obj,
type: [obj.type, "null"],
};
}
return anyOf([obj, { type: "null" }]);
};
export const nillable = (type: SchemaObjectOrRef): SchemaObject =>
optional(nullable(type));
export const optional = (type: SchemaObjectOrRef): CustomSchemaObject => ({
kind: "custom",
object: autoRef(type),
optional: true,
});
export const oneOf = (types: SchemaObjectOrRef[]): SchemaObject => ({
oneOf: types.map(autoRef),
});
export const anyOf = (types: SchemaObjectOrRef[]): SchemaObject => ({
anyOf: types.map(autoRef),
});
export const enumerate = (values: string[]): SchemaObject => ({
type: "string",
enum: values,
});
export const constant = (value: string): SchemaObject => ({
type: "string",
enum: [value],
});
export const compose = (...sources: SchemaObject[]): SchemaObject => {
if (sources === undefined) {
throw new Error(`Sources for 'compose' cannot be undefined`);
}
const properties: SchemaObject["properties"] = {};
const requiredRecord: Record<string, boolean> = {};
sources.forEach((source) => {
Object.assign(properties, source.properties);
if (Array.isArray(source.required)) {
source.required.forEach((key) => {
requiredRecord[key] = true;
});
}
});
const required = Object.keys(requiredRecord);
return {
type: "object",
properties,
required: required.length === 0 ? undefined : required,
};
};