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 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.addUndefinedDefaults = void 0;
|
---|
11 | const interface_1 = require("../interface");
|
---|
12 | const utility_1 = require("./utility");
|
---|
13 | function addUndefinedDefaults(value, _pointer, schema) {
|
---|
14 | if (typeof schema === 'boolean' || schema === undefined) {
|
---|
15 | return value;
|
---|
16 | }
|
---|
17 | const types = utility_1.getTypesOfSchema(schema);
|
---|
18 | if (types.size === 0) {
|
---|
19 | return value;
|
---|
20 | }
|
---|
21 | let type;
|
---|
22 | if (types.size === 1) {
|
---|
23 | // only one potential type
|
---|
24 | type = Array.from(types)[0];
|
---|
25 | }
|
---|
26 | else if (types.size === 2 && types.has('array') && types.has('object')) {
|
---|
27 | // need to create one of them and array is simpler
|
---|
28 | type = 'array';
|
---|
29 | }
|
---|
30 | else if (schema.properties && types.has('object')) {
|
---|
31 | // assume object
|
---|
32 | type = 'object';
|
---|
33 | }
|
---|
34 | else if (schema.items && types.has('array')) {
|
---|
35 | // assume array
|
---|
36 | type = 'array';
|
---|
37 | }
|
---|
38 | else {
|
---|
39 | // anything else needs to be checked by the consumer anyway
|
---|
40 | return value;
|
---|
41 | }
|
---|
42 | if (type === 'array') {
|
---|
43 | return value == undefined ? [] : value;
|
---|
44 | }
|
---|
45 | if (type === 'object') {
|
---|
46 | let newValue;
|
---|
47 | if (value == undefined) {
|
---|
48 | newValue = {};
|
---|
49 | }
|
---|
50 | else if (interface_1.isJsonObject(value)) {
|
---|
51 | newValue = value;
|
---|
52 | }
|
---|
53 | else {
|
---|
54 | return value;
|
---|
55 | }
|
---|
56 | if (!interface_1.isJsonObject(schema.properties)) {
|
---|
57 | return newValue;
|
---|
58 | }
|
---|
59 | for (const [propName, schemaObject] of Object.entries(schema.properties)) {
|
---|
60 | if (propName === '$schema' || !interface_1.isJsonObject(schemaObject)) {
|
---|
61 | continue;
|
---|
62 | }
|
---|
63 | const value = newValue[propName];
|
---|
64 | if (value === undefined) {
|
---|
65 | newValue[propName] = schemaObject.default;
|
---|
66 | }
|
---|
67 | else if (interface_1.isJsonObject(value)) {
|
---|
68 | // Basic support for oneOf and anyOf.
|
---|
69 | const propertySchemas = schemaObject.oneOf || schemaObject.anyOf;
|
---|
70 | const allProperties = Object.keys(value);
|
---|
71 | // Locate a schema which declares all the properties that the object contains.
|
---|
72 | const adjustedSchema = interface_1.isJsonArray(propertySchemas) &&
|
---|
73 | propertySchemas.find((s) => {
|
---|
74 | if (!interface_1.isJsonObject(s)) {
|
---|
75 | return false;
|
---|
76 | }
|
---|
77 | const schemaType = utility_1.getTypesOfSchema(s);
|
---|
78 | if (schemaType.size === 1 && schemaType.has('object') && interface_1.isJsonObject(s.properties)) {
|
---|
79 | const properties = Object.keys(s.properties);
|
---|
80 | return allProperties.every((key) => properties.includes(key));
|
---|
81 | }
|
---|
82 | return false;
|
---|
83 | });
|
---|
84 | if (adjustedSchema && interface_1.isJsonObject(adjustedSchema)) {
|
---|
85 | newValue[propName] = addUndefinedDefaults(value, _pointer, adjustedSchema);
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 | return newValue;
|
---|
90 | }
|
---|
91 | return value;
|
---|
92 | }
|
---|
93 | exports.addUndefinedDefaults = addUndefinedDefaults;
|
---|