{"record":{"id":"a38abd571c4305d1","repo":"jquense/yup","slug":"conditions-must-return-a-schema-object","errorCode":null,"errorMessage":"conditions must return a schema object","messagePattern":"conditions must return a schema object","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/Condition.ts","lineNumber":74,"sourceCode":"\n  resolve(base: TIn, options: ResolveOptions) {\n    let values = this.refs.map((ref) =>\n      // TODO: ? operator here?\n      ref.getValue(options?.value, options?.parent, options?.context),\n    );\n\n    let schema = this.fn(values, base, options);\n\n    if (\n      schema === undefined ||\n      // @ts-ignore this can be base\n      schema === base\n    ) {\n      return base;\n    }\n\n    if (!isSchema(schema))\n      throw new TypeError('conditions must return a schema object');\n\n    return schema.resolve(options);\n  }\n}\n\nexport default Condition;\n","sourceCodeStart":56,"sourceCodeEnd":81,"githubUrl":"https://github.com/jquense/yup/blob/ff31eee8a2b10c938bb3a22544d9e33957a8cf01/src/Condition.ts#L56-L81","documentation":"Condition.resolve() evaluates the condition's `is` predicate and returns the selected branch's resolved schema. After collapsing function branches (then/otherwise can be callbacks), the resulting value must still be a yup schema; if it is not, the condition cannot proceed and this TypeError is thrown. It guards against branches that return the wrong type or functions that forget to return anything.","triggerScenarios":"then/otherwise given as a function that returns a non-schema value (or returns nothing), e.g. `.when('x', { is: true, then: (s) => { s.required(); } })` — the arrow returns undefined; or then/otherwise set to a plain value like `true`.","commonSituations":"Arrow functions with block bodies missing the return; returning a chained call on an object that is not a schema (typo like `yup.stringg()`); refactoring that replaced schemas with plain objects; TypeScript builds without strict types letting a wrong value through.","solutions":["Ensure every then/otherwise callback returns a yup schema: add `return s.required()` instead of just calling it.","Check each branch value is a yup schema (created via yup.string(), yup.object(), etc.), not a plain value or object.","Add a return-type annotation / TypeScript check on the condition builder so non-schema returns fail at compile time."],"exampleFix":"// before\n.when('other', { is: true, then: (s) => { s.required(); } })\n// after\n.when('other', { is: true, then: (s) => s.required() })","handlingStrategy":"type-guard","validationCode":"import { isSchema } from 'yup';\nbranches.forEach((b) => { if (typeof b === 'function' && !isSchema(b())) throw new Error('condition branch must return a schema'); });","typeGuard":"const returnsSchema = (fn) => isSchema(fn());","tryCatchPattern":"try {\n  schema.validateSync(value);\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('conditions must return a schema object')) {\n    // fix the then/otherwise callback return value\n  }\n  throw e;\n}","preventionTips":["Use concise arrow bodies for then/otherwise so the schema is returned implicitly.","Never end a branch callback with a void call; always return the chained schema.","Enable TypeScript return-type checking on condition builders."],"tags":["yup","schema","condition","type-error"],"backgroundTag":"invalid-schema-condition-config","analyzedSha":"ff31eee8a2b10c938bb3a22544d9e33957a8cf01","analyzedAt":"2026-08-31T21:51:03.773Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}