{"record":{"id":"468f7ecace051294","repo":"jquense/yup","slug":"must-include-key-or-index-for-nested-validatio","errorCode":null,"errorMessage":"Must include `key` or `index` for nested validations","messagePattern":"Must include `key` or `index` for nested validations","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/schema.ts","lineNumber":540,"sourceCode":"        }\n        if (--count <= 0) {\n          nextOnce(nestedErrors);\n        }\n      });\n    }\n  }\n\n  asNestedTest({\n    key,\n    index,\n    parent,\n    parentPath,\n    originalParent,\n    options,\n  }: NestedTestConfig): RunTest {\n    const k = key ?? index;\n    if (k == null) {\n      throw TypeError('Must include `key` or `index` for nested validations');\n    }\n\n    const isIndex = typeof k === 'number';\n    let value = parent[k];\n\n    const testOptions = {\n      ...options,\n      // Nested validations fields are always strict:\n      //    1. parent isn't strict so the casting will also have cast inner values\n      //    2. parent is strict in which case the nested values weren't cast either\n      strict: true,\n      parent,\n      value,\n      originalValue: originalParent[k],\n      // FIXME: tests depend on `index` being passed around deeply,\n      //   we should not let the options.key/index bleed through\n      key: undefined,\n      // index: undefined,","sourceCodeStart":522,"sourceCodeEnd":558,"githubUrl":"https://github.com/jquense/yup/blob/ff31eee8a2b10c938bb3a22544d9e33957a8cf01/src/schema.ts#L522-L558","documentation":"yup's asNestedTest builds the per-field test for validating an object property or array element. It requires either `key` (object field name) or `index` (array position) so it can read the nested value from the parent and build the correct path. If both are null, yup cannot address the nested value and throws this TypeError instead of producing a bogus path.","triggerScenarios":"Calling low-level schema internals (e.g. schema.asNestedTest({...}) or casting/validating through internals like cast with nested schemas) without passing key/index; usually via misuse of internal APIs or a plugin/binding that composes nested schemas incorrectly rather than through normal object().shape()/array().of() usage.","commonSituations":"Custom form-library bindings (e.g. hand-rolled Formik alternatives) wiring yup internals; calling validateAt/cast on nested schemas with hand-built options objects; after upgrading yup where the internal NestedTestConfig contract changed.","solutions":["Use the public API (object().shape({...}), array().of(...), validateAt(path, value)) so yup supplies key/index itself.","If calling asNestedTest directly, pass either `key: 'fieldName'` or `index: 0` in the config object.","Check custom integrations/plugins that build NestedTestConfig and ensure the field identifier is forwarded from the parent loop.","After a yup upgrade, update internal-API usage to the current asNestedTest signature."],"exampleFix":"// before\nschema.asNestedTest({ parent: data, parentPath: 'user', originalParent: data, options });\n// after\nschema.asNestedTest({ key: 'user', parent: data, parentPath: 'user', originalParent: data, options });","handlingStrategy":"validation","validationCode":"// if you must call asNestedTest, verify config first\nfunction callNestedTest(schema, cfg) {\n  if (cfg.key == null && cfg.index == null) {\n    throw new TypeError('asNestedTest requires key or index');\n  }\n  return schema.asNestedTest(cfg);\n}","typeGuard":"function hasNestedKey(cfg: { key?: string | number; index?: number }): boolean {\n  return cfg.key != null || cfg.index != null;\n}","tryCatchPattern":"try {\n  const test = schema.asNestedTest({ key, index, parent, parentPath, originalParent, options });\n  test({ value: parent[key], options }, panic, next);\n} catch (e) {\n  if (e instanceof TypeError && /key.*index/.test(e.message)) {\n    console.error('NestedTestConfig missing key/index:', cfg);\n  }\n}","preventionTips":["Prefer public APIs (shape, of, validateAt) over internals so key/index are derived automatically.","When composing schemas in custom bindings, always forward the loop's key or index into NestedTestConfig.","Recheck internal-API calls after every yup major upgrade.","Write a smoke test that validates a nested object and array path to catch missing identifiers early."],"tags":["yup","nested-schema","typeerror","internal-api","missing-key"],"backgroundTag":"missing-required-option","analyzedSha":"ff31eee8a2b10c938bb3a22544d9e33957a8cf01","analyzedAt":"2026-08-31T21:51:03.773Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}