Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple and effective CUID regex? #118

Closed
brikou opened this issue Sep 8, 2018 · 5 comments
Closed

Simple and effective CUID regex? #118

brikou opened this issue Sep 8, 2018 · 5 comments

Comments

@brikou
Copy link

brikou commented Sep 8, 2018

I've already check isCuid method, but this fn only check that it is a string starting by "c"

Do you know a simple and effective way to check that given string look like a real CUID?

Thanks for your great lib 👍

@MarkHerhold
Copy link
Contributor

MarkHerhold commented Sep 8, 2018

See #88

Basically there are no other guarantees.

@brikou
Copy link
Author

brikou commented Sep 8, 2018

thanks @MarkHerhold

@supermacro
Copy link

supermacro commented Mar 24, 2021

Basically there are no other guarantees.

There is definitely a defined set of values that a cuid is comprised of. I'm re-posting my screenshot here from #88 to help others in case they're looking for something a bit more definitive.

Screen Shot 2021-03-24 at 7 50 53 PM

So based on the above you could have something like this:

const CODES = {
  a: 97,
  zero: 48,
}

// numerical char codes range from 48 -> 48 + 9
const NUMBERS_CHAR_CODES = Array(10).fill(null).map((_, idx) => idx + CODES.zero)

// lowercase alphabet codes
const LOWERCASE_LETTERS_CHAR_CODES = Array(26).fill(null).map((_, idx) => idx + CODES.a)

const VALID_CUID_CHAR_CODES = [ ...NUMBERS_CHAR_CODES, ...LOWERCASE_LETTERS_CHAR_CODES ]

const containsOnlyValidCuidValues = (val: string): boolean => {
  // remove 'c' char
  const tail = val.substr(1)

  return tail.split('').every(
    (char) => VALID_CUID_CHAR_CODES.includes(char.charCodeAt(0))
  )
}


// https://github.com/ericelliott/cuid/issues/88#issuecomment-339848922
const isCuid = (val: unknown): val is string =>
  typeof val === 'string' &&
  val.charAt(0) === 'c' &&
  val.length > 7 &&
  containsOnlyValidCuidValues(val)

@iki
Copy link

iki commented Oct 18, 2022

// export { isCuid } from 'cuid';

// NOTE: Future cuid may change, they are only guaranteed to start with `c` and have at least 7 characters
// See https://github.com/paralleldrive/cuid/issues/88#issuecomment-339848922
// Having generation under control, we extend the check to lowercase+digits charset and 25 characters length 
const cuidRegex = /^c[a-z0-9]{24}$/;
export const isCuid = (value: string) => cuidRegex.test(value);

@ericelliott
Copy link
Collaborator

I do not recommend using a regex to validate cuids.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants