Skip to content

Commit fb9d779

Browse files
authored
Making CriticalSound.name field required in typings (#426)
* Making CriticalSound.name field required in typings * Disallowing non-empty strings in APNs sound config * Test cases for empty string
1 parent bf899ab commit fb9d779

File tree

4 files changed

+55
-7
lines changed

4 files changed

+55
-7
lines changed

src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ declare namespace admin.messaging {
473473

474474
type CriticalSound = {
475475
critical?: boolean;
476-
name?: string;
476+
name: string;
477477
volume?: number;
478478
}
479479

src/messaging/messaging.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ export interface Aps {
180180

181181
export interface CriticalSound {
182182
critical?: boolean;
183-
name?: string;
183+
name: string;
184184
volume?: number;
185185
}
186186

@@ -340,14 +340,19 @@ function validateAps(aps: Aps) {
340340
}
341341

342342
function validateApsSound(sound: string | CriticalSound) {
343-
if (typeof sound === 'undefined' || validator.isString(sound)) {
343+
if (typeof sound === 'undefined' || validator.isNonEmptyString(sound)) {
344344
return;
345345
} else if (!validator.isNonNullObject(sound)) {
346346
throw new FirebaseMessagingError(
347347
MessagingClientErrorCode.INVALID_PAYLOAD,
348-
'apns.payload.aps.sound must be a string or a non-null object');
348+
'apns.payload.aps.sound must be a non-empty string or a non-null object');
349349
}
350350

351+
if (!validator.isNonEmptyString(sound.name)) {
352+
throw new FirebaseMessagingError(
353+
MessagingClientErrorCode.INVALID_PAYLOAD,
354+
'apns.payload.aps.sound.name must be a non-empty string');
355+
}
351356
const volume = sound.volume;
352357
if (typeof volume !== 'undefined') {
353358
if (!validator.isNumber(volume)) {

src/utils/validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export function isBase64String(value: any): boolean {
100100
* @param {any} value The value to validate.
101101
* @return {boolean} Whether the value is a non-empty string or not.
102102
*/
103-
export function isNonEmptyString(value: any): boolean {
103+
export function isNonEmptyString(value: any): value is string {
104104
return isString(value) && value !== '';
105105
}
106106

test/unit/messaging/messaging.spec.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,7 @@ describe('Messaging', () => {
17281728
payload: {
17291729
aps: {
17301730
sound: {
1731+
name: 'default',
17311732
volume,
17321733
},
17331734
},
@@ -1881,12 +1882,29 @@ describe('Messaging', () => {
18811882
});
18821883
});
18831884

1884-
const invalidApnsSounds: any[] = [null, [], true, 1.23];
1885+
const invalidApnsSounds: any[] = ['', null, [], true, 1.23];
18851886
invalidApnsSounds.forEach((sound) => {
18861887
it(`should throw given APNS payload with invalid aps sound: ${JSON.stringify(sound)}`, () => {
18871888
expect(() => {
18881889
messaging.send({apns: {payload: {aps: {sound}}}, token: 'token'});
1889-
}).to.throw('apns.payload.aps.sound must be a string or a non-null object');
1890+
}).to.throw('apns.payload.aps.sound must be a non-empty string or a non-null object');
1891+
});
1892+
});
1893+
invalidApnsSounds.forEach((name) => {
1894+
it(`should throw given invalid APNS critical sound name: ${name}`, () => {
1895+
const message: Message = {
1896+
condition: 'topic-name',
1897+
apns: {
1898+
payload: {
1899+
aps: {
1900+
sound: {name},
1901+
},
1902+
},
1903+
},
1904+
};
1905+
expect(() => {
1906+
messaging.send(message);
1907+
}).to.throw('apns.payload.aps.sound.name must be a non-empty string');
18901908
});
18911909
});
18921910
});
@@ -2403,6 +2421,31 @@ describe('Messaging', () => {
24032421
},
24042422
},
24052423
},
2424+
{
2425+
label: 'APNS critical sound name only',
2426+
req: {
2427+
apns: {
2428+
payload: {
2429+
aps: {
2430+
sound: {
2431+
name: 'test.sound',
2432+
},
2433+
},
2434+
},
2435+
},
2436+
},
2437+
expectedReq: {
2438+
apns: {
2439+
payload: {
2440+
aps: {
2441+
sound: {
2442+
name: 'test.sound',
2443+
},
2444+
},
2445+
},
2446+
},
2447+
},
2448+
},
24062449
{
24072450
label: 'APNS critical sound explicitly false',
24082451
req: {

0 commit comments

Comments
 (0)