{"record":{"id":"9c12796862f3772e","repo":"Automattic/mongoose","slug":"cast-to-boolean-failed-for-value-value-at-pat","errorCode":null,"errorMessage":"Cast to boolean failed for value \"${value}\" at path \"${path}\"","messagePattern":"Cast to boolean failed for value \"(.+?)\" at path \"(.+?)\"","errorType":"exception","errorClass":"CastError","httpStatus":null,"severity":"error","filePath":"lib/cast/boolean.js","lineNumber":28,"sourceCode":" * @param {string} [path] optional the path to set on the CastError\n * @return {boolean|null|undefined}\n * @throws {CastError} if `value` is not one of the allowed values\n * @api private\n */\n\nmodule.exports = function castBoolean(value, path) {\n  if (module.exports.convertToTrue.has(value)) {\n    return true;\n  }\n  if (module.exports.convertToFalse.has(value)) {\n    return false;\n  }\n\n  if (value == null) {\n    return value;\n  }\n\n  throw new CastError('boolean', value, path);\n};\n\nmodule.exports.convertToTrue = new Set([true, 'true', 1, '1', 'yes']);\nmodule.exports.convertToFalse = new Set([false, 'false', 0, '0', 'no']);\n","sourceCodeStart":10,"sourceCodeEnd":33,"githubUrl":"https://github.com/Automattic/mongoose/blob/49cdab01366679723b487ecb754b38570f783289/lib/cast/boolean.js#L10-L33","documentation":"castBoolean accepts exactly the values in two Sets -- true/'true'/1/'1'/'yes' map to true and false/'false'/0/'0'/'no' map to false -- while null/undefined pass through. Anything else throws CastError('boolean', value, path); the sets are case-sensitive, so 'TRUE', 'Yes', 'y', and 'on' all fail.","triggerScenarios":"doc.flag = 'y' | 'on' | 2 | 'TRUE'; casting req.query.active ('on' from a checkbox) straight into a Boolean path; Model.find({ active: 'On' }).","commonSituations":"HTML checkboxes that submit 'on'; YAML/env values like 'Yes'/'Off'; free-text boolean fields; case mismatches between client and server conventions; numeric flags other than 0/1.","solutions":["Normalize to a real boolean yourself: doc.active = req.body.active === 'on'","Restrict string inputs to the exact vocabulary: 'true'/'false'/'1'/'0'/'yes'/'no'","Extend the vocabulary once, app-wide: const castBoolean = require('mongoose/lib/cast/boolean'); castBoolean.convertToTrue.add('y'); castBoolean.convertToFalse.add('n');"],"exampleFix":"// before\ndoc.active = 'on'; // not in convertToTrue / convertToFalse\n\n// after\ndoc.active = req.body.active === 'on';","handlingStrategy":"validation","validationCode":"const castBoolean = require('mongoose/lib/cast/boolean');\nfunction isCastableBoolean(v) {\n  return v == null ||\n    castBoolean.convertToTrue.has(v) ||\n    castBoolean.convertToFalse.has(v);\n}\nif (!isCastableBoolean(req.body.active)) {\n  throw new TypeError('Invalid boolean input');\n}","typeGuard":"const castBoolean = require('mongoose/lib/cast/boolean');\nfunction isCastableBoolean(v) {\n  return v == null ||\n    castBoolean.convertToTrue.has(v) ||\n    castBoolean.convertToFalse.has(v);\n}","tryCatchPattern":"try {\n  await doc.save();\n} catch (err) {\n  if (err.name === 'CastError' && err.kind === 'boolean') {\n    // err.value shows the offending input; map it to true/false and retry\n  }\n  throw err;\n}","preventionTips":["Convert form/query booleans at the controller boundary","Document the accepted boolean vocabulary in your API contract","Add extra values ('y'/'n', 'on'/'off') to the convert sets only deliberately, app-wide"],"tags":["mongoose","boolean","cast","form-input"],"backgroundTag":"boolean-cast-failed","analyzedSha":"49cdab01366679723b487ecb754b38570f783289","analyzedAt":"2026-08-21T22:54:00.882Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}