oculus42 / short-uuid

Translate standard UUIDs into shorter formats and back.
MIT License
450 stars 13 forks source link

Regexp for short uuid #63

Closed rzkhosroshahi closed 2 years ago

rzkhosroshahi commented 2 years ago

Hey 👋

Thanks for your amazing tool. I was looking is there regexp pattern in your source code and I couldn't find any. Is that possible to give me hint or regexp pattern? I really need it

wparad commented 2 years ago

The regex would 100% be dependent on your alphabet. For instance the default alphabets are here: https://github.com/oculus42/short-uuid/blob/develop/index.js#L9:

const flickrBase58 = '123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ';
const cookieBase90 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!#$%&'()*+-./:<=>?@[]^_`{|}~";
rzkhosroshahi commented 2 years ago

Actually, I think this will be the right question: how can I valid a short id and some random string?

ex: 'footballPlayer' vs 'wBxoRRN265LUF1TW5KpyGp'

How can I find out which string is a valid short id?

wparad commented 2 years ago

But this still begs the question, why would you ever need to do this:

const short = require('short-uuid');
const anyBase = require('any-base');
// using flickr 58 or using your own
// * consistentLength MUST be set to `true` or else we can't make the correct assumptions below
const translator = short(short.constants.flickrBase58, { consistentLength: true });

const stringToValidate = 'STRING';
const notValid = stringToValidate.length > translator.maxLength
  || anyBase(translator.alphabet, anyBase.HEX)(stringToValidate).length !== 32;
rzkhosroshahi commented 2 years ago

Because I translated UUID to short id and put it in URL as a param. I need to be sure this param will be always valid as a short id and not just a random route path string. I know this is the corner case but If I not able to validation this short id I should use regular uuid.

wparad commented 2 years ago

If you are translating from a valid UUID then it will always be a valid short id. And if the value is making a round trip, surely it is irrelevant if it is valid, you care if it is relevant, right? e.g. that the value exists in a DB for instance. Which makes a validation unnecessary. And if that isn't the case, maybe you could share more about what you are doing and why

oculus42 commented 2 years ago

While it may not work for alphabets with special characters, you can use a relatively simple operation to generate a RegExp.

const short = require('short-uuid');
const translator = short(); // Defaults to flickrBase58
const shortRegex = new RegExp(`^[${translator.alphabet}]{${translator.maxLength}}$`);

shortRegex.test(translator.new()); // true

It is easy enough to get a false positive with an alphabet like the flickrBase58, though.

const falsePositive = 'WordWithMatchingLength';
shortRegex.test(falsePositive); // true

If you are pulling the ID out of a URL, it would be best to ensure you don't have any possible route overlap where a verb or path could be in the same position as the ID.

oculus42 commented 2 years ago

@rzkhosroshahi Closing this as an issue. Please comment if you have additional questions.