-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.ts
537 lines (475 loc) · 13 KB
/
schema.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/**
* `Schema` is a TypeScript representation of a correct JSON Typedef schema.
*
* The JSON Typedef specification allows schemas to take on one of eight forms.
* Each of those forms has its own type in this module; `Schema` is simply a union
* of each of those eight types.
*/
export type Schema =
| SchemaFormEmpty
| SchemaFormRef
| SchemaFormType
| SchemaFormEnum
| SchemaFormElements
| SchemaFormProperties
| SchemaFormValues
| SchemaFormDiscriminator;
/**
* `SchemaFormEmpty` represents schemas of the empty form.
*/
export type SchemaFormEmpty = SharedFormProperties;
/**
* `SchemaFormRef` represents schemas of the ref form.
*/
export type SchemaFormRef = SharedFormProperties & {
ref: string;
};
/**
* `SchemaFormType` represents schemas of the type form.
*/
export type SchemaFormType = SharedFormProperties & {
type: Type;
};
/**
* `Type` represents the legal values of the "type" keyword in JSON Typedef.
*/
export type Type =
| "boolean"
| "float32"
| "float64"
| "int8"
| "uint8"
| "int16"
| "uint16"
| "int32"
| "uint32"
| "string"
| "timestamp";
/**
* `SchemaFormEnum` represents schemas of the enum form.
*/
export type SchemaFormEnum = SharedFormProperties & {
enum: string[];
};
/**
* `SchemaFormElements` represents schemas of the elements form.
*/
export type SchemaFormElements = SharedFormProperties & {
elements: Schema;
};
/**
* `SchemaFormProperties` represents schemas of the properties form.
*/
export type SchemaFormProperties =
& SharedFormProperties
& (
| {
properties?: { [name: string]: Schema };
optionalProperties: { [name: string]: Schema };
additionalProperties?: boolean;
}
| {
properties: { [name: string]: Schema };
optionalProperties?: { [name: string]: Schema };
additionalProperties?: boolean;
}
);
/**
* `SchemaFormValues` represents schemas of the values form.
*/
export type SchemaFormValues = SharedFormProperties & {
values: Schema;
};
/**
* `SchemaFormDiscriminator` represents schemas of the discriminator form.
*/
export type SchemaFormDiscriminator = SharedFormProperties & {
discriminator: string;
mapping: { [name: string]: Schema };
};
/**
* `SharedFormProperties` contains the properties shared among all schema forms.
*/
interface SharedFormProperties {
definitions?: { [definition: string]: Schema };
metadata?: { [name: string]: unknown };
nullable?: boolean;
}
/**
* `isEmptyForm` checks whether some `Schema` is of the empty form.
*
* @param schema The schema to validate
*/
export function isEmptyForm(schema: Schema): schema is SchemaFormEmpty {
// deno-lint-ignore no-unused-vars
const { definitions, nullable, metadata, ...rest } = schema;
return Object.keys(rest).length === 0;
}
/**
* `isRefForm` checks whether some `Schema` is of the ref form.
*
* @param schema The schema to validate
*/
export function isRefForm(schema: Schema): schema is SchemaFormRef {
return "ref" in schema;
}
/**
* `isTypeForm` checks whether some `Schema` is of the type form.
*
* @param schema The schema to validate
*/
export function isTypeForm(schema: Schema): schema is SchemaFormType {
return "type" in schema;
}
/**
* `isEnumForm` checks whether some `Schema` is of the enum form.
*
* @param schema The schema to validate
*/
export function isEnumForm(schema: Schema): schema is SchemaFormEnum {
return "enum" in schema;
}
/**
* `isElementsForm` checks whether some `Schema` is of the elements form.
*
* @param schema The schema to validate
*/
export function isElementsForm(schema: Schema): schema is SchemaFormElements {
return "elements" in schema;
}
/**
* `isPropertiesForm` checks whether some `Schema` is of the properties form.
*
* @param schema The schema to validate
*/
export function isPropertiesForm(
schema: Schema,
): schema is SchemaFormProperties {
return "properties" in schema || "optionalProperties" in schema;
}
/**
* `isPropertiesForm` checks whether some `Schema` is of the values form.
*
* @param schema The schema to validate
*/
export function isValuesForm(schema: Schema): schema is SchemaFormValues {
return "values" in schema;
}
/**
* `isDiscriminatorForm` checks whether some `Schema` is of the values form.
*
* @param schema The schema to validate
*/
export function isDiscriminatorForm(
schema: Schema,
): schema is SchemaFormDiscriminator {
return "discriminator" in schema;
}
/**
* `isValidSchema` checks whether some `Schema` is correct, according to the syntax
* rules of JSON Typedef.
*
* In particular, `isValidSchema` verifies that:
*
* 1. The schema does not have any non-root definitions,
* 2. All references point to actually-existing definitions,
* 3. All enums are non-empty, and do not contain duplicates,
* 4. The `properties` and `optionalProperties` of a schema never share
* properties,
* 5. All schemas in `mapping` are of the properties form,
* 6. Schemas in `mapping` never re-specify the `discriminator` property
*
* If an object returned from `JSON.parse` passes both [isSchema](#isSchema) and
* [isValidSchema](#isValidSchema), then it is a correct JSON Typedef schema.
*
* @param schema The schema to validate
* @param root The schema to consider as the "root" schema. If undefined,
* `schema` will be used as the root. This is usually what you want to do.
*/
export function isValidSchema(schema: Schema, root?: Schema): boolean {
if (root === undefined) {
root = schema;
}
if (schema.definitions !== undefined) {
if (root !== schema) {
return false;
}
for (const subSchema of Object.values(schema.definitions)) {
if (!isValidSchema(subSchema, root)) {
return false;
}
}
}
if (isRefForm(schema)) {
if (!(schema.ref in (root.definitions || {}))) {
return false;
}
}
if (isEnumForm(schema)) {
if (schema.enum.length === 0) {
return false;
}
if (schema.enum.length !== new Set(schema.enum).size) {
return false;
}
}
if (isElementsForm(schema)) {
return isValidSchema(schema.elements, root);
}
if (isPropertiesForm(schema)) {
for (const subSchema of Object.values(schema.properties || {})) {
if (!isValidSchema(subSchema, root)) {
return false;
}
}
for (const subSchema of Object.values(schema.optionalProperties || {})) {
if (!isValidSchema(subSchema, root)) {
return false;
}
}
for (const key of Object.keys(schema.properties || {})) {
if (key in (schema.optionalProperties || {})) {
return false;
}
}
}
if (isValuesForm(schema)) {
return isValidSchema(schema.values, root);
}
if (isDiscriminatorForm(schema)) {
for (const subSchema of Object.values(schema.mapping)) {
if (!isValidSchema(subSchema, root) || !isPropertiesForm(subSchema)) {
return false;
}
if (subSchema.nullable) {
return false;
}
if (schema.discriminator in (subSchema.properties || {})) {
return false;
}
if (schema.discriminator in (subSchema.optionalProperties || {})) {
return false;
}
}
}
return true;
}
// Index of valid form "signatures" -- i.e., combinations of the presence of the
// keywords (in order):
//
// ref type enum elements properties optionalProperties additionalProperties
// values discriminator mapping
//
// The keywords "definitions", "nullable", and "metadata" are not included here,
// because they would restrict nothing.
const VALID_FORMS = [
// Empty form
[false, false, false, false, false, false, false, false, false, false],
// Ref form
[true, false, false, false, false, false, false, false, false, false],
// Type form
[false, true, false, false, false, false, false, false, false, false],
// Enum form
[false, false, true, false, false, false, false, false, false, false],
// Elements form
[false, false, false, true, false, false, false, false, false, false],
// Properties form -- properties or optional properties or both, and never
// additional properties on its own
[false, false, false, false, true, false, false, false, false, false],
[false, false, false, false, false, true, false, false, false, false],
[false, false, false, false, true, true, false, false, false, false],
[false, false, false, false, true, false, true, false, false, false],
[false, false, false, false, false, true, true, false, false, false],
[false, false, false, false, true, true, true, false, false, false],
// Values form
[false, false, false, false, false, false, false, true, false, false],
// Discriminator form
[false, false, false, false, false, false, false, false, true, true],
];
// List of valid values that the "type" keyboard may take on.
const VALID_TYPES = [
"boolean",
"float32",
"float64",
"int8",
"uint8",
"int16",
"uint16",
"int32",
"uint32",
"string",
"timestamp",
];
/**
* `isSchema` checks whether some piece of JSON data has the shape of a JSON
* Typedef Schema.
*
* This function only looks at the "shape" of data: it just makes sure all
* property names and types are valid, and that the data takes on one of the
* eight JSON Typedef forms.
*
* If an object returned from `JSON.parse` passes both [isSchema](#isSchema) and
* [isValidSchema](#isValidSchema), then it is a correct JSON Typedef schema.
*
* @param data The data to check
*/
export function isSchema(data: unknown): data is Schema {
if (typeof data !== "object" || Array.isArray(data) || data === null) {
return false;
}
// TypeScript does not let us coerce `{}` into `{ [index: string]: unknown }`.
// At the time of writing, it's unclear why this is the case, nor under what
// circumstances such an coercion would be wrong.
//
// So we work around the compiler here.
// deno-lint-ignore no-explicit-any
const obj: { [index: string]: unknown } = data as any;
const {
definitions = undefined,
nullable = undefined,
metadata = undefined,
ref = undefined,
type = undefined,
enum: enum_ = undefined,
elements = undefined,
properties = undefined,
optionalProperties = undefined,
additionalProperties = undefined,
values = undefined,
discriminator = undefined,
mapping = undefined,
...rest
} = obj;
const formSignature = [
ref !== undefined,
type !== undefined,
enum_ !== undefined,
elements !== undefined,
properties !== undefined,
optionalProperties !== undefined,
additionalProperties !== undefined,
values !== undefined,
discriminator !== undefined,
mapping !== undefined,
];
let formOk = false;
for (const validForm of VALID_FORMS) {
formOk = formOk ||
validForm.every((value, index) => value === formSignature[index]);
}
if (!formOk) {
return false;
}
if (definitions !== undefined) {
if (
typeof definitions !== "object" ||
Array.isArray(definitions) ||
definitions === null
) {
return false;
}
for (const value of Object.values(definitions)) {
if (!isSchema(value)) {
return false;
}
}
}
if (nullable !== undefined) {
if (typeof nullable !== "boolean") {
return false;
}
}
if (metadata !== undefined) {
if (
typeof metadata !== "object" ||
Array.isArray(metadata) ||
metadata === null
) {
return false;
}
}
if (ref !== undefined) {
if (typeof ref !== "string") {
return false;
}
}
if (type !== undefined) {
if (typeof type !== "string" || !VALID_TYPES.includes(type)) {
return false;
}
}
if (enum_ !== undefined) {
if (!Array.isArray(enum_)) {
return false;
}
if (!enum_.every((elem) => typeof elem === "string")) {
return false;
}
}
if (elements !== undefined) {
if (!isSchema(elements)) {
return false;
}
}
if (properties !== undefined) {
if (
typeof properties !== "object" ||
Array.isArray(properties) ||
properties === null
) {
return false;
}
for (const value of Object.values(properties)) {
if (!isSchema(value)) {
return false;
}
}
}
if (optionalProperties !== undefined) {
if (
typeof optionalProperties !== "object" ||
Array.isArray(optionalProperties) ||
optionalProperties === null
) {
return false;
}
for (const value of Object.values(optionalProperties)) {
if (!isSchema(value)) {
return false;
}
}
}
if (additionalProperties !== undefined) {
if (typeof additionalProperties !== "boolean") {
return false;
}
}
if (values !== undefined) {
if (!isSchema(values)) {
return false;
}
}
if (discriminator !== undefined) {
if (typeof discriminator !== "string") {
return false;
}
}
if (mapping !== undefined) {
if (
typeof mapping !== "object" ||
Array.isArray(mapping) ||
mapping === null
) {
return false;
}
for (const value of Object.values(mapping)) {
if (!isSchema(value)) {
return false;
}
}
}
if (Object.keys(rest).length !== 0) {
return false;
}
return true;
}