{"record":{"id":"e30342f6428de662","repo":"Automattic/mongoose","slug":"cast-to-boolean-failed-for-value-value-type","errorCode":null,"errorMessage":"Cast to Boolean failed for value \"${value}\" (type ${valueType}) at path \"${path}\"","messagePattern":"Cast to Boolean failed for value \"(.+?)\" \\(type (.+?)\\) at path \"(.+?)\"","errorType":"exception","errorClass":"CastError","httpStatus":null,"severity":"error","filePath":"lib/schema/boolean.js","lineNumber":235,"sourceCode":" * @param {object} value\n * @param {object} model this value is optional\n * @api private\n */\n\nSchemaBoolean.prototype.cast = function(value) {\n  let castBoolean;\n  if (typeof this._castFunction === 'function') {\n    castBoolean = this._castFunction;\n  } else if (typeof this.constructor.cast === 'function') {\n    castBoolean = this.constructor.cast();\n  } else {\n    castBoolean = SchemaBoolean.cast();\n  }\n\n  try {\n    return castBoolean(value);\n  } catch (error) {\n    throw new CastError('Boolean', value, this.path, error, this);\n  }\n};\n\nconst $conditionalHandlers = { ...SchemaType.prototype.$conditionalHandlers };\n\n/**\n * Contains the handlers for different query operators for this schema type.\n * For example, `$conditionalHandlers.$in` is the function Mongoose calls to cast `$in` filter operators.\n *\n * @property $conditionalHandlers\n * @memberOf SchemaBoolean\n * @instance\n * @api public\n */\n\nObject.defineProperty(SchemaBoolean.prototype, '$conditionalHandlers', {\n  enumerable: false,\n  value: $conditionalHandlers","sourceCodeStart":217,"sourceCodeEnd":253,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/schema/boolean.js#L217-L253","documentation":"SchemaBoolean.cast accepts booleans plus values registered in the convertToTrue/convertToFalse maps (0/1, '0'/'1', 'true'/'false', 'yes'/'no' by default); anything else is rejected as CastError Boolean rather than guessed truthiness.","triggerScenarios":"`doc.active = 'on'` (an HTML checkbox value); `doc.active = 2`; `doc.active = 'maybe'`; localized truthy words like 'si'/'oui' not present in the maps.","commonSituations":"HTML forms sending checkbox 'on' values; i18n truthy words; APIs serializing booleans as 'Y'/'N' or 1/2 codes.","solutions":["Normalize at the boundary: map 'on'/'checked' to true, 'off'/absent to false before assignment.","Extend the maps: mongoose.Schema.Types.Boolean.convertToTrue.add('on') and convertToFalse.add('off') - they are Sets.","Reject with a 400 when the value is not in your accepted boolean vocabulary."],"exampleFix":"// before\ndoc.subscribed = req.body.subscribed; // 'on' -> CastError Boolean\n\n// after (once at bootstrap):\nmongoose.Schema.Types.Boolean.convertToTrue.add('on');\nmongoose.Schema.Types.Boolean.convertToFalse.add('off');\n// then:\ndoc.subscribed = req.body.subscribed;","handlingStrategy":"validation","validationCode":"const toBool = (v) => {\n  if (typeof v === 'boolean') return v;\n  if (mongoose.Schema.Types.Boolean.convertToTrue.has(v)) return true;\n  if (mongoose.Schema.Types.Boolean.convertToFalse.has(v)) return false;\n  throw new TypeError(`not a recognized boolean: ${String(v)}`);\n};\ndoc.subscribed = toBool(req.body.subscribed);","typeGuard":"const isCastableBoolean = (v) =>\n  typeof v === 'boolean' ||\n  mongoose.Schema.Types.Boolean.convertToTrue.has(v) ||\n  mongoose.Schema.Types.Boolean.convertToFalse.has(v);","tryCatchPattern":null,"preventionTips":["Convert form encodings ('on'/'off', checkbox presence) to real booleans in the controller.","Register extra truthy/falsy words once at bootstrap if your users send them.","Keep one shared boolean vocabulary across API and schema."],"tags":["cast-error","boolean","form-data","input-validation"],"backgroundTag":"mongoose-cast-error","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}