feat(common): improve extensibility on few built-in pipes #9496
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.
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
The built-in
ParseEnumPipe
has a protected method (isEnum
) that would help devs on considering if a value is a valid enum, see:nest/packages/common/pipes/parse-enum.pipe.ts
Lines 51 to 63 in 85bda83
but
ParseBoolPipe
,ParseFloatPipe
andParseIntPipe
doesn't has such capabilityWhat is the new behavior?
By implemeting the Template Method pattern, when extending some of those pipes listed above, one could define how they want to evaluate some value as a valid value to proceed the transformation.
For instance, currently,
ParseBoolPipe
will transform"true"
totrue
, but what if we want to treat"1"
astrue
as well? we would have to write our own pipe or write some normalization logic in thetransform
method.Now we could easily extend the built-in pipe like this:
To summary, this PR adds
ParseBoolPipe#isTrue
,ParseBoolPipe#isFalse
,ParseFloat#isNumeric
andParseInt#isNumeric
. All returnsboolean
.Does this PR introduce a breaking change?
I'm not 100% sure on this because devs could have extended some of that pipes and write a method on them that will override the newly added methods, which can lead to unexpected behavior (I guess?)