-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.ts
More file actions
23 lines (21 loc) · 752 Bytes
/
index.ts
File metadata and controls
23 lines (21 loc) · 752 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { ValidationError } from "@threefold/types";
type AssertReturn = void | never;
/**
* @description
* Assertion
*/
export function assertHasField(fields: any): AssertReturn {
if (Object.keys(fields).length === 0) {
throw new ValidationError(`[Object] must contain at least 1 field.`);
}
for (const key in fields) {
if (fields[key] instanceof Object) assertHasField(fields[key]);
else if (fields[key] !== true)
throw new ValidationError(`Expected "${key}" of type [true | string] but got [${typeof fields[key]}]`);
}
}
export function assertID(value: any): AssertReturn {
if (value === "" || value === undefined || value === null) {
throw new ValidationError(`Expected a valid [ID] but got [${value}].`);
}
}