Skip to content

Commit 25175be

Browse files
SeddikBellaminePierreJeanjacquot
authored andcommitted
fix(validators): improve campaignRequest schema validation error message
- Add strict mode to prevent undefined coercion to empty object - Add custom test to check for undefined/null/empty object before field validation - Fix error message to show 'campaignRequest is required' instead of 'sign is a required field' when campaignRequest is undefined
1 parent 48e8698 commit 25175be

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/utils/validators.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,14 @@ export const campaignRequestSchema = () =>
117117
trust: positiveIntegerStringSchema().required(),
118118
salt: stringSchema().required(),
119119
sign: stringSchema().required(),
120-
}).typeError('${path} should be a BulkRequest object');
120+
})
121+
.strict()
122+
.typeError('${path} should be a BulkRequest object')
123+
.test('is-defined', '${path} is required', (value) => {
124+
// Check if value is undefined, null, or an empty object (which would be coerced from undefined)
125+
return (
126+
value !== undefined &&
127+
value !== null &&
128+
!(typeof value === 'object' && Object.keys(value).length === 0)
129+
);
130+
});

tests/e2e/sendEmailCampaign.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -314,15 +314,12 @@ describe('web3mail.sendEmailCampaign()', () => {
314314
.catch((e) => (error = e));
315315

316316
expect(error).toBeDefined();
317-
// campaignRequest is undefined, so accessing campaignRequest.workerpool throws TypeError
318-
// which gets wrapped in WorkflowError
319-
const isWorkflowError = error instanceof Web3mailWorkflowError;
320-
const isTypeError = error instanceof TypeError;
321-
expect(isWorkflowError || isTypeError).toBe(true);
322-
// Check message only if it's a WorkflowError
323-
expect(
324-
!isWorkflowError || error.message === 'Failed to sendEmailCampaign'
325-
).toBe(true);
317+
318+
const isValidationrror = error instanceof ValidationError;
319+
expect(isValidationrror).toBe(true);
320+
expect(error.message === 'campaignRequest is a required field').toBe(
321+
true
322+
);
326323
},
327324
MAX_EXPECTED_WEB2_SERVICES_TIME
328325
);

0 commit comments

Comments
 (0)