Skip to content

Commit

Permalink
fix: validationScript
Browse files Browse the repository at this point in the history
  • Loading branch information
7sete7 committed Apr 15, 2024
1 parent a741c4c commit 49fc42c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
7 changes: 3 additions & 4 deletions src/imports/data/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down Expand Up @@ -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
}`,
);
}
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 10 additions & 6 deletions src/imports/data/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});

Expand Down

0 comments on commit 49fc42c

Please sign in to comment.