This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
admin-graphql-api-utilities: Improve GID regex and add parseGidObject
#2647
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
One thing I noticed while looking at the code for GIDs was that the regex is not always reliable. It would let many invalid GIDs through:
gid://shopify/A/B/C/D/E/F/G/123
- does not validate number of componentsgid://-_-_-_-/--------/__________
- does not validate identifiers@#$%^&^*()/Foo/123
- does not validate prefix (forparseGid
/parseGidWithParams
)Note
The validity of these GIDs are based on Shopify's documentation, the GID code (which uses in part the URI spec from RFC 2396), and my own understanding/intuition.
The examples above could very well be considered valid for the sake of this package, however, I would strongly suggest for them not to be. They make for more edge cases, added complexity, a harder time improving types, and an increased risk of errors.
To fix these issues, I updated the regex to completely validate GIDs when parsing. This results in the following rules:
a-z
,A-Z
,0-9
,_
(except namespaces), and-
. See RFC 3986 for details. This is actually a bit stricter than the URI spec since we only allow alphanumeric characters and hyphens (no+
,.
, or other typically allowed character).gid://
) must exist."12345"
as a GID).Changelog
Added
parseGidObject
to parse a GID string into an object with the relevant componentsChanged
parseGid
andparseGidWithParams
no longer accept partial GIDsOpen Questions
parseGidType
andparseGid
, replacing them withparseGidObject
(possibly renaming it toparseGid
)?parseGidObject
? I chose not to so we save on wasted operations for those who just want the ID or type. However, we definitely could do that.