Skip to content

[bug] : parseId() and isId() accept IDs with an empty body #24

Description

@suhailopensource

Verified on main:

parseId("user_");      // => { prefix: "user", id: "" }
isId("user_", "user"); // => true
getPrefix("user_");    // => "user"

"user_" is a prefix and a separator with no ID after it — it cannot be a value this library
produced, since every generator appends at least one random character. But all three read-side
functions report success, so code that parses an ID and then looks it up gets an empty-string
key instead of a rejection.

Intended semantics — decided, do not re-litigate in the PR

  • parseId returns undefined when the body after the separator is empty.
  • isId is unchanged — it is documented as a prefix check, and tightening it would break
    callers relying on the current behaviour. Any change there waits for 2.0.
  • getPrefix is unchanged for the same reason.

What to do

Add the empty-body guard to parseId in src/utils/validate.ts, alongside the non-string guard
added in #13. Document in the README/docs that parseId is the strict reader and isId is the
loose prefix check.

Acceptance criteria

  • parseId("user_") returns undefined; parseId("user_x") still returns
    { prefix: "user", id: "x" }.
  • Same behaviour with a custom separator: parseId("user.", ".") is undefined.
  • isId and getPrefix behaviour is untouched, with a test asserting isId("user_", "user")
    is still true so the distinction is pinned.
  • Tests in test/validate.test.ts; CHANGELOG entry under Unreleased (this is a user-visible
    behaviour change).
  • No inline comments — see CONTRIBUTING.

Activity

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

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions