Skip to content
1 min read · 161 words

Function: validateOrThrow() ​

ts
function validateOrThrow<T>(
  schema: Schema,
  value: unknown,
  convert?: boolean,
): T;

Defined in: src/lib/utils/validation.ts:113

Validates value against schema synchronously and returns the coerced result typed as T.

Type Parameters ​

Type ParameterDescription
TThe expected type of value after successful validation.

Parameters ​

ParameterTypeDefault valueDescription
schemaSchemaundefinedThe schema to validate against.
valueunknownundefinedThe value to validate.
convertbooleanfalseWhen true, the validator coerces values to their target types (e.g. string "1" → number 1). Defaults to false to prevent silent type coercion.

Returns ​

T

The validated (and optionally coerced) value typed as T.

Remarks ​

Collects all field errors before throwing. Use asyncValidateOrThrow for schemas that include async custom validators.

Throws ​

ValidationException when value does not satisfy schema.