jquense/yup · error · TypeError
The value of ${options.path || 'field'} could not be cast to
Error message
The value of ${options.path || 'field'} could not be cast to a value that satisfies the schema type: "${resolvedSchema.type}".
attempted value: ${formattedValue}
result of cast: ${formattedResult} What it means
When a value cannot be cast into a value satisfying the schema's type, cast() throws this TypeError detailing the schema type, the attempted value, and the cast result. It wraps the underlying cast failure with debugging context including the field path from options.path.
Source
Thrown at src/schema.ts:398
let resolvedSchema = this.resolve({
...options,
value,
// parent: options.parent,
// context: options.context,
});
let allowOptionality = options.assert === 'ignore-optionality';
let result = resolvedSchema._cast(value, options as any);
if (options.assert !== false && !resolvedSchema.isType(result)) {
if (allowOptionality && isAbsent(result)) {
return result as any;
}
let formattedValue = printValue(value);
let formattedResult = printValue(result);
throw new TypeError(
`The value of ${
options.path || 'field'
} could not be cast to a value ` +
`that satisfies the schema type: "${resolvedSchema.type}". \n\n` +
`attempted value: ${formattedValue} \n` +
(formattedResult !== formattedValue
? `result of cast: ${formattedResult}`
: ''),
);
}
return result;
}
protected _cast(rawValue: any, options: CastOptions<TContext>): any {
let value =
rawValue === undefined
? rawValueView on GitHub (pinned to ff31eee8a2)
Solutions
- Validate the input first with schema.isValid()/validate() before calling cast()
- Ensure the value can be coerced: numbers accept numeric strings, undefined via default()
- Allow the failing case explicitly with .nullable(), .default(), or .typeError()
- Fix upstream data so it matches the expected type
Example fix
// before
const n = yup.number().cast('abc'); // throws
// after
const parsed = yup.number().cast('42'); // 42
const safe = parsedInput !== undefined ? yup.number().cast(parsedInput) : undefined; Defensive patterns
Strategy: validation
Validate before calling
const ok = await schema.isValid(rawInput); const value = ok ? schema.cast(rawInput) : undefined;
Type guard
function isCastable(schema: yup.AnySchema, v: unknown): boolean {
try { schema.cast(v); return true; } catch { return false; }
} Try / catch
let value;
try { value = schema.cast(input); } catch (e) { if (e instanceof TypeError) value = schema.getDefault(); else throw e; } Prevention
- Always isValid()/validate() before cast() on untrusted input
- Add .typeError() and .default() to make cast behavior explicit
- Never cast raw querystring/form values without a numeric check
When it happens
Trigger: yup.number().cast('abc'), cast(null) on a schema with no nullable(), or casting an object into a string schema; also occurs when custom cast functions return invalid values.
Common situations: Casting untrusted form/querystring input where the value is a string like 'not-a-number'; deserializing JSON where a field's type drifted; calling cast() before validating user data.
Related errors
- Only valid options for round() are: ${avail.join(', ')}
- You cannot `concat()` schema's of different types: ${this.ty
- `test` is a required parameters
- Exclusive tests must provide a unique `name` identifying the
- Must include `key` or `index` for nested validations
AI-assisted analysis of jquense/yup@ff31eee8a2 (2026-08-31).
Data as JSON: /api/errors/e91042ca4dc80cc9.
Report an issue: GitHub.