-
-
Notifications
You must be signed in to change notification settings - Fork 903
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
Provide regular expressions and JSON schemas for UUIDs #498
Comments
Thank you for coming back here @goloroden! We're definitely interested in covering your use cases in order to reduce the number of To make progress here I would like to understand your use cases for the above two features a bit better. Regarding the regexes: Applying e.g. the import { version } from 'uuid';
let isV4 = false;
try {
isV4 = version('SOME_UUID') === 4;
} catch (e) {} If you want to avoid the error handling you could do: import { validate, version } from 'uuid';
const isV4 = validate(SOME_UUID) && version(SOME_UUID) === 4; That said we have discussed exposing the plain regex(es) in the past so this might definitely be an option, I would just like to understand the benefits over the existing APIs like Regarding the JSON schema to be honest that looks like a pretty niche requirement that might be better suited for a dedicated module, have you considered that option already? Again, I would love to understand the use cases around this one a bit better. |
Hey @ctavan 😊 Regarding regular expressions, I can provide an example of where we use them: In a solution for one of our customers we have an admin CLI which can be used to add, edit, and delete entities from the command-line. To identify these entities, you have to provide their ID (which, obviously, is a UUID). To provide these IDs there are two options: The user can use a flag, or enter them in interactive mode. For the former, the Regarding the JSON schema case: It's basically once again about validating user input, but this time in an HTTP API where we check the entire request body. We use JSON schema for this anyway, so it would be helpful to not have to define the schema over and over again. Of course it's possible to define this elsewhere, but TBH an extra module just for this sounds annoying… and I think having it as part of the Does this help? |
Thanks for the explanation. I understand that providing regex(es) comes in handy for interop with validation libraries. @broofa do you see any drawbacks of exposing the regexes (apart from the fact that we'd have to expose different regexps for the different versions, which we don't have internally yet)? Regarding JSON schema I'm still reluctant. If we add something this framework/technology specific, it would create a precedent for exposing whatever other framework-specific validation formats might come up in the future (e.g. a GraphQL Type and the likes) and I don't think that this is something that this general-purpose library should support. |
This sounds like the pattern attribute. I've mentioned this previously as a possible rationale for exposing the regex we currently use for However, as I think this issue shows, the problem is going to be that each use case will likely need subtly different incantations of the regex. Should the regex be limited to specific versions? Require strict upper/lower casing only? Allow leading/trailing whitespace? Allow omission of hyphens? Accept the nil UUID? Include a capture group? Include the The permutations quickly become unmanageable. And the more I think about it, the more I'm of the mind that exposing the Another option would be to provide a regex generator/helper method, |
Regarding schemas, I agree with @ctavan. Not a business we should be in. Also suffers from the same permutation issues as regexs. Also, JSON-Schema now supports the |
@goloroden does the built-in JSON-Schema support for uuid, that @broofa linked to, solve the JSON schema part of things for you? |
@ctavan Theoretically it would, if the JSON schema validator we're using did support this… 🙄 However, this should not be your problem, this is our problem. However, the regular expression would still be useful. OTOH this means that you can't use the |
Cool!
You are totally right, the |
@goloroden Out of curiousity, which validator are you using? I know |
@broofa We used Hence we switched to jjv, which is way smaller (compare https://bundlephobia.com/result?p=ajv@6.12.3 to https://bundlephobia.com/result?p=jjv@1.0.2), and which produces – when combined with jjve – nice, human-readable error messages. Unfortunately, its last release has been 6 years ago, which is probably the main reason why it doesn't support UUIDs. So, not that you get this wrong: |
Closing, as it doesn't seem like a compelling enough argument has been made to add this. |
Hi 👋
I'm one of the authors of the uuidv4 module.
With the new 8.3 version of
uuid
there is almost everything there that is needed from our POV, and we are planning to deprecateuuidv4
in favor of this module.However, there are two very small things missing, before we can do this:
First, it would be awesome to have regular expressions for UUIDs.
In
uuidv4
we use the following ones:Besides, it would be great to have JSON schemas for UUIDs. Again, we're using this code here, which builds on top of the aforementioned regexs:
Unfortunately, we are missing the time to update the
uuid
module ourselves, however this should be fairly easy to integrate.The text was updated successfully, but these errors were encountered: