Skip to content

Commit 9648aa3

Browse files
committed
feat(@angular-devkit/core): add enum support to getTypesOfSchema
1 parent 7d782a3 commit 9648aa3

File tree

1 file changed

+24
-1
lines changed
  • packages/angular_devkit/core/src/json/schema

1 file changed

+24
-1
lines changed

packages/angular_devkit/core/src/json/schema/utility.ts

+24-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { JsonObject, isJsonObject } from '../interface';
8+
import { JsonObject, isJsonArray, isJsonObject } from '../interface';
99

1010

1111
const allTypes = ['string', 'integer', 'number', 'object', 'array', 'boolean', 'null'];
@@ -23,6 +23,29 @@ export function getTypesOfSchema(schema: JsonObject | true): Set<string> {
2323
potentials = new Set([schema.type]);
2424
} else if (Array.isArray(schema.type)) {
2525
potentials = new Set(schema.type as string[]);
26+
} else if (isJsonArray(schema.enum)) {
27+
potentials = new Set();
28+
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+
38+
case 'object':
39+
if (Array.isArray(v)) {
40+
potentials.add('array');
41+
} else if (v === null) {
42+
potentials.add('null');
43+
} else {
44+
potentials.add('object');
45+
}
46+
break;
47+
}
48+
}
2649
} else {
2750
potentials = new Set(allTypes);
2851
}

0 commit comments

Comments
 (0)