{"record":{"id":"e91042ca4dc80cc9","repo":"jquense/yup","slug":"the-value-of-options-path-field-could-not","errorCode":null,"errorMessage":"The value of ${options.path || 'field'} could not be cast to a value that satisfies the schema type: \"${resolvedSchema.type}\". \n\nattempted value: ${formattedValue} \nresult of cast: ${formattedResult}","messagePattern":"The value of (.+?) could not be cast to a value that satisfies the schema type: \"(.+?)\"\\. \n\nattempted value: (.+?) \nresult of cast: (.+?)","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/schema.ts","lineNumber":398,"sourceCode":"    let resolvedSchema = this.resolve({\n      ...options,\n      value,\n      // parent: options.parent,\n      // context: options.context,\n    });\n    let allowOptionality = options.assert === 'ignore-optionality';\n\n    let result = resolvedSchema._cast(value, options as any);\n\n    if (options.assert !== false && !resolvedSchema.isType(result)) {\n      if (allowOptionality && isAbsent(result)) {\n        return result as any;\n      }\n\n      let formattedValue = printValue(value);\n      let formattedResult = printValue(result);\n\n      throw new TypeError(\n        `The value of ${\n          options.path || 'field'\n        } could not be cast to a value ` +\n          `that satisfies the schema type: \"${resolvedSchema.type}\". \\n\\n` +\n          `attempted value: ${formattedValue} \\n` +\n          (formattedResult !== formattedValue\n            ? `result of cast: ${formattedResult}`\n            : ''),\n      );\n    }\n\n    return result;\n  }\n\n  protected _cast(rawValue: any, options: CastOptions<TContext>): any {\n    let value =\n      rawValue === undefined\n        ? rawValue","sourceCodeStart":380,"sourceCodeEnd":416,"githubUrl":"https://github.com/jquense/yup/blob/ff31eee8a2b10c938bb3a22544d9e33957a8cf01/src/schema.ts#L380-L416","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nconst n = yup.number().cast('abc'); // throws\n// after\nconst parsed = yup.number().cast('42'); // 42\nconst safe = parsedInput !== undefined ? yup.number().cast(parsedInput) : undefined;","handlingStrategy":"validation","validationCode":"const ok = await schema.isValid(rawInput);\nconst value = ok ? schema.cast(rawInput) : undefined;","typeGuard":"function isCastable(schema: yup.AnySchema, v: unknown): boolean {\n  try { schema.cast(v); return true; } catch { return false; }\n}","tryCatchPattern":"let value;\ntry { value = schema.cast(input); } catch (e) { if (e instanceof TypeError) value = schema.getDefault(); else throw e; }","preventionTips":["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"],"tags":["typeerror","casting","schema-validation"],"backgroundTag":"type-cast-failed","analyzedSha":"ff31eee8a2b10c938bb3a22544d9e33957a8cf01","analyzedAt":"2026-08-31T21:51:03.773Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}