Skip to content

Commit 0c87beb

Browse files
aseyfpourbraaar
andauthored
fix: pass IsBase64 options correctly (#2549)
* fix: fill base64 options correctly pass the constraint to the isBase64 function add unit test IsBase64 options * refactor: pass options by name --------- Co-authored-by: Brage Sekse Aarset <brage.aarset@gmail.com>
1 parent b07bb16 commit 0c87beb

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/decorator/string/IsBase64.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function IsBase64(
2626
name: IS_BASE64,
2727
constraints: [options],
2828
validator: {
29-
validate: (value, args): boolean => isBase64(value),
29+
validate: (value, args): boolean => isBase64(value, options),
3030
defaultMessage: buildMessage(eachPrefix => eachPrefix + '$property must be base64 encoded', validationOptions),
3131
},
3232
},

test/functional/validation-functions-and-decorators.spec.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1658,17 +1658,27 @@ describe('IsBase64', () => {
16581658
const validValues = ['aGVsbG8='];
16591659
const invalidValues = [null, undefined, 'hell*mynameisalex'];
16601660

1661+
const validBase64UrlValues = ['dGVzdA', 'dGV_zdA'];
1662+
const invalidBase64UrlValues = [null, undefined, 'dGVzdA=', 'MTIzNDU2Nzg5!!', 'SGVsbG8+V29ybGQ='];
1663+
16611664
class MyClass {
16621665
@IsBase64()
16631666
someProperty: string;
16641667
}
16651668

1666-
it('should not fail if validator.validate said that its valid', () => {
1667-
return checkValidValues(new MyClass(), validValues);
1669+
class MyClassWithConstraint {
1670+
@IsBase64({ urlSafe: true })
1671+
someProperty: string;
1672+
}
1673+
1674+
it('should not fail if validator.validate said that its valid', async () => {
1675+
await checkValidValues(new MyClass(), validValues);
1676+
await checkValidValues(new MyClassWithConstraint(), validBase64UrlValues);
16681677
});
16691678

1670-
it('should fail if validator.validate said that its invalid', () => {
1671-
return checkInvalidValues(new MyClass(), invalidValues);
1679+
it('should fail if validator.validate said that its invalid', async () => {
1680+
await checkInvalidValues(new MyClass(), invalidValues);
1681+
await checkInvalidValues(new MyClassWithConstraint(), invalidBase64UrlValues);
16721682
});
16731683

16741684
it('should not fail if method in validator said that its valid', () => {

0 commit comments

Comments
 (0)