-
Notifications
You must be signed in to change notification settings - Fork 357
Using prop-types outside of React #34
Comments
I'm using it to do validating. Currently
Maybe |
@reallyimeric This works, but only if you don't set |
Adds a `checkPropTypeWithErrors` convenience function, which calls `checkPropTypes` with `throwErrors` set to true Fixes facebook#34
Adds a `checkPropTypeWithErrors` convenience function, which calls `checkPropTypes` with `throwErrors` set to true Fixes facebook#34
When specified, the argument `warningLogger` will be called with the same arguments as `warning`. Instead of logging errors to the fbjs warning logger, we are able to handle them externally. Fixes facebook#34
When specified, the argument `warningLogger` will be called with the same arguments as `warning`. Instead of logging errors to the fbjs warning logger, we are able to handle them externally. Fixes facebook#34
When specified, the argument `warningLogger` will be called with the same arguments as `warning`. Instead of logging errors to the fbjs warning logger, we are able to handle them externally. Fixes facebook#34
any updates ? |
For anyone looking for a validating library, ajv would be a good choice. |
I want to use like this: class Test {
constructor(props) {
this.scale = props.scale;
}
}
Test.propTypes = {
scale: PropTypes.number,
};
Test.defaultProps = {
scale: 1,
};
const test = new Test({});
// expect: 1
console.log(test.scale); 🤧🤧🤧 |
@dragonwong for that to work, the JS language itself would have to support prop types and default props. You can, however, do this: class Test {
constructor({ scale = 1, ...rest }) {
this.props = { scale, ...rest };
}
} |
@ljharb thanks, but I think it can not support complex feature such as And |
aggree with @dragonwong , I was trying to use prop-types in nodeJS for attributes validation |
I'm not sure what's still actionable here - you can certainly use this library outside React ( |
@ljharb The use case example given in the original issue can't be done with the library, as-is. You can, indeed, call
Emphasis mine. So in my example where I'm trying to prevent someone from passing bad data via a REST API, I can verify that with prop-types on the server, but it will automatically be turned off for me in production. This is not desired behavior for a schema checking library. |
@jwalton ah - you can choose to use the development version in production, and it'll work fine https://unpkg.com/[email protected]/prop-types.js |
But how would I do that? const originNodeEnv = process.env.NODE_ENV;
process.env.NODE_ENV = 'development';
const propTypes = require('prop-types');
process.env.NODE_ENV = originNodeEnv; Yuck. |
The file I linked does not check |
iow, instead of requiring |
If you open up p = require('prop-types/prop-types');
console.log(p.checkPropTypes.toString()); You'll see the code is: function checkPropTypes(typeSpecs, values, location, componentName, getStack) {
if ("development" !== 'production') {
// Do a bunch of stuff
}
} Open up that link you posted above and find |
Yep, and that works fine, because the string "development" will never be equal to the string "production". |
I am not a smart man. :P Thanks, sir. |
I'd like to see this in the docs, that would be a big help :) |
a PR is welcome. |
|
@alexeychikk then that’s a bug in the DT package - all files should be typed, not just the main. (separately, |
@ljharb TS module system is not the issue. With esModuleInterop it works both ways: import * as PropTypes from "prop-types";
import PropTypes from "prop-types"; The problem is that type definitions in |
Sure, that's why i said "separately" :-) please file a bug on DT so that the types can be defined for every file. |
When specified, the argument `warningLogger` will be called with the same arguments as `warning`. Instead of logging errors to the fbjs warning logger, we are able to handle them externally. Fixes facebook#34
When specified, the argument `warningLogger` will be called with the same arguments as `warning`. Instead of logging errors to the fbjs warning logger, we are able to handle them externally. Fixes facebook#34
When specified, the argument `warningLogger` will be called with the same arguments as `warning`. Instead of logging errors to the fbjs warning logger, we are able to handle them externally. Fixes facebook#34
When specified, the argument `warningLogger` will be called with the same arguments as `warning`. Instead of logging errors to the fbjs warning logger, we are able to handle them externally. Fixes facebook#34
Now that
prop-types
is a top-level package, it occurs to me that there are several use cases where this could be a useful library outside of React.Use Case
Suppose, for example, you are writing an application with a REST interface that takes in some complicated JSON data, and queries MongoDB:
Simple enough... But, oh noes! There are dark forces at work on the internet, and someone sends us this JSON body:
MongoDB faithfully tries to load all 10 million of our users into memory, the server crashes, our company goes bankrupt, and our children starve.
But wait!
prop-types
to the rescue!Why this is awesome
There's a lot to recommend this. First, it's easy to check types without having to pull in some extra build step like TypeScript or Flow (especially nice on existing projects which don't already incorporate those technologies). Second, it's a syntax that many people are already very familiar with, because it's so widely used in React.
The challenge is, of course, that today prop-types is so tightly coupled to React. For starters, all of prop-types is disabled if
NODE_ENV === 'production'
, which is an obvious problem, so there'd need to be some changes. Turning prop-types into a more general-purpose library should have advantages for React though, as it should make it easier to do things like #28.API Proposal
This is just to give a vague idea of what I'm after here, there's lots of room for improvement on this, but:
PropTypes.isInvalid(typeSpec, value, options)
typeSpec
is an object where keys are property names, and values are the associated PropType objects (e.g.{username: PropTypes.string}
).value
is the object we're going to check.options.context
- Seeoptions.onValidationError()
.options.onValidationError({path, expectedType, actualType, errorString, context})
- If this function returns a truthy value, thenisInvalid()
will return that value immediately. If this function throws, the the error will propagate all the way to the caller ofisInvalid()
.path
is the path of the value being checked (e.g. "user.name").expectedType
is the type that was expected at this path (e.g. 'string'), actualType is the type we actually found.context
is theoptions.context
object passed toisInvalid()
.errorString
is an english-language ready made error string to describe what went wrong. The default implementation simply returnserrorString
.I think this gives you flexibility to do quite a bit. The existing React use case, where we need to know if
value
came from props or context or whatever is solved by passing{location, componentName}
along in thecontext
, soonValidationError()
can construct an appropriate string. React would setonValidationError: () => null
when running production code.One minor downside to this is it makes a webpacked production React app slightly bigger, since all this code that used to get optimized out is no longer going to be enclosed in
NODE_ENV === 'production'
checks. I can think of a couple of ways around that, but I'll leave that for future discussion.The text was updated successfully, but these errors were encountered: