From 0eb70fd321af7bd2a52ea5b8a614321397f794c0 Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Tue, 11 Feb 2025 09:45:49 -0400 Subject: sugar, checks: withNestedProp+filterReferenceErrors: enter arrays --- src/common-util/sugar.js | 10 +++++++++- src/data/checks.js | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/common-util/sugar.js b/src/common-util/sugar.js index 90d47b7c..89699f60 100644 --- a/src/common-util/sugar.js +++ b/src/common-util/sugar.js @@ -356,11 +356,19 @@ export function splitKeys(key) { } // Follows a key path like 'foo.bar.baz' to get an item nested deeply inside -// an object. +// an object. If a value partway through the chain is an array, the values +// down the rest of the chain are gotten for each item in the array. +// +// obj: {x: [{y: ['a']}, {y: ['b', 'c']}]} +// key: 'x.y' +// -> [['a'], ['b', 'c']] +// export function getNestedProp(obj, key) { const recursive = (o, k) => (k.length === 1 ? o[k[0]] + : Array.isArray(o[k[0]]) + ? o[k[0]].map(v => recursive(v, k.slice(1))) : recursive(o[k[0]], k.slice(1))); return recursive(obj, splitKeys(key)); diff --git a/src/data/checks.js b/src/data/checks.js index 9a859165..57d0c1a4 100644 --- a/src/data/checks.js +++ b/src/data/checks.js @@ -237,7 +237,7 @@ export function filterReferenceErrors(wikiData, { const aggregate = openAggregate({message: `Errors validating between-thing references in data`}); for (const [thingDataProp, propSpec] of referenceSpec) { const thingData = getNestedProp(wikiData, thingDataProp); - const things = Array.isArray(thingData) ? thingData : [thingData]; + const things = Array.isArray(thingData) ? thingData.flat(Infinity) : [thingData]; aggregate.nest({message: `Reference errors in ${colors.green('wikiData.' + thingDataProp)}`}, ({nest}) => { for (const thing of things) { nest({message: `Reference errors in ${inspect(thing)}`}, ({nest, push, filter}) => { -- cgit 1.3.0-6-gf8a5