Please share effective way to validate cuid2
IamManchanda opened this issue ยท 6 comments
Not sure whether this is right place to query this "so apologies" but can you Guide me an effective way to validate cuid2?
FWIW, I came across this comment that you don't recommend validating by regex
What I was really warning against in the original cuid was trying to type check based on the structure of the id. For validation, it's fine to use a simple check:
- It's a string
- It should not exceed your maximum length (e.g. whatever max length you set for your db, or 32 chars)
- It's base36, meaning it only contains [a-z][0-9]. You should feel free to use a regex for that check.
Got it ... Thanks that helps me @ericelliott ๐
isCuid(id)
now works in v2.2.0
@ericelliott - Great but I think there is a bug in code that needs to be patched ... please check #38 (review)
Not a bug. It is not a good idea to enforce initial letter convention in application code because application code should always treat ids as opaque values. We treat ids as opaque so that if you ever need to use a different kind of id, you can switch without breaking all your entity validations app-wide.
This is why I'm not a fan of building strict regex validators for ids (of any kind) - they are almost always too strict because coders like to be a precise as they can with validations. The problem is that doing so can backfire in expensive ways.
Ohk if you say so @ericelliott ... thanks!