Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generalized behavior #277

Closed
wants to merge 3 commits into from

Conversation

Projects
None yet
3 participants
@nemzes
Copy link

commented Jun 11, 2019

prop-types is a great way to define object schemas, which would make it a suitable tool for writing tests, validating API data, etc. Since this could be shared with React component prop validation, it would be possible to reuse schemas in these disparate scenarios. In its current implementation, however, it's not easy to address those use-cases since the library is geared to provide in-browser feedback during development.

This PR introduces a PropTypes.generalize() function that changes the behaviour of the library to throw Errors instead of logging to the console. It also removes the "avoid duplicate messages" behaviour that exists for the benefit of human consumption. It should then be possible to validate arbitrary objects in automated workflows, e.g.:

import PropTypes from `prop-types`;

PropTypes.generalize();
const propTypes = ...
const dataObject = ...
try {
  PropTypes.checkPropTypes(propTypes, dataObject);
}
catch (err) {
  // Handle invalid data object
}
var printWarning = function() {};

if (process.env.NODE_ENV !== 'production') {
if (usage.shouldDoSomething()) {

This comment has been minimized.

Copy link
@ljharb

ljharb Jun 11, 2019

Collaborator

this change will prevent bundling optimizations they delete this chunk of code in production.

@ljharb

This comment has been minimized.

Copy link
Collaborator

commented Jun 11, 2019

You may be interested in the checkPropTypes function, which already allows you to use this library outside of react, with no changes needed.

@ljharb ljharb changed the title Add generalised behaviour Add generalized behavior Jun 11, 2019

@nemzes

This comment has been minimized.

Copy link
Author

commented Jun 11, 2019

Ah… and now I feel dumb. It might be a good idea to add a link to that package in the README here 😁

@nemzes nemzes closed this Jun 11, 2019

@ljharb

This comment has been minimized.

Copy link
Collaborator

commented Jun 11, 2019

It is - see both "If you absolutely need to trigger the validation manually," and "If you DO want to use validation in production" in the readme.

@nemzes

This comment has been minimized.

Copy link
Author

commented Jun 11, 2019

Uhmn, no…? The behaviour of PropTypes.checkPropTypes is still to log issues to the console. It's very awkward to use this is automated testing.

When you mentioned "You may be interested in the checkPropTypes function" I actually thought you were referring to this: https://github.com/ratehub/check-prop-types (which is what I think should be in the README). That package allows using its own checkPropTypes(), which does not rely on console for output.

I still need to see how to use this in production though.

@ljharb

This comment has been minimized.

Copy link
Collaborator

commented Jun 11, 2019

See #28 for that feature request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.