{"id":"759c2140359c9e92","repo":"colinhacks/zod","slug":"cannot-create-literal-schema-with-no-valid-values","errorCode":null,"errorMessage":"Cannot create literal schema with no valid values","messagePattern":"Cannot create literal schema with no valid values","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/zod/src/v4/core/schemas.ts","lineNumber":3279,"sourceCode":"}\n\nexport interface $ZodLiteralInternals<T extends util.Literal = util.Literal> extends $ZodTypeInternals<T, T> {\n  def: $ZodLiteralDef<T>;\n  values: Set<T>;\n  pattern: RegExp;\n  isst: errors.$ZodIssueInvalidValue;\n}\n\nexport interface $ZodLiteral<T extends util.Literal = util.Literal> extends $ZodType {\n  _zod: $ZodLiteralInternals<T>;\n}\n\nexport const $ZodLiteral: core.$constructor<$ZodLiteral> = /*@__PURE__*/ core.$constructor(\n  \"$ZodLiteral\",\n  (inst, def) => {\n    $ZodType.init(inst, def);\n    if (def.values.length === 0) {\n      throw new Error(\"Cannot create literal schema with no valid values\");\n    }\n\n    const values = new Set<util.Literal>(def.values);\n    inst._zod.values = values;\n    inst._zod.pattern = new RegExp(\n      `^(${def.values\n\n        .map((o) => (typeof o === \"string\" ? util.escapeRegex(o) : o ? util.escapeRegex(o.toString()) : String(o)))\n        .join(\"|\")})$`\n    );\n\n    inst._zod.parse = (payload, _ctx) => {\n      const input = payload.value;\n      if (values.has(input)) {\n        return payload;\n      }\n      payload.issues.push({\n        code: \"invalid_value\",","sourceCodeStart":3261,"sourceCodeEnd":3297,"githubUrl":"https://github.com/colinhacks/zod/blob/912f0f51b0ced654d0069741e7160834dca742ee/packages/zod/src/v4/core/schemas.ts#L3261-L3297","documentation":"Thrown in the `$ZodLiteral` constructor when `def.values` is empty. A literal schema must accept at least one concrete value; `z.literal()` with no argument, or `z.literal([])` (an empty array spread into values), produces an empty set and cannot match anything, so construction fails immediately at definition time.","triggerScenarios":"Calling `z.literal()` with no arguments; `z.literal([])`; programmatically building `z.literal([...someArray])` where `someArray` is empty; spreading a dynamic list into `z.literal(...)` that turns out empty.","commonSituations":"Generating schemas from configuration/JSON where the allowed-value list may be empty; refactoring a list of literals and accidentally passing an empty array; type-loose wrappers around `z.literal`.","solutions":["Pass at least one literal value to `z.literal(...)`, e.g. `z.literal('active')` or `z.literal(['a','b'])`.","If the value list is dynamic, guard it: only construct `z.literal(values)` when `values.length > 0`, otherwise fall back to `z.never()` or `z.any()` depending on intent.","Check the upstream data source feeding the literal list to ensure it is non-empty."],"exampleFix":"// before\nconst allowed: string[] = [];\nconst S = z.literal(allowed); // throws\n\n// after\nconst allowed: string[] = [];\nconst S = allowed.length > 0 ? z.literal(allowed) : z.never();","handlingStrategy":"validation","validationCode":"function safeLiteral(values) {\n  if (!values || (Array.isArray(values) ? values.length === 0 : values === undefined)) {\n    throw new Error('z.literal requires at least one value');\n  }\n  return z.literal(values);\n}","typeGuard":"function hasLiteralValues(values): boolean {\n  return Array.isArray(values) ? values.length > 0 : values !== undefined && values !== null;\n}","tryCatchPattern":"try {\n  const S = z.literal(dynamicValues);\n} catch (e) {\n  if (e instanceof Error && e.message === 'Cannot create literal schema with no valid values') {\n    // fall back to z.never() or ensure the source list is populated\n  }\n  throw e;\n}","preventionTips":["Guard dynamically-built literal value lists for emptiness before constructing z.literal.","Validate upstream config/data that feeds literal lists at app startup.","Default empty lists to a sentinel literal or z.never() explicitly."],"tags":["literal","schema-definition","validation"],"analyzedSha":"912f0f51b0ced654d0069741e7160834dca742ee","analyzedAt":"2026-08-03T17:41:55.908Z","schemaVersion":2}