Skip to content

Commit 0b84c1d

Browse files
hanslalexeagle
authored andcommitted
feat(@angular-devkit/core): move findTypes in utility and export
1 parent 4a25d7c commit 0b84c1d

File tree

2 files changed

+71
-50
lines changed

2 files changed

+71
-50
lines changed

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

Lines changed: 4 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,7 @@
77
*/
88
import { JsonObject, JsonValue, isJsonObject } from '../interface';
99
import { JsonPointer } from './interface';
10-
11-
const allTypes = ['string', 'integer', 'number', 'object', 'array', 'boolean', 'null'];
12-
13-
function findTypes(schema: JsonObject): Set<string> {
14-
if (!schema) {
15-
return new Set();
16-
}
17-
18-
let potentials: Set<string>;
19-
if (typeof schema.type === 'string') {
20-
potentials = new Set([schema.type]);
21-
} else if (Array.isArray(schema.type)) {
22-
potentials = new Set(schema.type as string[]);
23-
} else {
24-
potentials = new Set(allTypes);
25-
}
26-
27-
if (isJsonObject(schema.not)) {
28-
const notTypes = findTypes(schema.not);
29-
potentials = new Set([...potentials].filter(p => !notTypes.has(p)));
30-
}
31-
32-
if (Array.isArray(schema.allOf)) {
33-
for (const sub of schema.allOf) {
34-
const types = findTypes(sub as JsonObject);
35-
potentials = new Set([...potentials].filter(p => types.has(p)));
36-
}
37-
}
38-
39-
if (Array.isArray(schema.oneOf)) {
40-
let options = new Set<string>();
41-
for (const sub of schema.oneOf) {
42-
const types = findTypes(sub as JsonObject);
43-
options = new Set([...options, ...types]);
44-
}
45-
potentials = new Set([...potentials].filter(p => options.has(p)));
46-
}
47-
48-
if (Array.isArray(schema.anyOf)) {
49-
let options = new Set<string>();
50-
for (const sub of schema.anyOf) {
51-
const types = findTypes(sub as JsonObject);
52-
options = new Set([...options, ...types]);
53-
}
54-
potentials = new Set([...potentials].filter(p => options.has(p)));
55-
}
56-
57-
return potentials;
58-
}
10+
import { getTypesOfSchema } from './utility';
5911

6012
export function addUndefinedDefaults(
6113
value: JsonValue,
@@ -66,7 +18,7 @@ export function addUndefinedDefaults(
6618
return value;
6719
}
6820

69-
const types = findTypes(schema);
21+
const types = getTypesOfSchema(schema);
7022
if (types.size === 0) {
7123
return value;
7224
}
@@ -110,6 +62,8 @@ export function addUndefinedDefaults(
11062
for (const propName of Object.getOwnPropertyNames(schema.properties)) {
11163
if (propName in newValue) {
11264
continue;
65+
} else if (propName == '$schema') {
66+
continue;
11367
}
11468

11569
// TODO: Does not currently handle more complex schemas (oneOf/anyOf/etc.)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import { JsonObject, isJsonObject } from '../interface';
9+
10+
11+
const allTypes = ['string', 'integer', 'number', 'object', 'array', 'boolean', 'null'];
12+
13+
export function getTypesOfSchema(schema: JsonObject | true): Set<string> {
14+
if (!schema) {
15+
return new Set();
16+
}
17+
if (schema === true) {
18+
return new Set(allTypes);
19+
}
20+
21+
let potentials: Set<string>;
22+
if (typeof schema.type === 'string') {
23+
potentials = new Set([schema.type]);
24+
} else if (Array.isArray(schema.type)) {
25+
potentials = new Set(schema.type as string[]);
26+
} else {
27+
potentials = new Set(allTypes);
28+
}
29+
30+
if (isJsonObject(schema.not)) {
31+
const notTypes = getTypesOfSchema(schema.not);
32+
potentials = new Set([...potentials].filter(p => !notTypes.has(p)));
33+
}
34+
35+
if (Array.isArray(schema.allOf)) {
36+
for (const sub of schema.allOf) {
37+
const types = getTypesOfSchema(sub as JsonObject);
38+
potentials = new Set([...potentials].filter(p => types.has(p)));
39+
}
40+
}
41+
42+
if (Array.isArray(schema.oneOf)) {
43+
let options = new Set<string>();
44+
for (const sub of schema.oneOf) {
45+
const types = getTypesOfSchema(sub as JsonObject);
46+
options = new Set([...options, ...types]);
47+
}
48+
potentials = new Set([...potentials].filter(p => options.has(p)));
49+
}
50+
51+
if (Array.isArray(schema.anyOf)) {
52+
let options = new Set<string>();
53+
for (const sub of schema.anyOf) {
54+
const types = getTypesOfSchema(sub as JsonObject);
55+
options = new Set([...options, ...types]);
56+
}
57+
potentials = new Set([...potentials].filter(p => options.has(p)));
58+
}
59+
60+
if (schema.properties) {
61+
potentials.add('object');
62+
} else if (schema.items) {
63+
potentials.add('array');
64+
}
65+
66+
return potentials;
67+
}

0 commit comments

Comments
 (0)