Skip to content

num() accepts values with trailing characters like "10_000", "30s", and "0x10" and returns a truncated number #261

Closed

Description

@trackoor

Since #160 switched num() from +input to parseFloat(input) to reject empty strings, num() stops parsing at the first character parseFloat does not recognize instead of rejecting the value.

// test.js
const { cleanEnv, num } = require("envalid");

const env = cleanEnv(
  { MAX_ITEMS: "10_000", TIMEOUT: "30s", RATIO: "0x10", VERSION: "1.2.3" },
  { MAX_ITEMS: num(), TIMEOUT: num(), RATIO: num(), VERSION: num() },
);
console.log(env.MAX_ITEMS, env.TIMEOUT, env.RATIO, env.VERSION);
$ node test.js
10 30 0 1.2

Actual: cleanEnv succeeds and returns 10, 30, 0, and 1.2.

Expected: cleanEnv should throw an EnvError for malformed numeric strings like "10_000", "30s", and "1.2.3" (and either parse "0x10" as 16 or reject it), matching the README description of num() ("42", "0.23", "1e5"). Checking input.trim() === '' before coercing with Number(input) would reject both empty strings and trailing characters.

Tested on envalid 9.0.0 (main commit 8ab2075), Node v26.13.0.

Credit: https://ucsbplse.github.io/

Activity

  1. af commented on Nov 10, 2026

    @af
    Owner

    @trackoor thanks, #262 should fix this, lmk if there are any cases it doesn't cover

  2. trackoor commented on Nov 10, 2026

    @trackoor
    Author

    Thanks for the quick fix! #262 covers all the cases I had, closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions