paralleldrive / cuid2

Next generation guids. Secure, collision-resistant ids optimized for horizontal scaling and performance.
MIT License
2.6k stars 53 forks source link

Cuid validation failing #66

Closed stctheproducer closed 1 month ago

stctheproducer commented 1 year ago

There's a bug with the validation function. It's returning true regardless of the value.

Expected

import { createId, isCuid } from '@paralleldrive/cuid2';

console.log(
  isCuid(createId()), // true
  isCuid('not a cuid'), // false
)

Actual Results

import c from '@paralleldrive/cuid2'

let id = c.createId()

console.log(id) // ehx2jphxcar7ihzxquylc8vr

c.isCuid(id) // true

c.isCuid('user') // true

c.isCuid('') // false

c.isCuid(2) // false

I'm running node v18.17.0.

patrickalima98 commented 1 year ago

The same problem here

stctheproducer commented 1 year ago

https://github.com/paralleldrive/cuid2/blob/fb07094487ba5ad038a51ef67bcb9c5f525c173b/src/index.js#L103

I just realized the function call takes in a minLength and the default is 24. I guess, I'll have to be explicit about that or just use a custom regex pattern in the code.

mittalyashu commented 2 months ago

I am using v2.2.0

cuid.isCuid('undefined') // true
ericelliott commented 1 month ago

unfortunately, the string (NOT the value), "undefined" is indeed a valid cuid.

That's just the nature of a very large domain of random alphanumeric characters.

It is working as intended, but perhaps we could hand-code some common exceptions, like "undefined".