getValid()function

getValid(value: unknown, validator: Validator<T>): T | undefined
ParamType
valueunknown
The unsafe value to validate. required
validatorValidator<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 Error instances) are rethrown — only string validation messages are swallowed into undefined.

Examples

getValid(unsafeValue, STRING) // valid string, or `undefined`