can1357/oh-my-pi · error · OmpTypeError

cannot apply positive to ${schema.ir.k}

Error message

cannot apply positive to ${schema.ir.k}

What it means

`.positive()` asserts a strict lower bound (value > 0) and therefore only exists for number schemas; calling it on any other kind throws this OmpTypeError. The message interpolates the actual IR kind. Thrown from `positive()` at packages/omptype/src/zod.ts:164.

Source

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

			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}`;
			const narrowed = schema.narrow((value, ctx) => {
				expression.lastIndex = 0;
				const matches = expression.test(value as string);
				expression.lastIndex = 0;
				return matches || ctx.mustBe(expectation);
			});
			return next(narrowed);

View on GitHub (pinned to 9690622007)

Solutions

  1. Call .positive() only on t.number() schemas
  2. If the value is a numeric string, switch the field to t.number() (or transform before validating)
  3. For inclusive >= 0 semantics use .nonnegative() on a number schema instead

Example fix

// before
t.string().positive()
// after
t.number().positive()
Defensive patterns

Strategy: type-guard

Validate before calling

function canApplyPositive(schema: ZodLikeSchema<unknown>): boolean {
  return schema.ir.k === "number";
}
if (!canApplyPositive(schema)) throw new Error("positive() requires a number schema");

Type guard

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

Try / catch

try {
  schema = base.positive();
} catch (err) {
  if (err instanceof OmpTypeError && err.message.startsWith("cannot apply positive")) {
    throw new Error("field must be t.number() to use .positive()");
  } else throw err;
}

Prevention

When it happens

Trigger: `t.string().positive()`, `t.boolean().positive()`, `.positive()` on object/array/union schemas.

Common situations: Schema chains where a numeric field was retyped to string (e.g. amounts from form inputs as strings) while the sign constraint remained; generic builders that apply positivity uniformly; copy-paste between numeric and non-numeric field definitions.

Related errors


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