-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrequireString.ts
More file actions
23 lines (21 loc) · 765 Bytes
/
requireString.ts
File metadata and controls
23 lines (21 loc) · 765 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 { InvalidStringException } from "../../exception";
import { nonString } from "./nonString";
/**
* Ensures that the provided value is a string, throwing an error otherwise.
*
* @param {unknown} value - The value to validate as a string.
* @param {string} [message] - Optional custom error message if the validation fails.
* @returns {string} The validated string if the input is a valid string.
* @throws {@link InvalidStringException}
* @see {@link isString}
* @see {@link nonString}
* @see {@link requireNonEmptyString}
* @since 1.0.0
* @version 1.0.0
*/
export function requireString(value: unknown, message?: string): string {
if (nonString(value)) {
throw new InvalidStringException(message);
}
return value;
}