feat: Filter invalid values in getIds#979
Conversation
I would 100% prefer this. I know I'm always the one who says "is this really a breaking change" -- and I must say this is an interesting example! After all, this should fix existing uses, rather than break them... right? It's hard to imagine code depending on invalid input to this function throwing 🤷 |
|
I agree with making this a breaking change. Let's remove the options argument. |
|
Is it worth it to extract the validation functionality from |
Per feedback on plexinc#979
avaly
left a comment
There was a problem hiding this comment.
We can ship this as is, but let's mark the commit as a breaking change.
b98bfd6 to
d6bf05f
Compare
|
I removed the I also squashed this all down to a single commit because it was terrible fixing each of the conflicts that arose in the test file; easier to just fix everything all at once. |
avaly
left a comment
There was a problem hiding this comment.
I think this PR should be fine as is. I don't think we need to mark it as breaking change. The only thing to change though is the commit prefix from feat: to fix: since I think this can be viewed as a bug fix.
f4fdb3a to
11ae31b
Compare
🏋️
getIdsrobustnessgetIdsis 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
ObjectIdconstructor 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
getIdsimplementation 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 afilterInvalidoption, 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.getAllaccept anIterable<ObjectIdConstructorParameter>, widening acceptable input typesadd optionalGetIdsOptionsargument togetIds, with optionalfilterInvalidboolean membergetIdsinnermaptoflatMap, to allow single-pass filteringtry/catchtogetIdsinnerflatMap, to rely on ObjectId constructor validation behavioradd error re-throw togetIdsinnerflatMap, gated onfilterInvalidoptionadd test case for invalid input with nofilterInvalidoption