-
Notifications
You must be signed in to change notification settings - Fork 542
/
assert.ts
50 lines (47 loc) · 1.47 KB
/
assert.ts
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { AssertionError } from '@metamask/utils';
import { Struct, assert as assertSuperstruct } from 'superstruct';
export type AssertionErrorConstructor =
| (new (args: { message: string }) => Error)
| ((args: { message: string }) => Error);
/**
* Check if a value is a constructor.
*
* @param fn - The value to check.
* @returns `true` if the value is a constructor, or `false` otherwise.
*/
function isConstructable(
fn: AssertionErrorConstructor,
): fn is new (args: { message: string }) => Error {
return Boolean(typeof fn?.prototype?.constructor?.name === 'string');
}
/**
* Assert a value against a Superstruct struct.
*
* @param value - The value to validate.
* @param struct - The struct to validate against.
* @param errorPrefix - A prefix to add to the error message. Defaults to
* "Assertion failed".
* @param ErrorWrapper - The error class to throw if the assertion fails.
* Defaults to {@link AssertionError}.
* @throws If the value is not valid.
*/
export function assertStruct<T, S>(
value: unknown,
struct: Struct<T, S>,
errorPrefix = 'Assertion failed',
ErrorWrapper: AssertionErrorConstructor = AssertionError,
): asserts value is T {
try {
assertSuperstruct(value, struct);
} catch (error) {
if (isConstructable(ErrorWrapper)) {
throw new ErrorWrapper({
message: `${errorPrefix}: ${error.message}.`,
});
} else {
throw ErrorWrapper({
message: `${errorPrefix}: ${error.message}.`,
});
}
}
}