Skip to content

Commit 46766bb

Browse files
committed
core/Template: Only add defaults in schema to primitive types that have no user definition
1 parent bad9e44 commit 46766bb

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

lib/template.js

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -161,34 +161,40 @@ class Template {
161161
}
162162
this.definitions[defType] = Object.assign({}, schemaDef, this.definitions[defType]);
163163
this.typeDefinitions[defType] = Object.assign({}, schemaDef);
164-
} else if (defType === 'text') {
165-
acc.properties[defName] = {
166-
type: 'string',
167-
format: 'text'
168-
};
169-
} else if (defType === 'array') {
170-
acc.properties[defName] = {
171-
type: defType,
172-
items: {
173-
type: 'string'
174-
}
175-
};
176-
} else if (defType === 'hidden') {
177-
acc.properties[defName] = {
178-
type: 'string',
179-
format: 'hidden',
180-
default: ''
181-
};
182164
} else {
183-
acc.properties[defName] = {
184-
type: defType
185-
};
165+
if (defType === 'text') {
166+
acc.properties[defName] = {
167+
type: 'string',
168+
format: 'text'
169+
};
170+
} else if (defType === 'array') {
171+
acc.properties[defName] = {
172+
type: defType,
173+
items: {
174+
type: 'string'
175+
}
176+
};
177+
} else if (defType === 'hidden') {
178+
acc.properties[defName] = {
179+
type: 'string',
180+
format: 'hidden'
181+
};
182+
} else {
183+
acc.properties[defName] = {
184+
type: defType
185+
};
186+
}
187+
188+
const propType = acc.properties[defName].type;
189+
if (!this.definitions[defName] && typeof primitives[propType] !== 'undefined') {
190+
acc.properties[defName].default = primitives[propType];
191+
}
186192
}
187193
if (this.definitions[defName]) {
188194
Object.assign(acc.properties[defName], this.definitions[defName]);
189195
}
190196
const propDef = acc.properties[defName];
191-
if (typeof propDef.default === 'undefined' && propDef.format !== 'hidden') {
197+
if (propDef.format !== 'hidden') {
192198
required.add(defName);
193199
}
194200
break;
@@ -220,9 +226,6 @@ class Template {
220226
throw new Error(`unsupported type for section "${mstName}": ${defType}`);
221227
}
222228

223-
acc.properties[mstName] = Object.assign({}, newDef);
224-
required.add(mstName);
225-
226229
if (items.properties) {
227230
Object.keys(items.properties).forEach((item) => {
228231
if (!dependencies[item]) {
@@ -235,15 +238,23 @@ class Template {
235238
if (asBool) {
236239
// Hoist properties to global scope
237240
this._mergeSchemaInto(acc, items, dependencies);
241+
if (typeof newDef.default === 'undefined') {
242+
newDef.default = primitives[newDef.type];
243+
}
238244
}
245+
246+
acc.properties[mstName] = Object.assign({}, newDef);
247+
required.add(mstName);
248+
239249
break;
240250
}
241251
case '^': {
242252
const items = this._handleParsed(curr[4], typeSchemas);
243253

244254
if (!acc.properties[mstName]) {
245255
acc.properties[mstName] = {
246-
type: 'boolean'
256+
type: 'boolean',
257+
default: primitives.boolean
247258
};
248259
}
249260
if (items.properties) {
@@ -292,13 +303,6 @@ class Template {
292303
default: primitives.string
293304
};
294305
}
295-
// Set default values for any primitives
296-
Object.keys(schema.properties).forEach((prop) => {
297-
const def = schema.properties[prop];
298-
if (typeof def.default === 'undefined' && typeof primitives[def.type] !== 'undefined') {
299-
schema.properties[prop].default = primitives[def.type];
300-
}
301-
});
302306

303307
// Get propertyOrder from definition if available
304308
Object.values(schema.properties).forEach((schemaDef) => {

test/template.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ describe('Template class tests', function () {
134134
items: {
135135
default: '',
136136
type: 'string'
137-
},
138-
default: []
137+
}
139138
},
140139
boolean_variable: { type: 'boolean', default: false },
141140
number_variable: { type: 'number', default: 0 },

0 commit comments

Comments
 (0)