getValid()function
getValid(value: unknown, validator: Validator<T>): T | undefined
| Param | Type | |
|---|---|---|
value | unknown | The unsafe value to validate. required |
validator | Validator<T> | The validator to validate the value with. required |
| Return | |
|---|---|
T | undefined | The valid value, or undefined if validation threw a string message. |
| Throws | |
|---|---|
unknown | Error if the validator throws a non-string error. |
Validate a value with a given validator, or return undefined if the value could not be validated.
- Non-string thrown errors (i.e. real
Errorinstances) are rethrown — only string validation messages are swallowed intoundefined.
Examples
getValid(unsafeValue, STRING) // valid string, or `undefined`