|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be |
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | | -import { JsonArray, JsonObject, JsonValue } from '../interface'; |
| 8 | +import { JsonObject, JsonValue } from '../interface'; |
9 | 9 | import { JsonPointer } from './interface'; |
10 | 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 (JsonValue.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 | +} |
11 | 59 |
|
12 | 60 | export function addUndefinedDefaults( |
13 | | - value: JsonValue | undefined, |
| 61 | + value: JsonValue, |
14 | 62 | _pointer: JsonPointer, |
15 | 63 | schema?: JsonObject, |
16 | | - _root?: JsonObject | JsonArray, |
17 | 64 | ): JsonValue { |
18 | | - if (value === undefined && schema) { |
19 | | - if (schema.items || schema.type == 'array') { |
20 | | - return []; |
| 65 | + if (!schema) { |
| 66 | + return value; |
| 67 | + } |
| 68 | + |
| 69 | + const types = findTypes(schema); |
| 70 | + if (types.size === 0) { |
| 71 | + return value; |
| 72 | + } |
| 73 | + |
| 74 | + let type; |
| 75 | + if (types.size === 1) { |
| 76 | + // only one potential type |
| 77 | + type = Array.from(types)[0]; |
| 78 | + } else if (types.size === 2 && types.has('array') && types.has('object')) { |
| 79 | + // need to create one of them and array is simpler |
| 80 | + type = 'array'; |
| 81 | + } else if (schema.properties && types.has('object')) { |
| 82 | + // assume object |
| 83 | + type = 'object'; |
| 84 | + } else if (schema.items && types.has('array')) { |
| 85 | + // assume array |
| 86 | + type = 'array'; |
| 87 | + } else { |
| 88 | + // anything else needs to be checked by the consumer anyway |
| 89 | + return value; |
| 90 | + } |
| 91 | + |
| 92 | + if (type === 'array') { |
| 93 | + return value == undefined ? [] : value; |
| 94 | + } |
| 95 | + |
| 96 | + if (type === 'object') { |
| 97 | + let newValue; |
| 98 | + if (value == undefined) { |
| 99 | + newValue = {} as JsonObject; |
| 100 | + } else if (JsonValue.isJsonObject(value)) { |
| 101 | + newValue = value; |
| 102 | + } else { |
| 103 | + return value; |
21 | 104 | } |
22 | | - if (schema.properties || schema.type == 'object') { |
23 | | - const newValue: JsonObject = {}; |
24 | | - for (const propName of Object.getOwnPropertyNames(schema.properties || {})) { |
25 | | - newValue[propName] = undefined as any; // tslint:disable-line:no-any |
26 | | - } |
27 | 105 |
|
| 106 | + if (!JsonValue.isJsonObject(schema.properties)) { |
28 | 107 | return newValue; |
29 | 108 | } |
30 | | - } else if (schema |
31 | | - && typeof value == 'object' && value |
32 | | - && (schema.properties || schema.type == 'object') |
33 | | - ) { |
34 | | - for (const propName of Object.getOwnPropertyNames(schema.properties || {})) { |
35 | | - (value as JsonObject)[propName] = (propName in value) |
36 | | - ? (value as JsonObject)[propName] |
37 | | - : undefined as any; // tslint:disable-line:no-any |
| 109 | + |
| 110 | + for (const propName of Object.getOwnPropertyNames(schema.properties)) { |
| 111 | + if (propName in newValue) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + |
| 115 | + // TODO: Does not currently handle more complex schemas (oneOf/anyOf/etc.) |
| 116 | + const defaultValue = (schema.properties[propName] as JsonObject).default; |
| 117 | + |
| 118 | + newValue[propName] = defaultValue; |
38 | 119 | } |
| 120 | + |
| 121 | + return newValue; |
39 | 122 | } |
40 | 123 |
|
41 | | - return value as JsonValue; |
| 124 | + return value; |
42 | 125 | } |
0 commit comments