-
Notifications
You must be signed in to change notification settings - Fork 59
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
PropTypes and Prop Validation with JSX #111
Comments
It is already implemented as attribute validators, check them out. |
Taking a look at the docs here, I think we would do something like this: class MyLabel extends Component {
render() {
return <span class="label">{this.config.label}</span>
}
}
MyLabel.STATE = {
label: {
validator: core.isString
}
} It also seems like In YUI, there was a |
Another subtle difference with Edit: To expand on this, |
the nice thing of what Metal inherited from YUI is the no-privates approach. You have a state, someone owns you, they owns your state too. It can lead to more fragile practices but it's very flexible in the medium run. In YUI you had |
Edit: Ignore this comment, it seems to actually be working as expected. May have had a typo or something. |
Thanks a lot for explaining this so well @yuchi! :D Just confirming, as @yuchi said, we indeed do not have a separation between internal only data like React does. We preferred leaving that to the developer's own organization. You can have internal state if you wish, and signify that with a leading or trailing As for the validators, @mthadley, the default behavior for them is to just ignore invalid values. But our |
@mairatma Thanks! I like that idea, that would be a very nice utility to have. I think anyone else coming from React would also be happy to have that available to them. We could also probably find a way to strip out those checks in production code, whether its through the build system or some other method. Also, thanks @yuchi for your replies. It was very helpful! |
Have you taken a look at how react does @mthadley ? #ifndef NDEBUG
#define DASSERT(condition, message) ASSERT(condition, message)
#define DLOG(message) LOG(message)
#define DLOGERR(message) LOGERR(message)
#else
#define DASSERT(condition, message)
#define DLOG(message)
#define DLOGERR(message)
#endif // ! NDEBUG i.e, macro all the way hahah also, maybe we shouldn't get too biased about how react does things (not saying that they're doing it wrong) |
@cirocosta I know there is a webpack loader that does something like that: https://github.com/friskfly/if-loader Good point. We should remember that Metal is not React. We can still make use of good practices and ideas we learned elsewhere, but we should also consider what is the Metal way of doing it. |
@cirocosta, @mthadley +1! I'm glad you guys understand that :) That said, I know that there certainly are good patterns and ideas in React that we can learn from and use to improve our library, and those are worth thinking about. And in some cases, like in this validator one, some developers would prefer if the behavior was different. So having this separate utilities library that you can use for your validators is ideal here, since we don't need to change the default behavior but can also provide a different one. Unfortunately I'm very busy these days, so I don't think I'll be able to get to this very soon. But let me know if any of you could help out with this idea. We could create a new repo for this and you could send me prs. Might be a good way to get more used to Metal.js (at least the core library, which has some type checks that could be used here, as well as the pattern we're using for our repos) :) |
@mairatma, I can start working on it today. If you want to create a repo, just send me the link and I will send you any prs |
Thanks a lot, @bryceosterhaus! Any idea what name we should use for this? I don't think there should be the word prop in it, since we don't have props. I've created the repo as metal-state-validators, but if you guys have a better name we can change it. |
I imagine that is probably fine for most people. If we want some other suggestions, here are a few that come to mind: |
I think I like |
@eduardolundgren pointed something out to me now which makes sense. These utilities will be validator functions in Metal.js, so the name |
The name validator gives us more space for having other kinds of validators not only type checkers. And seems like |
Makes sense to me. One thing I did notice with the validator, is that it only passes one argument, value. Would it make sense to also pass in the context of that component? Something more along the lines of file: metal-state/blob/master/src/State.js
callValidator_(name, value) {
var info = this.stateInfo_[name];
var config = info.config;
var renderer = this.getRenderer();
var component = this.constructor.name;
if (config.validator) {
return this.callFunction_(config.validator, [value, name, component, renderer]);
}
return true;
} This helps for our specific use case so that we can alert the developer as to which state attribute and which component this is happening in. Also passing in the renderer can give us context of the parent component if it is nested. Not sure if there is an easier way to do that since we may not want all of those args for non type-checking validators. |
When we call validators we always do it in the component's context, as you can see here: https://github.com/metal/metal-state/blob/master/src/State.js#L192. Unless the validator function is previously bound by another context you should be able to get the component instance via |
@mairatma, that works for component and the renderer. However, the key of that value is prone for error. We can try to derive the key from Would it be okay to call the validator with value and name? return this.callFunction_(config.validator, [value, name]); This way we at least insure that the value the validator operating on will always match its key. |
Sure, makes total sense. Could you send a pr with this change to metal-state? Besides this change it'd just require adding a test case for validators to check that the name is being passed as well :) |
This change is also inline with YUI Attribute which I know inspired parts of |
Yeah I will go ahead and do that now. Thanks @mairatma! |
Now that metal-state-validators already exists we can track further requests related to it there, so closing this issue :) |
One we have found very helpful while working with react is the way that they have implemented PropTypes: https://facebook.github.io/react/docs/reusable-components.html We have found that it helps to self document code and point out issues sooner by logging errors when propTypes do not match up correctly.
Would it be possible to implement this type of feature in Metal JSX?
The text was updated successfully, but these errors were encountered: