Skip to content

Commit 7812519

Browse files
committed
feat: add input validation to go() method
1 parent b3bdd88 commit 7812519

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

src/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,20 @@ export class Cacheism {
2121
status: number,
2222
callback: (existing: Hit | Miss) => Promise<unknown>
2323
): Promise<Hit | Miss> {
24+
// Input validation
25+
if (typeof cacheDomain !== 'string') {
26+
throw new TypeError('cacheDomain must be a string');
27+
}
28+
if (typeof cachePath !== 'string') {
29+
throw new TypeError('cachePath must be a string');
30+
}
31+
if (typeof status !== 'number' || status < 0 || status > 3) {
32+
throw new TypeError('status must be a valid Status value (0-3)');
33+
}
34+
if (typeof callback !== 'function') {
35+
throw new TypeError('callback must be a function');
36+
}
37+
2438
const name = this.cacheName(cacheDomain, cachePath);
2539
let existing: Hit | Miss = new Miss(name, 'Missing cache', 0);
2640
const hasCache = await this.store.isset(name);

test/validation.cjs

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
const expect = require('expect.js');
2+
3+
const { Cacheism } = require('../dist/index.cjs');
4+
const cache = new Cacheism(Cacheism.store.memory());
5+
6+
describe('input validation', function() {
7+
8+
describe('cacheDomain', function() {
9+
10+
it('should throw TypeError when cacheDomain is null', async function() {
11+
try {
12+
await cache.go(null, 'path', Cacheism.Status.onlyFresh, async () => 'data');
13+
expect().fail('Expected TypeError to be thrown');
14+
} catch (err) {
15+
expect(err).to.be.a(TypeError);
16+
expect(err.message).to.be('cacheDomain must be a string');
17+
}
18+
});
19+
20+
it('should throw TypeError when cacheDomain is undefined', async function() {
21+
try {
22+
await cache.go(undefined, 'path', Cacheism.Status.onlyFresh, async () => 'data');
23+
expect().fail('Expected TypeError to be thrown');
24+
} catch (err) {
25+
expect(err).to.be.a(TypeError);
26+
expect(err.message).to.be('cacheDomain must be a string');
27+
}
28+
});
29+
30+
it('should throw TypeError when cacheDomain is a number', async function() {
31+
try {
32+
await cache.go(123, 'path', Cacheism.Status.onlyFresh, async () => 'data');
33+
expect().fail('Expected TypeError to be thrown');
34+
} catch (err) {
35+
expect(err).to.be.a(TypeError);
36+
expect(err.message).to.be('cacheDomain must be a string');
37+
}
38+
});
39+
40+
it('should throw TypeError when cacheDomain is an object', async function() {
41+
try {
42+
await cache.go({}, 'path', Cacheism.Status.onlyFresh, async () => 'data');
43+
expect().fail('Expected TypeError to be thrown');
44+
} catch (err) {
45+
expect(err).to.be.a(TypeError);
46+
expect(err.message).to.be('cacheDomain must be a string');
47+
}
48+
});
49+
50+
});
51+
52+
describe('cachePath', function() {
53+
54+
it('should throw TypeError when cachePath is null', async function() {
55+
try {
56+
await cache.go('domain', null, Cacheism.Status.onlyFresh, async () => 'data');
57+
expect().fail('Expected TypeError to be thrown');
58+
} catch (err) {
59+
expect(err).to.be.a(TypeError);
60+
expect(err.message).to.be('cachePath must be a string');
61+
}
62+
});
63+
64+
it('should throw TypeError when cachePath is undefined', async function() {
65+
try {
66+
await cache.go('domain', undefined, Cacheism.Status.onlyFresh, async () => 'data');
67+
expect().fail('Expected TypeError to be thrown');
68+
} catch (err) {
69+
expect(err).to.be.a(TypeError);
70+
expect(err.message).to.be('cachePath must be a string');
71+
}
72+
});
73+
74+
it('should throw TypeError when cachePath is a number', async function() {
75+
try {
76+
await cache.go('domain', 123, Cacheism.Status.onlyFresh, async () => 'data');
77+
expect().fail('Expected TypeError to be thrown');
78+
} catch (err) {
79+
expect(err).to.be.a(TypeError);
80+
expect(err.message).to.be('cachePath must be a string');
81+
}
82+
});
83+
84+
it('should throw TypeError when cachePath is an array', async function() {
85+
try {
86+
await cache.go('domain', [], Cacheism.Status.onlyFresh, async () => 'data');
87+
expect().fail('Expected TypeError to be thrown');
88+
} catch (err) {
89+
expect(err).to.be.a(TypeError);
90+
expect(err.message).to.be('cachePath must be a string');
91+
}
92+
});
93+
94+
});
95+
96+
describe('status', function() {
97+
98+
it('should throw TypeError when status is negative', async function() {
99+
try {
100+
await cache.go('domain', 'path', -1, async () => 'data');
101+
expect().fail('Expected TypeError to be thrown');
102+
} catch (err) {
103+
expect(err).to.be.a(TypeError);
104+
expect(err.message).to.be('status must be a valid Status value (0-3)');
105+
}
106+
});
107+
108+
it('should throw TypeError when status is greater than 3', async function() {
109+
try {
110+
await cache.go('domain', 'path', 4, async () => 'data');
111+
expect().fail('Expected TypeError to be thrown');
112+
} catch (err) {
113+
expect(err).to.be.a(TypeError);
114+
expect(err.message).to.be('status must be a valid Status value (0-3)');
115+
}
116+
});
117+
118+
it('should throw TypeError when status is a string', async function() {
119+
try {
120+
await cache.go('domain', 'path', 'onlyFresh', async () => 'data');
121+
expect().fail('Expected TypeError to be thrown');
122+
} catch (err) {
123+
expect(err).to.be.a(TypeError);
124+
expect(err.message).to.be('status must be a valid Status value (0-3)');
125+
}
126+
});
127+
128+
it('should throw TypeError when status is null', async function() {
129+
try {
130+
await cache.go('domain', 'path', null, async () => 'data');
131+
expect().fail('Expected TypeError to be thrown');
132+
} catch (err) {
133+
expect(err).to.be.a(TypeError);
134+
expect(err.message).to.be('status must be a valid Status value (0-3)');
135+
}
136+
});
137+
138+
it('should throw TypeError when status is undefined', async function() {
139+
try {
140+
await cache.go('domain', 'path', undefined, async () => 'data');
141+
expect().fail('Expected TypeError to be thrown');
142+
} catch (err) {
143+
expect(err).to.be.a(TypeError);
144+
expect(err.message).to.be('status must be a valid Status value (0-3)');
145+
}
146+
});
147+
148+
});
149+
150+
describe('callback', function() {
151+
152+
it('should throw TypeError when callback is null', async function() {
153+
try {
154+
await cache.go('domain', 'path', Cacheism.Status.onlyFresh, null);
155+
expect().fail('Expected TypeError to be thrown');
156+
} catch (err) {
157+
expect(err).to.be.a(TypeError);
158+
expect(err.message).to.be('callback must be a function');
159+
}
160+
});
161+
162+
it('should throw TypeError when callback is undefined', async function() {
163+
try {
164+
await cache.go('domain', 'path', Cacheism.Status.onlyFresh, undefined);
165+
expect().fail('Expected TypeError to be thrown');
166+
} catch (err) {
167+
expect(err).to.be.a(TypeError);
168+
expect(err.message).to.be('callback must be a function');
169+
}
170+
});
171+
172+
it('should throw TypeError when callback is a string', async function() {
173+
try {
174+
await cache.go('domain', 'path', Cacheism.Status.onlyFresh, 'not a function');
175+
expect().fail('Expected TypeError to be thrown');
176+
} catch (err) {
177+
expect(err).to.be.a(TypeError);
178+
expect(err.message).to.be('callback must be a function');
179+
}
180+
});
181+
182+
it('should throw TypeError when callback is a number', async function() {
183+
try {
184+
await cache.go('domain', 'path', Cacheism.Status.onlyFresh, 123);
185+
expect().fail('Expected TypeError to be thrown');
186+
} catch (err) {
187+
expect(err).to.be.a(TypeError);
188+
expect(err.message).to.be('callback must be a function');
189+
}
190+
});
191+
192+
it('should throw TypeError when callback is an object', async function() {
193+
try {
194+
await cache.go('domain', 'path', Cacheism.Status.onlyFresh, {});
195+
expect().fail('Expected TypeError to be thrown');
196+
} catch (err) {
197+
expect(err).to.be.a(TypeError);
198+
expect(err.message).to.be('callback must be a function');
199+
}
200+
});
201+
202+
});
203+
204+
});

0 commit comments

Comments
 (0)