From bbace4c93bfa110a3a0c282bd72a6d9737465f4f Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 29 Dec 2019 18:57:29 -0500 Subject: [PATCH] Fix schema defaults not applying to arrays --- ext/bg/js/json-schema.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/ext/bg/js/json-schema.js b/ext/bg/js/json-schema.js index b059d757..3238bc3e 100644 --- a/ext/bg/js/json-schema.js +++ b/ext/bg/js/json-schema.js @@ -352,8 +352,13 @@ class JsonSchemaProxyHandler { } } - if (type === 'object') { - value = JsonSchemaProxyHandler.populateObjectDefaults(value, schema); + switch (type) { + case 'object': + value = JsonSchemaProxyHandler.populateObjectDefaults(value, schema); + break; + case 'array': + value = JsonSchemaProxyHandler.populateArrayDefaults(value, schema); + break; } return value; @@ -384,6 +389,16 @@ class JsonSchemaProxyHandler { return value; } + + static populateArrayDefaults(value, schema) { + for (let i = 0, ii = value.length; i < ii; ++i) { + const propertySchema = JsonSchemaProxyHandler.getPropertySchema(schema, i); + if (propertySchema === null) { continue; } + value[i] = JsonSchemaProxyHandler.getValidValueOrDefault(propertySchema, value[i]); + } + + return value; + } } class JsonSchema {