requireValid()function
requireValid(value: unknown, validator: Validator<T>, caller: AnyCaller = requireValid): T
| Param | Type | |
|---|---|---|
value | unknown | The unsafe value to validate. required |
validator | Validator<T> | The validator to validate the value with. required |
caller | AnyCaller | Function to attribute the thrown error to (defaults to requireValid). Defaults to requireValid |
| Return | |
|---|---|
T | The valid value. |
| Throws | |
|---|---|
unknown | RequiredError if validation threw a string message. |
unknown | Error if the validator throws a non-string error. |
Validate a value with a given validator, or throw RequiredError if the value could not be validated.
- String validation messages are wrapped in a
RequiredError; non-string errors are rethrown as-is.
Examples
requireValid(unsafeValue, STRING) // valid string (or throws)