Skip to content

Commit bfc377c

Browse files
authored
Merge pull request #26 from sparksuite/input-validation
Add input validation to the `validateText()` function
2 parents 8926786 + de1f06d commit bfc377c

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/validate-text.ts

+34
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,40 @@ async function validateText(
4848
options: ValidateTextOptionsWithWarnings
4949
): Promise<ValidateTextResultWithWarnings>;
5050
async function validateText(textToBeValidated: string, options?: ValidateTextOptions): Promise<ValidateTextResult> {
51+
// Validations
52+
if (!textToBeValidated) {
53+
throw new Error('You must pass in text to be validated');
54+
}
55+
56+
if (typeof textToBeValidated !== 'string') {
57+
throw new Error('The text to be validated must be a string');
58+
}
59+
60+
if (options) {
61+
const allowedMediums: typeof options['medium'][] = [
62+
'all',
63+
'braille',
64+
'embossed',
65+
'handheld',
66+
'print',
67+
'projection',
68+
'screen',
69+
'speech',
70+
'tty',
71+
'tv',
72+
];
73+
74+
if (options.medium && !allowedMediums.includes(options.medium)) {
75+
throw new Error(`The medium must be one of the following: ${allowedMediums.join(', ')}`);
76+
}
77+
78+
const allowedWarningLevels: typeof options['warningLevel'][] = [0, 1, 2, 3];
79+
80+
if (options.warningLevel && !allowedWarningLevels.includes(options.warningLevel)) {
81+
throw new Error(`The warning level must be one of the following: ${allowedWarningLevels.join(', ')}`);
82+
}
83+
}
84+
5185
// Build URL for fetching
5286
const params = {
5387
text: encodeURIComponent(textToBeValidated),

test/index.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,29 @@ describe('#validateText()', () => {
6161
errors: [],
6262
});
6363
});
64+
65+
it('Complains about missing text', async () => {
66+
// @ts-expect-error: We're trying to force an error here
67+
await expect(cssValidator.validateText()).rejects.toThrow('You must pass in text to be validated');
68+
await expect(cssValidator.validateText('')).rejects.toThrow('You must pass in text to be validated');
69+
});
70+
71+
it('Complains about text not being a string', async () => {
72+
// @ts-expect-error: We're trying to force an error here
73+
await expect(cssValidator.validateText(true)).rejects.toThrow('The text to be validated must be a string');
74+
});
75+
76+
it('Complains about invalid medium', async () => {
77+
// @ts-expect-error: We're trying to force an error here
78+
await expect(cssValidator.validateText('abc', { medium: 'fake' })).rejects.toThrow(
79+
'The medium must be one of the following:'
80+
);
81+
});
82+
83+
it('Complains about invalid warning level', async () => {
84+
// @ts-expect-error: We're trying to force an error here
85+
await expect(cssValidator.validateText('abc', { warningLevel: 'fake' })).rejects.toThrow(
86+
'The warning level must be one of the following:'
87+
);
88+
});
6489
});

0 commit comments

Comments
 (0)