From 980a1ddf745032a790a043e8040cdb7ec5b110ad Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sun, 26 Jan 2020 12:34:27 -0500 Subject: [PATCH] Improve support for array schemas --- ext/bg/js/json-schema.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/ext/bg/js/json-schema.js b/ext/bg/js/json-schema.js index 7a7f2489..f32176fd 100644 --- a/ext/bg/js/json-schema.js +++ b/ext/bg/js/json-schema.js @@ -149,7 +149,23 @@ class JsonSchemaProxyHandler { case 'array': { const items = schema.items; - return JsonSchemaProxyHandler.isObject(items) ? items : null; + if (JsonSchemaProxyHandler.isObject(items)) { + return items; + } + if (Array.isArray(items)) { + if (property >= 0 && property < items.length) { + return items[property]; + } + } + + const additionalItems = schema.additionalItems; + if (additionalItems === false) { + return null; + } else if (JsonSchemaProxyHandler.isObject(additionalItems)) { + return additionalItems; + } else { + return JsonSchemaProxyHandler._unconstrainedSchema; + } } default: return null; @@ -322,6 +338,18 @@ class JsonSchemaProxyHandler { return 'Array length too long'; } + for (let i = 0, ii = value.length; i < ii; ++i) { + const propertySchema = JsonSchemaProxyHandler.getPropertySchema(schema, i, value); + if (propertySchema === null) { + return `No schema found for array[${i}]`; + } + + const error = JsonSchemaProxyHandler.validate(value[i], propertySchema); + if (error !== null) { + return error; + } + } + return null; }