UNPKG

2.94 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.getTypesOfSchema = void 0;
11const interface_1 = require("../interface");
12const allTypes = ['string', 'integer', 'number', 'object', 'array', 'boolean', 'null'];
13function getTypesOfSchema(schema) {
14 if (!schema) {
15 return new Set();
16 }
17 if (schema === true) {
18 return new Set(allTypes);
19 }
20 let potentials;
21 if (typeof schema.type === 'string') {
22 potentials = new Set([schema.type]);
23 }
24 else if (Array.isArray(schema.type)) {
25 potentials = new Set(schema.type);
26 }
27 else if (interface_1.isJsonArray(schema.enum)) {
28 potentials = new Set();
29 // Gather the type of each enum values, and use that as a starter for potential types.
30 for (const v of schema.enum) {
31 switch (typeof v) {
32 case 'string':
33 case 'number':
34 case 'boolean':
35 potentials.add(typeof v);
36 break;
37 case 'object':
38 if (Array.isArray(v)) {
39 potentials.add('array');
40 }
41 else if (v === null) {
42 potentials.add('null');
43 }
44 else {
45 potentials.add('object');
46 }
47 break;
48 }
49 }
50 }
51 else {
52 potentials = new Set(allTypes);
53 }
54 if (interface_1.isJsonObject(schema.not)) {
55 const notTypes = getTypesOfSchema(schema.not);
56 potentials = new Set([...potentials].filter((p) => !notTypes.has(p)));
57 }
58 if (Array.isArray(schema.allOf)) {
59 for (const sub of schema.allOf) {
60 const types = getTypesOfSchema(sub);
61 potentials = new Set([...types].filter((t) => potentials.has(t)));
62 }
63 }
64 if (Array.isArray(schema.oneOf)) {
65 let options = new Set();
66 for (const sub of schema.oneOf) {
67 const types = getTypesOfSchema(sub);
68 options = new Set([...options, ...types]);
69 }
70 potentials = new Set([...options].filter((o) => potentials.has(o)));
71 }
72 if (Array.isArray(schema.anyOf)) {
73 let options = new Set();
74 for (const sub of schema.anyOf) {
75 const types = getTypesOfSchema(sub);
76 options = new Set([...options, ...types]);
77 }
78 potentials = new Set([...options].filter((o) => potentials.has(o)));
79 }
80 if (schema.properties) {
81 potentials.add('object');
82 }
83 else if (schema.items) {
84 potentials.add('array');
85 }
86 return potentials;
87}
88exports.getTypesOfSchema = getTypesOfSchema;