can1357/oh-my-pi · error · OmpTypeError

cannot apply max to ${ir.k}

Error message

cannot apply max to ${ir.k}

What it means

`.max()` only applies to string and number schemas; calling it on any other kind (object, boolean, date, array-of-union, etc.) throws this OmpTypeError. The message interpolates the actual IR kind (`ir.k`) of the schema it was called on. Thrown from `max()` at packages/omptype/src/zod.ts:157.

Source

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

				if (Number.isNaN(bound)) throw new OmpTypeError("number min must not be NaN");
				if (ir.min !== undefined && ir.min >= bound) return next(restrictBase(schema, ir));
				return next(restrictBase(schema, { ...ir, min: bound, xmin: false }));
			}
			throw new OmpTypeError(`cannot apply min to ${ir.k}`);
		},
		max(bound: number): ZodLikeSchema<Out> {
			const ir = schema.ir;
			if (ir.k === "string" || ir.k === "array") {
				lengthBound("max", schema, bound);
				const max = ir.max === undefined ? bound : Math.min(ir.max, bound);
				return next(restrictBase(schema, { ...ir, max }));
			}
			if (ir.k === "number") {
				if (Number.isNaN(bound)) throw new OmpTypeError("number max must not be NaN");
				if (ir.max !== undefined && ir.max <= bound) return next(restrictBase(schema, ir));
				return next(restrictBase(schema, { ...ir, max: bound, xmax: false }));
			}
			throw new OmpTypeError(`cannot apply max to ${ir.k}`);
		},
		int(): ZodLikeSchema<Out> {
			if (schema.ir.k !== "number") throw new OmpTypeError(`cannot apply int to ${schema.ir.k}`);
			return next(restrictBase(schema, { ...schema.ir, int: true }));
		},
		positive(): ZodLikeSchema<Out> {
			if (schema.ir.k !== "number") throw new OmpTypeError(`cannot apply positive to ${schema.ir.k}`);
			const ir = schema.ir;
			if (ir.min !== undefined && ir.min > 0) return next(restrictBase(schema, ir));
			return next(restrictBase(schema, { ...ir, min: 0, xmin: true }));
		},
		nonnegative(): ZodLikeSchema<Out> {
			if (schema.ir.k !== "number") throw new OmpTypeError(`cannot apply nonnegative to ${schema.ir.k}`);
			return this.min(0);
		},
		regex(expression: RegExp, message?: string): ZodLikeSchema<Out> {
			if (schema.ir.k !== "string") throw new OmpTypeError(`cannot apply regex to ${schema.ir.k}`);
			const expectation = message ?? `matching ${expression}`;

View on GitHub (pinned to 9690622007)

Solutions

  1. Ensure the schema is a string or number schema before calling .max()
  2. Move the .max() call to the correct field's schema in the shape
  3. If writing generic code, branch on schema kind (or use a type-level constraint) before applying bounds

Example fix

// before
t.object({ enabled: t.boolean() }).max(10);
// after
t.object({ enabled: t.boolean() });
// or apply max to the numeric field:
t.object({ count: t.number().max(10) });
Defensive patterns

Strategy: type-guard

Validate before calling

function canApplyMax(schema: ZodLikeSchema<unknown>): boolean {
  const k = schema.ir.k;
  return k === "string" || k === "number" || k === "array";
}
if (!canApplyMax(schema)) throw new Error("schema kind does not support max");

Type guard

function isBoundedKind(ir: { k: string }): ir is { k: "string" | "number" | "array" } {
  return ir.k === "string" || ir.k === "number" || ir.k === "array";
}

Try / catch

try {
  schema = base.max(n);
} catch (err) {
  if (err instanceof OmpTypeError && err.message.startsWith("cannot apply max")) {
    throw new Error(`bug: .max() applied to schema of kind ${base.ir.k}`);
  } else throw err;
}

Prevention

When it happens

Trigger: `t.boolean().max(1)`, `t.object({...}).max(5)`, or any schema whose underlying IR kind is not "string", "array", or "number" having `.max()` called on it.

Common situations: Copy-pasted validation chains where the schema kind was changed but the `.max()` call was left behind; generic helper functions that apply bounds without knowing the schema kind; refactors where a field switched from string to boolean.

Related errors


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