facebook / prop-types

Runtime type checking for React props and similar objects
MIT License
4.48k stars 356 forks source link

Add an API solution to use this package as generic validation as well. #260

Closed rgraffbrd closed 5 years ago

rgraffbrd commented 5 years ago

The problem: When using this package to define an object shape for something like a view containing a form, you essentially need to define your validation twice, once for your props and once for your form validation. After that you need the same definition on your server, somehow, most likely. Using multiple type definition mechanisms and defining what is essentially the same structure multiple times is a shame and seems like a real waste.

The proposed solution: This package is already a great basis for generic validation. You could simply expose a method of some sort that would allow passing an object and running the prop-types validation on it. At that point it could even be used on the server.

As a bonus: Add a means of supplying custom errors/error messages.

I am aware that you are not supposed to call the prop types functions so that "it is safe to strip their implementations to optimize the bundle size". However, it would be a huge benefit to provide a means of keeping specific declarations of them intact.

Overall, you have such a nice package for defining these types of semantics here, I'd just like to see it reach it's full potential. :)

ljharb commented 5 years ago

Closing as a duplicate of #133. Also a duplicate of #34.