Skip to content

Commit 175811b

Browse files
committed
validateParams: add type validation
1 parent 67a89f9 commit 175811b

File tree

2 files changed

+41
-17
lines changed

2 files changed

+41
-17
lines changed

src/utils/validateParams/validateParams.spec.ts

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,46 @@
1-
import { it, expect } from '@jest/globals';
1+
import { it, expect, describe } from '@jest/globals';
22
import { validateParams } from './validateParams';
33

4-
it('should fail on the public key', () => {
5-
expect(() => validateParams('', 'default_service', 'my_test_template')).toThrow(
6-
'The public key is required',
7-
);
4+
describe('should fail on the public key', () => {
5+
it('no key', () => {
6+
expect(() => validateParams('', 'default_service', 'my_test_template')).toThrow(
7+
'The public key is required',
8+
);
9+
});
10+
11+
it('invalida type', () => {
12+
expect(() => validateParams({}, 'default_service', 'my_test_template')).toThrow(
13+
'The public key is required',
14+
);
15+
});
816
});
917

10-
it('should fail on the service ID', () => {
11-
expect(() => validateParams('d2JWGTestKeySomething', '', 'my_test_template')).toThrow(
12-
'The service ID is required',
13-
);
18+
describe('should fail on the service ID', () => {
19+
it('no key', () => {
20+
expect(() => validateParams('d2JWGTestKeySomething', '', 'my_test_template')).toThrow(
21+
'The service ID is required',
22+
);
23+
});
24+
25+
it('invalida type', () => {
26+
expect(() => validateParams('d2JWGTestKeySomething', [], 'my_test_template')).toThrow(
27+
'The service ID is required',
28+
);
29+
});
1430
});
1531

16-
it('should fail on the template ID', () => {
17-
expect(() => validateParams('d2JWGTestKeySomething', 'default_service', '')).toThrow(
18-
'The template ID is required',
19-
);
32+
describe('should fail on the template ID', () => {
33+
it('no key', () => {
34+
expect(() => validateParams('d2JWGTestKeySomething', 'default_service', '')).toThrow(
35+
'The template ID is required',
36+
);
37+
});
38+
39+
it('invalida type', () => {
40+
expect(() => validateParams('d2JWGTestKeySomething', 'default_service', 3)).toThrow(
41+
'The template ID is required',
42+
);
43+
});
2044
});
2145

2246
it('should successfully pass the validation', () => {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
export const validateParams = (publicKey?: string, serviceID?: string, templateID?: string) => {
2-
if (!publicKey) {
1+
export const validateParams = (publicKey?: unknown, serviceID?: unknown, templateID?: unknown) => {
2+
if (!publicKey || typeof publicKey !== 'string') {
33
throw 'The public key is required. Visit https://dashboard.emailjs.com/admin/account';
44
}
55

6-
if (!serviceID) {
6+
if (!serviceID || typeof serviceID !== 'string') {
77
throw 'The service ID is required. Visit https://dashboard.emailjs.com/admin';
88
}
99

10-
if (!templateID) {
10+
if (!templateID || typeof templateID !== 'string') {
1111
throw 'The template ID is required. Visit https://dashboard.emailjs.com/admin/templates';
1212
}
1313
};

0 commit comments

Comments
 (0)