-
Notifications
You must be signed in to change notification settings - Fork 10
/
checkOptions.mjs
28 lines (24 loc) · 875 Bytes
/
checkOptions.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// @ts-check
import createHttpError from "http-errors";
/**
* Validates options are an enumerable object that conforms to a whitelist of
* allowed keys.
* @param {{ [key: string]: unknown }} options Options to validate.
* @param {ReadonlyArray<string>} allowedKeys Allowed option keys.
* @param {string} errorMessagePrefix Error message prefix.
*/
export default function checkOptions(options, allowedKeys, errorMessagePrefix) {
if (typeof options !== "object" || options == null || Array.isArray(options))
throw createHttpError(
500,
`${errorMessagePrefix} options must be an enumerable object.`
);
const invalid = Object.keys(options).filter(
(option) => !allowedKeys.includes(option)
);
if (invalid.length)
throw createHttpError(
500,
`${errorMessagePrefix} options invalid: \`${invalid.join("`, `")}\`.`
);
}