can1357/oh-my-pi · error · OmpTypeError

enum requires at least one value

Error message

enum requires at least one value

What it means

`omptype.enum()` requires at least one value to enumerate over; an empty value set has no members and cannot validate anything, so it throws an OmpTypeError. Thrown from `enumSchema` at packages/omptype/src/zod.ts:291 when the values array has length 0.

Source

Thrown at packages/omptype/src/zod.ts:291

			prop.def = member.defaultValue;
			prop.defFactory = typeof member.defaultValue === "function";
		}
		props.push(prop);
	}
	return decorateUnknown(schemaFromIR<unknown>({ k: "object", props, extras: "delete" })) as unknown as ZodLikeSchema<
		ObjectOutput<S>
	>;
}

export const string = (): ZodLikeSchema<string> => decorate(schemaFromIR(type.string.ir));
export const number = (): ZodLikeSchema<number> => decorate(schemaFromIR(type.number.ir));
export const boolean = (): ZodLikeSchema<boolean> => decorate(schemaFromIR(type.boolean.ir));
export const literal = <const Value>(value: Value): ZodLikeSchema<Value> =>
	decorate(schemaFromIR<Value>(type.enumerated(value).ir));
const enumSchema = <const Values extends readonly [string, ...string[]]>(
	values: Values,
): ZodLikeSchema<Values[number]> => {
	if (values.length === 0) throw new OmpTypeError("enum requires at least one value");
	return decorate(schemaFromIR<Values[number]>(type.enumerated(...values).ir));
};

export { enumSchema as enum };
export const union = <
	const Schemas extends readonly [ZodLikeSchema<unknown>, ZodLikeSchema<unknown>, ...ZodLikeSchema<unknown>[]],
>(
	schemas: Schemas,
): ZodLikeSchema<UnionOutput<Schemas>> =>
	decorate(schemaFromIR({ k: "union", members: schemas.map(schema => embed(schema)) }));
export const array = <Element>(element: ZodLikeSchema<Element>): ZodLikeSchema<Element[]> =>
	decorate(schemaFromIR({ k: "array", el: embed(element) }));
export const object = <const S extends Shape>(shape: S): ZodLikeSchema<Simplify<ObjectOutput<S>>> =>
	objectSchema(shape);
export const record = <Key extends string, Value>(
	keySchema: ZodLikeSchema<Key>,
	valueSchema: ZodLikeSchema<Value>,
): ZodLikeSchema<Record<string, Value>> => {

View on GitHub (pinned to 9690622007)

Solutions

  1. Pass at least one string literal: omptype.enum(["a", "b"])
  2. Check the source array for emptiness before constructing the schema and provide a default
  3. Avoid unsafe casts that silence the non-empty tuple requirement; validate the runtime array first

Example fix

// before
const statuses = config.statuses ?? [];
const schema = omptype.enum(statuses); // throws when empty
// after
const statuses = config.statuses?.length ? config.statuses : ["active"];
const schema = omptype.enum(statuses);
Defensive patterns

Strategy: validation

Validate before calling

function assertNonEmptyEnum(values: readonly string[]): void {
  if (values.length === 0) throw new Error("enum needs at least one value; check the source config/collection");
}
assertNonEmptyEnum(statuses); // before omptype.enum(statuses)

Type guard

function isNonEmptyStringTuple(values: readonly string[]): values is readonly [string, ...string[]] {
  return values.length > 0;
}

Try / catch

try {
  schema = omptype.enum(values);
} catch (err) {
  if (err instanceof OmpTypeError && err.message.includes("at least one value")) {
    schema = omptype.enum([fallbackValue]);
  } else throw err;
}

Prevention

When it happens

Trigger: `omptype.enum([])`; or `omptype.enum(someArray)` where someArray is an empty array at runtime (values filtered, config missing) even though the TypeScript signature `readonly [string, ...string[]]` demands a non-empty tuple.

Common situations: Enum values loaded from config/env/DB that end up empty; building enum schemas from dynamically collected keys where the collection is empty; casting `[] as any` to satisfy the type signature.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/6bc6302a32376b97. Report an issue: GitHub.