-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test-d.ts
318 lines (263 loc) · 7.75 KB
/
index.test-d.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// tslint:disable:no-implicit-dependencies
import { expectAssignable, expectType } from 'tsd'
import { Compute, JSONValue, Schema } from '.'
type WithAdditional<T, Additional = JSONValue> = Compute<
{ [_: string]: Additional } & T
>
// Empty and boolean schemas:
declare const empty: Schema<{}>
expectType<JSONValue>(empty)
declare const notDefined: Schema<undefined>
expectType<JSONValue>(notDefined)
declare const truthy: Schema<true>
expectType<JSONValue>(truthy)
declare const falsey: Schema<false>
expectType<never>(falsey)
// TODO handle alternative negation schemas:
// declare const notAny: Schema<{ not: {} }>
// expectType<never>(notAny)
// declare const notTruthy: Schema<{ not: true }>
// expectType<never>(notTruthy)
// Basic schemas:
declare const basicString: Schema<{ type: 'string' }>
expectType<string>(basicString)
declare const basicNumber: Schema<{ type: 'number' }>
expectType<number>(basicNumber)
declare const basicInteger: Schema<{ type: 'integer' }>
expectType<number>(basicInteger)
declare const basicNull: Schema<{ type: 'null' }>
expectType<null>(basicNull)
declare const basicBoolean: Schema<{ type: 'boolean' }>
expectType<boolean>(basicBoolean)
declare const multipleBasic1: Schema<{ type: ['string'] }>
expectType<string>(multipleBasic1)
declare const multipleBasic2: Schema<{ type: ['string', 'null', 'boolean'] }>
expectType<string | null | boolean>(multipleBasic2)
// Enum and const schemas:
declare const enumBasic: Schema<{ enum: ['some', 'other', 3] }>
expectType<'some' | 'other' | 3>(enumBasic)
declare const enumTypeOverride: Schema<{ enum: [1, 2, '3']; type: 'string' }>
expectType<'3'>(enumTypeOverride)
declare const constBasic: Schema<{ const: false }>
expectType<false>(constBasic)
declare const constComplex: Schema<{ const: { x: [1, 2] } }>
expectType<{ x: [1, 2] }>(constComplex)
// Negation schemas with basic types:
declare const basicNot: Schema<{ not: { type: 'string' } }>
expectType<number | boolean | null>(basicNot)
declare const basicNotNumber: Schema<{ not: { type: 'number' } }>
expectType<string | boolean | null>(basicNotNumber)
declare const basicNotInteger: Schema<{ not: { type: 'integer' } }>
expectType<string | boolean | null>(basicNotInteger)
declare const basicNotMultiple: Schema<{ not: { type: ['null', 'boolean'] } }>
expectType<string | number>(basicNotMultiple)
// Array schemas:
declare const arrayAny: Schema<{ type: 'array' }>
expectType<JSONValue[]>(arrayAny)
declare const arrayBasic: Schema<{ type: 'array'; items: { type: 'boolean' } }>
expectType<boolean[]>(arrayBasic)
declare const arrayMultipleBasic: Schema<{
type: 'array'
items: { type: ['string', 'boolean'] }
}>
expectType<Array<string | boolean>>(arrayMultipleBasic)
// Tuple schemas:
declare const tuple: Schema<{
type: 'array'
items: [{ type: 'string' }, { type: 'null' }, { const: 'asd' }]
}>
expectType<
[] | [string] | [string, null] | [string, null, 'asd', ...JSONValue[]]
>(tuple)
declare const tupleAdditional: Schema<{
type: 'array'
additionalItems: { type: 'string' }
items: [{ type: 'string' }, { type: 'null' }]
}>
expectType<[] | [string] | [string, null, ...string[]]>(tupleAdditional)
declare const tupleMaxLength: Schema<{
type: 'array'
items: [{ type: 'string' }, { type: 'null' }, true, true, true, true]
}>
expectType<
| []
| [string]
| [string, null]
| [string, null, JSONValue]
| [string, null, JSONValue, JSONValue]
| [string, null, JSONValue, JSONValue, JSONValue]
| [string, null, JSONValue, JSONValue, JSONValue, JSONValue, ...JSONValue[]]
>(tupleMaxLength)
declare const tupleNoAdditional: Schema<{
type: 'array'
additionalItems: false
items: [{ type: 'string' }, { type: 'null' }]
}>
expectType<[] | [string] | [string, null]>(tupleNoAdditional)
// $id references:
declare const idSchema: Schema<{
definitions: {
name: {
$id: '#name'
type: 'string'
}
}
$ref: '#name'
}>
expectType<string>(idSchema)
declare const idArraySchema: Schema<{
definitions: {
level: {
$id: '#level'
enum: [1, 2]
}
}
type: 'array'
items: {
$ref: '#level'
}
}>
expectType<Array<1 | 2>>(idArraySchema)
declare const idRecursiveSchema: Schema<{
definitions: {
person: {
$id: '#person'
type: 'object'
properties: {
child: {
$ref: '#person'
}
}
additionalProperties: false
}
}
type: 'object'
properties: {
person: {
$ref: '#person'
}
}
additionalProperties: false
}>
declare interface Person1 {
child?: Person1 | undefined
}
expectAssignable<{ person?: Person1 }>(idRecursiveSchema) // TODO: check with stricter `expectType`
// Object schemas:
declare const emptyObjectSchema: Schema<{
type: 'object'
}>
expectType<{ [_: string]: JSONValue }>(emptyObjectSchema)
declare const emptyPropertiesSchema: Schema<{
properties: {}
}>
expectType<{ [_: string]: JSONValue }>(emptyPropertiesSchema)
declare const emptyNoAdditionalObjectSchema: Schema<{
type: 'object'
properties: {}
additionalProperties: false
}>
expectType<{}>(emptyNoAdditionalObjectSchema)
declare const basicObjectSchema: Schema<{
type: 'object'
properties: { x: { type: 'string' }; y: { type: 'number' } }
required: ['x']
}>
expectType<WithAdditional<{ x: string; y?: number }>>(basicObjectSchema)
declare const basicObjectNoAdditionalSchema: Schema<{
type: 'object'
properties: { x: { type: 'string' }; y: { type: 'number' } }
required: ['x']
additionalProperties: false
}>
expectType<{ x: string; y?: number }>(basicObjectNoAdditionalSchema)
declare const nestedObjectSchema: Schema<{
type: 'object'
properties: {
x: {
properties: { y: { type: 'string' } }
}
}
additionalProperties: false
}>
expectType<{ x?: WithAdditional<{ y?: string }> }>(nestedObjectSchema)
// anyOf:
declare const anyOfSchema: Schema<{
anyOf: [{ type: 'string' }, { type: 'number' }]
}>
expectType<string | number>(anyOfSchema)
declare const anyOfArraySchema: Schema<{
type: 'array'
items: {
anyOf: [{ type: 'string' }, { type: 'array'; items: { type: 'number' } }]
}
}>
expectType<Array<string | number[]>>(anyOfArraySchema)
declare const anyOfObjectSchema: Schema<{
type: 'object'
properties: {
x: {
anyOf: [{ type: 'string' }, { type: 'array'; items: { type: 'number' } }]
}
}
required: ['x']
}>
expectType<WithAdditional<{ x: string | number[] }>>(anyOfObjectSchema)
// allOf:
declare const allOfSchema: Schema<{
allOf: [{ type: 'string' }, { type: 'number' }]
}>
expectType<string & number>(allOfSchema)
declare const allOfObjectSchema: Schema<{
allOf: [
{ properties: { x: { type: 'string' } }; additionalProperties: false },
{ properties: { y: { type: 'number' } }; additionalProperties: false }
]
}>
expectType<{ x?: string; y?: number }>(allOfObjectSchema)
declare const allOfOverrideAdditionalPropertiesSchema: Schema<{
allOf: [
{ properties: { x: { type: 'string' } }; additionalProperties: false },
{ properties: { y: { type: 'number' } } }
]
}>
expectType<{ x?: string; y?: number }>(allOfOverrideAdditionalPropertiesSchema)
// Complex objects:
declare const complexSchema: Schema<{
definitions: {
address: {
$id: '#address'
properties: {
city: { type: 'string' }
country: { type: 'string'; enum: ['FI', 'SV', 'NO'] }
}
required: ['city', 'country']
}
}
allOf: [
{ $ref: '#address' },
{
type: 'object'
properties: {
id: {
anyOf: [
{ type: 'number' },
{ type: 'array'; items: { type: 'number' } }
]
}
name: {
type: ['string', 'null']
}
}
required: ['name']
}
]
}>
expectType<
WithAdditional<{
city: string
country: 'FI' | 'SV' | 'NO'
name: string | null
id?: number | number[]
}>
>(complexSchema)