From 095b2e31d65c50b569ac55e2ca10239111b3a8de Mon Sep 17 00:00:00 2001 From: AliReza Seyfpour Date: Fri, 1 Nov 2024 15:00:23 +0330 Subject: [PATCH] fix: fill base64 options correctly pass the constraint to the isBase64 function add unit test IsBase64 options --- src/decorator/string/IsBase64.ts | 2 +- ...validation-functions-and-decorators.spec.ts | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/decorator/string/IsBase64.ts b/src/decorator/string/IsBase64.ts index 2c5cd3e5be..e7002e0e9a 100644 --- a/src/decorator/string/IsBase64.ts +++ b/src/decorator/string/IsBase64.ts @@ -26,7 +26,7 @@ export function IsBase64( name: IS_BASE64, constraints: [options], validator: { - validate: (value, args): boolean => isBase64(value), + validate: (value, args): boolean => isBase64(value, args?.constraints[0]), defaultMessage: buildMessage(eachPrefix => eachPrefix + '$property must be base64 encoded', validationOptions), }, }, diff --git a/test/functional/validation-functions-and-decorators.spec.ts b/test/functional/validation-functions-and-decorators.spec.ts index 78ceddcd6d..9f938616c4 100644 --- a/test/functional/validation-functions-and-decorators.spec.ts +++ b/test/functional/validation-functions-and-decorators.spec.ts @@ -1658,17 +1658,27 @@ describe('IsBase64', () => { const validValues = ['aGVsbG8=']; const invalidValues = [null, undefined, 'hell*mynameisalex']; + const validBase64UrlValues = ['dGVzdA', 'dGV_zdA']; + const invalidBase64UrlValues = [null, undefined, 'dGVzdA=', 'MTIzNDU2Nzg5!!', 'SGVsbG8+V29ybGQ=']; + class MyClass { @IsBase64() someProperty: string; } - it('should not fail if validator.validate said that its valid', () => { - return checkValidValues(new MyClass(), validValues); + class MyClassWithConstraint { + @IsBase64({ urlSafe: true }) + someProperty: string; + } + + it('should not fail if validator.validate said that its valid', async () => { + await checkValidValues(new MyClass(), validValues); + await checkValidValues(new MyClassWithConstraint(), validBase64UrlValues); }); - it('should fail if validator.validate said that its invalid', () => { - return checkInvalidValues(new MyClass(), invalidValues); + it('should fail if validator.validate said that its invalid', async () => { + await checkInvalidValues(new MyClass(), invalidValues); + await checkInvalidValues(new MyClassWithConstraint(), invalidBase64UrlValues); }); it('should not fail if method in validator said that its valid', () => {