-
Notifications
You must be signed in to change notification settings - Fork 356
Add isDefined and isNotNull parallels to isRequired #90
base: main
Are you sure you want to change the base?
Conversation
Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed. If you have received this in error or have any questions, please contact us at [email protected]. Thanks! |
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks! |
return null; | ||
if (props[propName] === null) { | ||
return allowNull ? null : new PropTypeError('The ' + location + ' `' + propFullName + '` is marked as required ' + ('in `' + componentName + '`, but its value is `null`.')); | ||
} else if (props[propName] == null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
== null
is the same as === undefined
in this case.
I used == null
because that's what the old code used, and it avoids referencing the undefined
value directly.
I think it's a holdover best practice from older browsers that allow undefined to be assigned a different value:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no worries, I make the same mistake all the time. I even use a font that helps me see the difference: https://github.com/tonsky/FiraCode
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
== null
also matches both null and undefined, which is the convention in propTypes, so it's not the same as === undefined
, by design.
@aweary any chance of getting some feedback on this PR? I'd love to see this merged & released if possible, since there isn't a good workaround for supporting |
@jharris4 |
@ljharb thanks for the suggestion, I just had a quick look at the source and tests for I'm curious, how would you suggest using |
Using |
@jharris4 I'll try and review soon, thanks for doing this btw! |
Any updates on this? I really don't want to use a whole new package for nullable required props. |
I'm not really sure this is a good approach; I'd prefer adding something like |
@ljharb Things like This PR encompasses the minimal change possible to give people the ability to distinguish between This PR is also a non breaking change and keeps the PropTypes package completely backwards compatible with previous versions. With the current state of the code most of the change that I made in this PR would be required anyway, since there is currently no way to allow
|
I realize it's out of scope of this PR; I'm suggesting that this PR's premise is flawed and it should be closed. |
I'd argue that the premise of the original code to conflate I get the sense that there are other users out there who could also benefit from this change to allow them to continue using It'd be unfortunate though, because without this PR I wonder whether the core React team have mostly switched to using |
Also with regards to
because of the strict handling done for isRequired in this code: prop-types/factoryWithTypeCheckers.js Line 293 in 9057907
However it would work just fine as:
|
@ljharb I'm kind of in the same boat as @jharris4. I agree that your solution makes more sense long-term, but it also requires a major breaking change to how |
I'm not sure why it requires a major change at all - I'm not proposing changing how I'm suggesting solely two semver-minor changes: adding If you insist on wanting to exclude From those primitives, everything this PR wants can be achieved, with composition. |
The trouble is that the current implementation of I have dozens of props in some of my apps that I'd like to validate to ensure that their values are always a The composition idea is a good one, so I'll run with it for a minute.
Assuming that PropTypes supported negation (which it doesn't), I can't quite get what I want using
Similarly with
This seems like the closest composition solution to what I actually want. The trouble is, it allows |
|
That could work, so long as I would perhaps generalize it a bit to |
I just became aware of a detailed issue filed a while back on React that discusses a lot of these points! facebook/react#2166 It's pretty funny to read the comment by @sebmarkbage:
|
Even adding a new top-level propType is a bit of a big deal, since this package gets so much usage - adding a new method to every top-level propType is much much riskier. I've only been maintaining this package for a few months, and the limited time I've had to invest in it has been spent cleaning up bugfixes and cutting long-awaited releases - which naturally pushes risky additions to the bottom of the list, even if I'd personally enjoy using them. I hope you, and everyone else, can be patient. |
Hey @ljharb, first of all thanks for donating your time to maintaining open source packages like this one. It can be a thankless job at times... If you feel that there might be a way to move this PR forward, could you elaborate on the risks you're referring to? The existing tests for this library all pass with this PR, and I took the trouble to add new tests to mitigate any risks. If you feel that more tests would be required to increase your confidence level, I could probably find some time to invest in that. Also, in case it helps inspire confidence we've got 3 different products using this PR (via forked package) with a combined total of several hundred components, deployed on-premise to about 300 clients, and haven't had a single issue (we use sentry for error tracking). |
How is this still open?! LGTM 🚀 |
This is a great PR! A consideration - does a failing PropType for either |
This change is still necessary to cover the simple use case of The current workaround looks like this (assuming
|
@icopp The workaround should't work because The PR doesn't change prop-types behavior (for backwards compatibility), but it adds At this point is there a way to completely remove prop-types from React and use our own callbacks? If this package is no longer being maintained and the notion is to "use TypeScript" (which we do), should prop-types import just be removed from core and replaced with something a little more simplistic? Something that can be better extended? On our team we really don't use prop-types at all anymore and use TypeScript, but I personally see the benefit of having runtime checking of props. The dream for me would be able to just define TypeScript types and have runtime checking in development to guard against badly typed endpoints. |
@lukescott this package is absolutely still being maintained - a new feature not being added, or taking a long time to be added, quite simply never means a package is unmaintained. PropTypes and TypeScript can not replace each other; PropTypes is incapable of checking function argument types or return values; TypeScript is incapable of determining basic things like "is it an integer". They should be used together, in concert. |
Why can't this just be added as an "Experimental" feature? |
That's not a concept that exists in semver. If it's published, it's supported until at least the next major, and that's effectively the same as forever. |
I am more referring to some sort repos feature system in accepting features that community want but the owner of the repo do not want (for whatever reason) and see how this feature is been used and etc. Experimental features even in React library itself... Why can't it be also in prop-type repo? Just thought it can give some oxygen in this tight grip...... |
just chiming in to say i also ran into issues with this and |
I ended up here after realizing that I needed to support a prop that could be both null but also a value, but not undefined. I just realized that the PR has been open for over 3 years! I guess I'll keep my fingers crossed. :) |
@jdhorner in the meantime, https://npmjs.com/airbnb-prop-types has |
@jdhorner The The fork of this package with this PR merged is available here if you'd like to use it: https://www.npmjs.com/package/prop-types-defined |
@jharris4 you'd use |
@ljharb ah yes, combing So for anyone looking for a way to do what this PR is about you have two options:
or
@ljharb any reason not to close this PR now? Seems like it's never going to be merged, and it might be better to just point people to another solution... |
Is there any movement on this PR? It would be nice to be able to do the following: PropTypes.oneOfType([PropTypes.string, PropTypes.oneOf([null])]).isRequired As others have probably already mentioned, |
@kesupile-kesupile it's looking increasingly likely that this will never be merged. A while ago I made a tiny plugin to allows nulls (it's a plugin rather than a fork like some of the other solutions so you don't need to worry about it going stale): https://www.npmjs.com/package/prop-types-nullable — it's tested, supports TypeScript, and the chosen syntax is compatible with common linter rules. npm install --save prop-types-nullable Usage: const PropTypes = require('prop-types');
const nullable = require('prop-types-nullable');
MyReactClass.propTypes = {
foobar: nullable(PropTypes.func).isRequired,
}; (or just copy the 10-or-so lines of code for |
@davidje13 https://npmjs.com/airbnb-prop-types has had an As to whether this will be merged or not, it's a relatively impactful change, so it's unlikely to be merged soon, at least. |
Also React no longer has prop-types as a dependency. Even though though you could use other libs before, it still included it, but not anymore. Better to use an alt lib like the Airbnb one or a fork of this one. |
Any updates on when/whether this will be reviewed? Its a small PR that offers a large amount of value. It would tremendously improve the project. All the other alternatives suggested are all very verbose. Seems odd to not move forward with this when it provides a simple solution with very little changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still not sure whether this is viable to land, but to be landable, it'd have to add similar tests to all four test files.
Adds the ability to do the following:
closes #57 and duplicates some work from #59