{"record":{"id":"936eb26bc10645d8","repo":"jquense/yup","slug":"unable-to-clone-src","errorCode":null,"errorMessage":"Unable to clone ${src}","messagePattern":"Unable to clone (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/util/cloneDeep.ts","lineNumber":40,"sourceCode":"    seen.set(src, copy);\n    for (let i = 0; i < src.length; i++) copy[i] = clone(src[i], seen);\n  } else if (src instanceof Map) {\n    // Map\n    copy = new Map();\n    seen.set(src, copy);\n    for (const [k, v] of src.entries()) copy.set(k, clone(v, seen));\n  } else if (src instanceof Set) {\n    // Set\n    copy = new Set();\n    seen.set(src, copy);\n    for (const v of src) copy.add(clone(v, seen));\n  } else if (src instanceof Object) {\n    // Object\n    copy = {};\n    seen.set(src, copy);\n    for (const [k, v] of Object.entries(src)) copy[k] = clone(v, seen);\n  } else {\n    throw Error(`Unable to clone ${src}`);\n  }\n  return copy;\n}\n\nexport default clone;\n","sourceCodeStart":22,"sourceCodeEnd":46,"githubUrl":"https://github.com/jquense/yup/blob/ff31eee8a2b10c938bb3a22544d9e33957a8cf01/src/util/cloneDeep.ts#L22-L46","documentation":"yup's cloneDeep (used when cloning schema option objects and values, e.g. default() / concat() / casting with structured cloning semantics) only knows how to clone plain objects, arrays, Date, RegExp, Map, Set and schemas. Anything else object-like that survives all instanceof checks (or non-Object exotic values reaching the else branch) hits `throw Error('Unable to clone ...')`.","triggerScenarios":"Passing an unclonable value into schema configuration that yup clones — e.g. `yup.string().default(() => someFunction)` is fine but `default(new WeakMap())`-style exotic objects, class instances that proxy weirdly, DOM nodes, or functions stored inside default/oneOf option objects; or circular structures in edge paths before the seen-map kicks in.","commonSituations":"Storing non-serializable objects (DOMNode, WeakMap, Symbol-keyed exotic, module namespace objects, worker/refs) in schema defaults or metadata; passing React refs or Vue reactive proxies into defaults; environment-specific objects (Buffer, cross-realm objects where instanceof fails).","solutions":["Replace the unclonable value with a plain serializable one, or provide it via a function: `default(() => getMyObj())` so yup clones the result at use-time — preferably a plain object.","Convert the value before it reaches the schema (e.g. WeakMap→Map, DOM node→its id, class instance→plain object via toJSON/spread).","If the value is a legit plain object failing instanceof due to cross-realm origins, normalize it with Object.assign({}, src) before use.","Check recent changes: often a new default()/meta() input introduced the exotic value; log the value shown in the message and trace its origin."],"exampleFix":"// before\nconst schema = yup.object().shape({\n  ctx: yup.mixed().default(weakRefContext), // Unable to clone [object WeakMap]\n});\n// after\nconst schema = yup.object().shape({\n  ctx: yup.mixed().default(() => ({ id: weakRefContext.id })),\n});","handlingStrategy":"validation","validationCode":"// ensure schema default/oneOf values are clonable before use\nfunction isClonable(v: unknown): boolean {\n  if (v == null || typeof v !== 'object') return true;\n  if (v instanceof Date || v instanceof RegExp || Array.isArray(v) || v instanceof Map || v instanceof Set) return true;\n  if (v.constructor === Object || v.constructor == null) return true;\n  return false; // class instances, WeakMaps, DOM nodes, functions-in-containers are risky\n}","typeGuard":"function isPlainObject(v: unknown): v is Record<string, unknown> {\n  return typeof v === 'object' && v !== null &&\n    (Object.getPrototypeOf(v) === Object.prototype || Object.getPrototypeOf(v) === null);\n}","tryCatchPattern":"try {\n  const out = schema.cast(input);\n} catch (e) {\n  if (typeof e?.message === 'string' && e.message.startsWith('Unable to clone')) {\n    console.error('Unclonable schema option/value detected:', e.message);\n  }\n}","preventionTips":["Keep schema defaults, oneOf values, and meta limited to JSON-serializable plain data.","Pass side-effectful values as factory functions: default(() => makeObj()).","Strip exotic values (WeakMap, DOM nodes, cross-realm objects) before they enter the schema.","Test schema.cast/default paths in CI with the actual option objects used in production."],"tags":["yup","clone","deep-copy","unsupported-type","schema-options"],"backgroundTag":"unclonable-value","analyzedSha":"ff31eee8a2b10c938bb3a22544d9e33957a8cf01","analyzedAt":"2026-08-31T21:51:03.773Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}