{"record":{"id":"e5072fc6d819e62b","repo":"jquense/yup","slug":"either-then-or-otherwise-is-required-for-wh","errorCode":null,"errorMessage":"either `then:` or `otherwise:` is required for `when()` conditions","messagePattern":"either `then:` or `otherwise:` is required for `when\\(\\)` conditions","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/Condition.ts","lineNumber":31,"sourceCode":"  then?: (schema: T) => ISchema<any>;\n  otherwise?: (schema: T) => ISchema<any>;\n};\n\nexport type ResolveOptions<TContext = any> = {\n  value?: any;\n  parent?: any;\n  context?: TContext;\n};\n\nclass Condition<TIn extends ISchema<any, any> = ISchema<any, any>> {\n  fn: ConditionBuilder<TIn>;\n\n  static fromOptions<TIn extends ISchema<any, any>>(\n    refs: Reference[],\n    config: ConditionConfig<TIn>,\n  ) {\n    if (!config.then && !config.otherwise)\n      throw new TypeError(\n        'either `then:` or `otherwise:` is required for `when()` conditions',\n      );\n\n    let { is, then, otherwise } = config;\n\n    let check =\n      typeof is === 'function'\n        ? is\n        : (...values: any[]) => values.every((value) => value === is);\n\n    return new Condition<TIn>(refs, (values, schema: any) => {\n      let branch = check(...values) ? then : otherwise;\n\n      return branch?.(schema) ?? schema;\n    });\n  }\n\n  constructor(","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/jquense/yup/blob/ff31eee8a2b10c938bb3a22544d9e33957a8cf01/src/Condition.ts#L13-L49","documentation":"Condition.fromOptions() builds a conditional schema branch from a `when()` call, and requires at least one of `then` or `otherwise` to know what schema to yield when `is` matches or fails. If neither is supplied there is nothing to resolve, so yup throws this TypeError immediately at condition-construction time. It is a configuration mistake, caught before any validation runs.","triggerScenarios":"Calling schema.when('other', { is: true }) (any config object with `is` but neither `then` nor `otherwise`), or when(refs, { is, then: undefined, otherwise: undefined }) explicitly.","commonSituations":"Building dynamic config objects where then/otherwise are spread from optional values that turn out undefined; copy-pasting a when() example and deleting one branch; generating conditions programmatically with optional branches.","solutions":["Provide at least one branch: add `then:` or `otherwise:` to the when() options object.","If a branch should be a no-op, set it explicitly, e.g. otherwise: (s) => s or then: yup.string().notRequired().","Check that the config object is not being spread with undefined keys overwriting real ones."],"exampleFix":"// before\nschema.when('age', { is: (v) => v >= 18 })\n// after\nschema.when('age', { is: (v) => v >= 18, then: (s) => s.required(), otherwise: (s) => s.notRequired() })","handlingStrategy":"validation","validationCode":"function validateWhenConfig(cfg) {\n  if (cfg && cfg.is != null && !cfg.then && !cfg.otherwise)\n    throw new TypeError('when() needs a `then` or `otherwise` branch');\n}","typeGuard":"const hasBranch = (c) => c != null && (typeof c.then !== 'undefined' || typeof c.otherwise !== 'undefined');","tryCatchPattern":null,"preventionTips":["Always provide both then and otherwise branches for clarity.","Build when() configs via a helper that enforces at least one branch.","Type the config in TS so then/otherwise are required keys."],"tags":["yup","schema","condition","configuration"],"backgroundTag":"invalid-schema-condition-config","analyzedSha":"ff31eee8a2b10c938bb3a22544d9e33957a8cf01","analyzedAt":"2026-08-31T21:51:03.773Z","schemaVersion":2},"datasetVersion":"2026-08-31T22:30:34.772Z"}