feat: Filter invalid values in getIds #979
Open
+105
−18
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.
🏋️
getIds
robustnessgetIds
is a useful utility function, but it accepts a limited subset of the valid ObjectId constructor argument types, and throws when presented with invalid input.It's not straight-forward to filter inputs to be valid constructor arguments; in some codebases we maintain bespoke utilities for performing these checks (eg;
isValidObjectId
).Unfortunately, the canonical validation for whether one of these arguments is a valid argument to the
ObjectId
constructor is cooked-in to the constructor itself.Therefore, rather than re-implementing this logic (or trying to get it extracted and exposed upstream), I updated the
getIds
implementation to rely on the constructor logic throughtry/catch
, and to discard invalid elements.🐤 Backwards-Compatibility
Per discussion, this is going to be a breaking change.
In order to keep the change backwards-compatible, I gated the filtering functionality behind afilterInvalid
option, passed togetIds
. If we were okay with making a breaking release, I think this would be much nicer as the default behavior.🔧 Changes
I updated
getAll
(🟦) and its specs (🟡) in a few ways.getAll
accept anIterable<ObjectIdConstructorParameter>
, widening acceptable input typesadd optionalGetIdsOptions
argument togetIds
, with optionalfilterInvalid
boolean membergetIds
innermap
toflatMap
, to allow single-pass filteringtry/catch
togetIds
innerflatMap
, to rely on ObjectId constructor validation behavioradd error re-throw togetIds
innerflatMap
, gated onfilterInvalid
optionadd test case for invalid input with nofilterInvalid
option