{"record":{"id":"61858463aaaf3a46","repo":"jquense/yup","slug":"ref-must-be-a-string-got","errorCode":null,"errorMessage":"ref must be a string, got: ","messagePattern":"ref must be a string, got: ","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/Reference.ts","lineNumber":34,"sourceCode":") {\n  return new Reference<TValue>(key, options);\n}\n\nexport default class Reference<TValue = unknown> {\n  readonly key: string;\n  readonly isContext: boolean;\n  readonly isValue: boolean;\n  readonly isSibling: boolean;\n  readonly path: any;\n\n  readonly getter: (data: unknown) => unknown;\n  readonly map?: (value: unknown) => TValue;\n\n  declare readonly __isYupRef: boolean;\n\n  constructor(key: string, options: ReferenceOptions<TValue> = {}) {\n    if (typeof key !== 'string')\n      throw new TypeError('ref must be a string, got: ' + key);\n\n    this.key = key.trim();\n\n    if (key === '') throw new TypeError('ref must be a non-empty string');\n\n    this.isContext = this.key[0] === prefixes.context;\n    this.isValue = this.key[0] === prefixes.value;\n    this.isSibling = !this.isContext && !this.isValue;\n\n    let prefix = this.isContext\n      ? prefixes.context\n      : this.isValue\n      ? prefixes.value\n      : '';\n\n    this.path = this.key.slice(prefix.length);\n    this.getter = this.path && getter(this.path, true);\n    this.map = options.map;","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/jquense/yup/blob/ff31eee8a2b10c938bb3a22544d9e33957a8cf01/src/Reference.ts#L16-L52","documentation":"Reference (created via yup.ref()) models a pointer to another field and requires its key to be a string, since the key is trimmed and parsed for `$`/`this` prefixes. Passing a non-string (number, object, undefined) means the reference path cannot be built, so the constructor throws this TypeError including the offending value. It fails fast at ref creation, before any validation.","triggerScenarios":"yup.ref(42), yup.ref(someVariable) where the variable is undefined/null/number, or programmatic ref paths built from non-string data (e.g. JSON config numbers).","commonSituations":"Dynamic field paths from numeric indices (`ref(i)` instead of `` ref(`${i}`) ``); destructured options with undefined path; reading paths from query params or env without coercion to string.","solutions":["Pass a string to yup.ref(): convert numbers with String(i) or template literals.","Guard the variable before calling: ensure it is defined and a string.","If the path is built dynamically, coerce/join parts: yup.ref([a, b].join('.'))."],"exampleFix":"// before\nyup.ref(index)\n// after\nyup.ref(String(index))","handlingStrategy":"type-guard","validationCode":"const makeRef = (key) => {\n  if (typeof key !== 'string') throw new TypeError(`ref key must be a string, got: ${typeof key}`);\n  return yup.ref(key);\n};","typeGuard":"const isRefKey = (v) => typeof v === 'string';","tryCatchPattern":null,"preventionTips":["Coerce dynamic path parts with String() or template literals.","Type ref parameters as string in TypeScript.","Validate config-supplied paths before constructing refs."],"tags":["yup","ref","type-error","argument"],"backgroundTag":"invalid-ref-path","analyzedSha":"ff31eee8a2b10c938bb3a22544d9e33957a8cf01","analyzedAt":"2026-08-31T21:51:03.773Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}