can1357/oh-my-pi · error · OmpTypeError

cannot apply min to ${ir.k}

Error message

cannot apply min to ${ir.k}

What it means

The zod-compat `.min()` method only supports string, array, and number schemas; applying it to any other kind throws `cannot apply min to <kind>` (kind = the schema's IR kind, e.g. `boolean`, `object`, `null`, `union`). Zod's `.min()` exists mainly on strings/numbers/arrays; omptype's compat layer enforces that surface.

Source

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

				error: {
					message: result.summary,
					issues: result.map(issue => ({ path: [...issue.path], message: issue.problem })),
				},
			};
		},
		min(bound: number): ZodLikeSchema<Out> {
			const ir = schema.ir;
			if (ir.k === "string" || ir.k === "array") {
				lengthBound("min", schema, bound);
				const min = ir.min === undefined ? bound : Math.max(ir.min, bound);
				return next(restrictBase(schema, { ...ir, min }));
			}
			if (ir.k === "number") {
				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 }));

View on GitHub (pinned to 9690622007)

Solutions

  1. Remove the `.min()` call from non-string/array/number schemas — it has no meaning there.
  2. For objects, express the constraint differently (e.g. refine on key count) rather than `.min()`.
  3. In generic code, narrow by kind before applying: only call `.min()` when the schema is a string/array/number schema.

Example fix

// before
const enabled = z.boolean().min(0); // throws
// after
const enabled = z.boolean();
Defensive patterns

Strategy: type-guard

Validate before calling

// apply min only to supported kinds
const k = (schema as unknown as { ir: { k: string } }).ir.k;
if (!['string', 'array', 'number'].includes(k)) throw new Error(`cannot apply min to ${k}`);

Type guard

function supportsMin(s: ZodLikeSchema<unknown>): boolean {
  const k = (s as unknown as { ir: { k: string } }).ir.k;
  return k === "string" || k === "array" || k === "number";
}

Try / catch

try {
  const bounded = maybeSchema.min(1);
} catch (err) {
  if (err instanceof Error && err.message.startsWith('cannot apply min to')) {
    return maybeSchema; // kind doesn't support min — skip the constraint
  }
  throw err;
}

Prevention

When it happens

Trigger: `z.boolean().min(0)`, `z.object({...}).min(1)`, `.min()` on a literal/null/union schema built through the compat layer.

Common situations: Generic helper functions applying `.min()` to whichever schema they receive; refactoring a field from string to boolean while keeping `.min()` chained; zod code migrated from third-party plugins where `.min()` existed on more types.

Related errors


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