From 49fc42c4f80616d6ccb4da80ede1945d7a63026d Mon Sep 17 00:00:00 2001 From: Leonardo Viva Date: Mon, 15 Apr 2024 10:47:23 -0300 Subject: [PATCH] fix: validationScript --- src/imports/data/data.js | 7 +++---- src/imports/data/scripts.js | 16 ++++++++++------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/imports/data/data.js b/src/imports/data/data.js index 51149fcb..030bd79d 100644 --- a/src/imports/data/data.js +++ b/src/imports/data/data.js @@ -1029,7 +1029,7 @@ export async function create({ authTokenId, document, data, contextUser, upsert, if (metaObject.validationScript != null) { tracingSpan?.addEvent('Running validation script'); - const validation = await processValidationScript({ script: metaObject.validationScript, data, fullData: extend({}, data, cleanedData), user }); + const validation = await processValidationScript({ script: metaObject.validationScript, validationData: metaObject.validationData, fullData: extend({}, data, cleanedData), user }); if (validation.success === false) { logger.error(validation, `Create - Script Validation Error - ${validation.reason}`); return errorReturn(`[${document}] ${validation.reason}`); @@ -1447,8 +1447,7 @@ export async function update({ authTokenId, document, data, contextUser, tracing Object.keys(data.data).forEach(fieldName => { if (record.diffs[fieldName] != null) { acc.push( - `[${document}] Record ${record.dataId} is out of date, field ${fieldName} was updated at ${DateTime.fromJSDate(record.createdAt).toISO()} by ${ - record.createdBy.name + `[${document}] Record ${record.dataId} is out of date, field ${fieldName} was updated at ${DateTime.fromJSDate(record.createdAt).toISO()} by ${record.createdBy.name }`, ); } @@ -1562,7 +1561,7 @@ export async function update({ authTokenId, document, data, contextUser, tracing if (metaObject.validationScript != null) { tracingSpan?.addEvent('Running validation script'); - const validationScriptResult = await processValidationScript({ script: metaObject.validationScript, data: bodyData, fullData: extend({}, record, data.data), user }); + const validationScriptResult = await processValidationScript({ script: metaObject.validationScript, validationData: metaObject.validationData, fullData: extend({}, record, data.data), user }); if (validationScriptResult.success === false) { logger.error(validationScriptResult, `Update - Script Validation Error - ${validationScriptResult.reason}`); return errorReturn(validationScriptResult.reason); diff --git a/src/imports/data/scripts.js b/src/imports/data/scripts.js index ea9524ee..ffc148e4 100644 --- a/src/imports/data/scripts.js +++ b/src/imports/data/scripts.js @@ -69,15 +69,19 @@ export async function runScriptBeforeValidation({ script, data, user, meta, extr } } -export async function processValidationScript({ script, data, fullData, user }) { - if (data != null) { - const extraData = BluebirdPromise.reduce( - Object.keys(data), +export async function processValidationScript({ script, validationData, fullData, user }) { + if (validationData != null) { + const extraData = await BluebirdPromise.reduce( + Object.keys(validationData), async (acc, validationField) => { - const validationFilter = data[validationField]; + const validationFilter = validationData[validationField]; const validationDataFilter = await parseDynamicData(validationFilter, '$this', fullData); + if (validationDataFilter.success !== true) { + return acc; + } + const fieldResult = await find({ - ...validationDataFilter, + ...validationDataFilter.data, contextUser: user, });