jquense/yup · error · TypeError

Only valid options for round() are: ${avail.join(', ')}

Error message

Only valid options for round() are: ${avail.join(', ')}

What it means

yup's number.round() delegates to a Math method (round, floor, ceil, trunc). This TypeError is thrown when the optional `roundType` argument is not one of the supported method names. It fails fast instead of silently calling an undefined Math function at transform time.

Source

Thrown at src/number.ts:146

      message,
      skipAbsent: true,
      test: (val) => Number.isInteger(val),
    });
  }

  truncate() {
    return this.transform((value) => (!isAbsent(value) ? value | 0 : value));
  }

  round(method?: 'ceil' | 'floor' | 'round' | 'trunc') {
    let avail = ['ceil', 'floor', 'round', 'trunc'];
    method = (method?.toLowerCase() as any) || ('round' as const);

    // this exists for symemtry with the new Math.trunc
    if (method === 'trunc') return this.truncate();

    if (avail.indexOf(method!.toLowerCase()) === -1)
      throw new TypeError(
        'Only valid options for round() are: ' + avail.join(', '),
      );

    return this.transform((value) =>
      !isAbsent(value) ? Math[method!](value) : value,
    );
  }
}

create.prototype = NumberSchema.prototype;

//
// Number Interfaces
//

export default interface NumberSchema<
  TType extends Maybe<number> = number | undefined,
  TContext = AnyObject,

View on GitHub (pinned to ff31eee8a2)

Solutions

  1. Use one of the exact supported strings: 'round', 'floor', 'ceil', or 'trunc' (case-insensitive)
  2. Call .truncate() directly instead of .round('trunc')
  3. For custom rounding, use numberSchema.transform(v => Math.fround(v)) or a similar transform

Example fix

// before
yup.number().round('nearest');
// after
yup.number().round('round');
Defensive patterns

Strategy: validation

Validate before calling

const ROUND_MODES = ['round','floor','ceil','trunc'];
function isValidRoundMode(m) { return typeof m === 'string' && ROUND_MODES.includes(m.toLowerCase()); }
if (!isValidRoundMode(mode)) mode = 'round';

Type guard

function isRoundMode(m: unknown): m is 'round'|'floor'|'ceil'|'trunc' {
  return typeof m === 'string' && ['round','floor','ceil','trunc'].includes(m.toLowerCase() as any);
}

Try / catch

try { schema.round(mode); } catch (e) { if (e instanceof TypeError) schema.round('round'); else throw e; }

Prevention

When it happens

Trigger: Calling numberSchema.round('closer'), .round('Round'), or any string not in the avail list; the argument is lowercased before the check so only exact supported words pass.

Common situations: Typo in the rounding mode ('floorr'), copying code from another library that uses different mode names, or dynamically passing a user-supplied mode string.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of jquense/yup@ff31eee8a2 (2026-08-31). Data as JSON: /api/errors/e113c2f744f02cb9. Report an issue: GitHub.