From 4f48b9bb3c26a0f5bda6b98248181f419bcd1859 Mon Sep 17 00:00:00 2001 From: John Teague Date: Wed, 10 Jul 2024 10:04:37 -0500 Subject: [PATCH] returning empty array for empty edit grids --- src/process/__tests__/process.test.ts | 24 ++++++++++++++++++++++++ src/process/filter/index.ts | 13 ++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/src/process/__tests__/process.test.ts b/src/process/__tests__/process.test.ts index 527eb6b7..391c376c 100644 --- a/src/process/__tests__/process.test.ts +++ b/src/process/__tests__/process.test.ts @@ -2862,6 +2862,29 @@ describe('Process Tests', () => { components: [nestedForm], }, ]; + it('should return empty array when no data is provided', async () => { + const submission = { + data: { + editGrid: [], + }, + }; + const context = { + form: { components }, + submission, + data: submission.data, + components, + processors: ProcessTargets.submission, + scope: {}, + config: { + server: true, + }, + }; + processSync(context); + context.processors = ProcessTargets.evaluator; + processSync(context); + expect(context.data).to.deep.equal({ editGrid: [] }); + + }) it('Should validate required component when it is filled out', async () => { const submission = { data: { @@ -2924,6 +2947,7 @@ describe('Process Tests', () => { processSync(context); expect((context.scope as ValidationScope).errors).to.have.length(1); }); + }); }); /* diff --git a/src/process/filter/index.ts b/src/process/filter/index.ts index 29552076..423b03f2 100644 --- a/src/process/filter/index.ts +++ b/src/process/filter/index.ts @@ -61,15 +61,18 @@ export const filterPostProcess: ProcessorFnSync = ( context: FilterContext ) => { const { scope, component, submission } = context; - let filtered = {}; + let filtered: Record = {}; for (const path in scope.filter) { + let value = get(submission?.data, path) as any; const pathFilter = scope.filter[path]; + if (pathFilter.compModelType === 'array') { + // special case for array, if it's empty, set it to empty array + if(value.length === 0) { + filtered[path] = [] + } continue; - } - if (pathFilter) { - let value = get(submission?.data, path) as any; - + } else if (pathFilter) { // when it's a dataModel Object, don't set values directly on the data object, let child fields do that. // it can have extra data on updates, so pass all other values except data // standard lodash set function will mutate original value, using the functional version so it doesn't