-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
270 lines (229 loc) · 8.3 KB
/
index.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
export namespace t {
export interface BaseType {
$id?: string;
$schema?: string;
$ref?: string;
$comment?: string;
type?: 'string' | 'number' | 'integer' | 'boolean' | 'null' | 'array' | 'object';
enum?: Array<string | number | boolean | null | object | any[]>;
const?: any;
title?: string;
description?: string;
definitions?: { [K: string]: Schema }
default?: string | number | boolean | null | object | any[];
// MUST be a non-empty array. Validates successfully against all schemas
allOf?: Schema[];
// MUST be a non-empty array. Validates successfully against at least one schema
anyOf?: Schema[];
// MUST be a non-empty array. Validates successfully against exactly one schema
oneOf?: Schema[];
// MUST be a valid JSON Schema
not?: Schema;
}
export interface NumberType<E extends number=number> extends BaseType {
type: 'number';
// strictly greater than 0
multipleOf?: number;
maximum?: number;
exclusiveMaximum?: number;
minimum?: number;
exclusiveMinimum?: number;
enum?: Array<E>;
default?: number;
}
export interface IntegerType<E extends number=number> extends BaseType {
type: 'integer';
// strictly greater than 0
multipleOf?: number;
maximum?: number;
exclusiveMaximum?: number;
minimum?: number;
exclusiveMinimum?: number;
enum?: Array<E>;
default?: number;
}
export interface StringType<E extends string=string> extends BaseType {
type: 'string';
// a non-negative integer
maxLength?: number;
// a non-negative integer
minLength?: number;
pattern?: string;
enum?: Array<E>;
default?: string;
}
export interface BooleanType<E extends boolean=boolean> extends BaseType {
type: 'boolean';
enum?: Array<E>;
default?: boolean;
}
export interface NullType<E extends null=null> extends BaseType {
type: 'null';
enum?: Array<E>;
default?: null;
}
export interface ArrayType<T extends Schema> extends BaseType {
type: 'array';
// Omitting this keyword has the same behavior as an empty schema.
items?: T;
// The value of this keyword MUST be a non-negative integer.
maxItems?: number;
// The value of this keyword MUST be a non-negative integer.
minItems?: number;
uniqueItems?: boolean;
// FIXME: An array instance is valid against "contains" if at least one of its elements is valid against the given schema.
// contains?: Schema;
default?: any[];
}
export interface TupleType<T extends Schema[]> extends BaseType {
type: 'array';
// Omitting this keyword has the same behavior as an empty schema.
items?: T;
// The value of this keyword MUST be a non-negative integer.
maxItems?: number;
// The value of this keyword MUST be a non-negative integer.
minItems?: number;
uniqueItems?: boolean;
// FIXME: An array instance is valid against "contains" if at least one of its elements is valid against the given schema.
// contains?: Schema;
default?: any[];
}
export interface AnyOfType<T extends Schema[]> extends BaseType {
anyOf: T;
}
export interface OneOfType<T extends Schema[]> extends BaseType {
oneOf: T;
}
export interface AllOfType<T extends Schema[]> extends BaseType {
allOf: T;
}
export type ObjectProperties = {[key: string]: Schema}
export interface ObjectType<T extends ObjectProperties, AdditionalProperties extends Schema=Schema> extends BaseType {
type: 'object';
// Omitting this keyword has the same behavior as an empty object.
properties?: T;
maxProperties?: number;
minProperties?: number;
// Omitting this keyword has the same behavior as an empty array.
// This is broken in TS 4.3 and above ;(
// required?: Array<keyof T>;
required?: string[];
additionalProperties?: AdditionalProperties;
// FIXME: dependencies?: {};
propertyNames?: Schema;
default?: object;
}
export interface RefType<T extends Schema> extends BaseType {
$ref: string;
}
export type Schema = boolean
| StringType
| NumberType
| IntegerType
| BooleanType
| NullType
| AnyOfType<Schema[]>
| OneOfType<Schema[]>
| OneOfType<Schema[]>
| ArrayType<Schema>
| TupleType<Schema[]>
| ObjectType<ObjectProperties>
| RefType<Schema>;
export interface ArrayTSType<U extends Schema> extends Array<TSType<U>> {}
// FIXME: why can't use MapToTSType<T> in TSType
type MapToTSType<T> = { [K in keyof T]: TSType<T[K]> };
type TSTypeNoRef<T> = T extends true ? any
: T extends false ? never
: T extends StringType<infer U> ? U
: T extends NumberType<infer U> ? U
: T extends IntegerType<infer U> ? U
: T extends BooleanType<infer U> ? U
: T extends NullType<infer U> ? U
: T extends ArrayType<infer U> ? ArrayTSType<U>
: T extends TupleType<infer U> ? MapToTSType<U>
: T extends ObjectType<infer U> ? MapToTSType<U>
// tuple to union. See https://github.com/Microsoft/TypeScript/issues/13298#issuecomment-423385929
: T extends AnyOfType<infer U> ? { [K in keyof U]: TSType<U[K]> }[number]
: T extends OneOfType<infer U> ? { [K in keyof U]: TSType<U[K]> }[number]
// FIXME: how to convert tuple to intersection type?
: T extends AllOfType<infer U> ? { [K in keyof U]: TSType<U[K]> }[number]
: never;
export type TSType<T> = T extends RefType<infer S> ? TSTypeNoRef<S> : TSTypeNoRef<T>
}
export namespace s {
export function object<T extends t.ObjectProperties>(body?: Partial<t.ObjectType<T>>): t.ObjectType<T> {
return {
...body,
'type': 'object',
};
}
export function number<E extends number=number>(body?: Partial<t.NumberType<E>>): t.NumberType<E> {
return {
...body,
'type': 'number',
};
}
export function integer<E extends number=number>(body?: Partial<t.IntegerType<E>>): t.IntegerType<E> {
return {
...body,
'type': 'integer',
};
}
export function boolean<E extends boolean=boolean>(body?: Partial<t.BooleanType<E>>): t.BooleanType<E> {
return {
...body,
'type': 'boolean',
};
}
export function string<E extends string=string>(body?: Partial<t.StringType<E>>): t.StringType<E> {
return {
...body,
'type': 'string',
};
}
export function array<T extends t.Schema>(body?: Partial<t.ArrayType<T>>): t.ArrayType<T> {
return {
...body,
'type': 'array',
};
}
export function tuple<T extends t.Schema[]>(body?: Partial<t.TupleType<T>>): t.TupleType<T> {
return {
...body,
'type': 'array',
};
}
// works with tuple
export function items<T extends t.Schema[]>(...schemas: T): T {
return schemas;
}
export function ref<T extends t.Schema>(path: string): t.RefType<T> {
return {
'$ref': path,
};
}
export function anyOf<T extends t.Schema[]>(...schemas: T): t.AnyOfType<T> {
if (schemas.length < 1) {
throw new Error('anyOf must be a non-empty array');
}
return {
'anyOf': schemas,
};
}
export function oneOf<T extends t.Schema[]>(...schemas: T): t.OneOfType<T> {
if (schemas.length < 1) {
throw new Error('oneOf must be a non-empty array');
}
return {
'oneOf': schemas,
};
}
export function allOf<T extends t.Schema[]>(...schemas: T): t.AllOfType<T> {
if (schemas.length < 1) {
throw new Error('allOf must be a non-empty array');
}
return {
'allOf': schemas,
};
}
}