Skip to content

Commit

Permalink
Merge pull request #537 from episphere/stage
Browse files Browse the repository at this point in the history
Stage -> prod sync updateParticipantData array validation
  • Loading branch information
JoeArmani authored Feb 5, 2024
2 parents 99a8f69 + e6aba6b commit 1ba71eb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
6 changes: 4 additions & 2 deletions updateParticipantData.json
Original file line number Diff line number Diff line change
Expand Up @@ -402,11 +402,13 @@
},
"173836415.266600170.543608829": {
"dataType": "array",
"mustExist": false
"mustExist": false,
"innerElementType": "string"
},
"173836415.266600170.110349197": {
"dataType": "array",
"mustExist": false
"mustExist": false,
"innerElementType": "string"
},
"857217152": {
"dataType": "number",
Expand Down
14 changes: 11 additions & 3 deletions utils/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -1139,9 +1139,16 @@ const flattenObject = (obj, parentPath = '') => {

if (value && typeof value === 'object') {
if (Array.isArray(value)) {
value.forEach((item, index) => {
traverse(item, `${newPath}[${index}]`);
});
// Check if element is an object to decide whether to traverse further.
// This ensures arrays of primitive values are kept intact.
// Seen in keys 173836415.266600170.110349197 & 173836415.266600170.543608829.
if (value.length === 0 || typeof value[0] !== 'object') {
flattened[newPath] = value;
} else {
value.forEach((item, index) => {
traverse(item, `${newPath}[${index}]`);
});
}
} else {
traverse(value, newPath);
}
Expand Down Expand Up @@ -1477,6 +1484,7 @@ const filterSelectedFields = (dataObjArray, selectedFieldsArray) => {
}



module.exports = {
getResponseJSON,
setHeaders,
Expand Down
7 changes: 7 additions & 0 deletions utils/sites.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,13 @@ const validateUpdateParticipantData = (value, existingValue, path, rule) => {
if (!Array.isArray(value)) {
return `Data mismatch: ${path} must be an array.`;
}
if (rule.innerElementType) {
for (let i = 0; i < value.length; i++) {
if (typeof value[i] !== rule.innerElementType) {
return `Data mismatch: Element at index ${i} of ${path} must be a ${rule.innerElementType}.`;
}
}
}
break;
case 'object':
if (typeof value !== 'object' || Array.isArray(value)) {
Expand Down

0 comments on commit 1ba71eb

Please sign in to comment.