skorokithakis / shortuuid

A generator library for concise, unambiguous and URL-safe UUIDs.
http://www.stavros.io/
BSD 3-Clause "New" or "Revised" License
2.05k stars 109 forks source link

regex match for shortuuids with the default alphabet #97

Closed gitpushdashf closed 1 year ago

gitpushdashf commented 1 year ago

Hi!

Not really an issue, more something to leave here in case it's helpful for someone else. This is for if you have IDs that you want to validate with say FastAPI/Pydantic, and you want a regex to make sure it's valid before processing it.

This is what I have so far: [2-9A-HJ-NP-Za-km-z]{22}

It may have issues, but seems to work for me. In FastAPI you could use a Path/Query/Field and declare regex="[2-9A-HJ-NP-Za-km-z]{22}" and it should catch it, but you may want to set min_length=22, max_length=22 as well.

I'm sure this can be improved. Feel free to close out or incorporate into documentation if you think it's useful.

Thank you!

skorokithakis commented 1 year ago

Added, thank you!