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

Validate immutable objects #65

Closed
henit opened this issue Oct 5, 2015 · 5 comments
Closed

Validate immutable objects #65

henit opened this issue Oct 5, 2015 · 5 comments

Comments

@henit
Copy link

henit commented Oct 5, 2015

Is there a way to validate that an array is an immutable array? Like something that would be instanceof for immutable arrays but not for normal mutable arrays.

The use case is actually React.Proptypes validation. Something similar to

propTypes: {
    myobject: React.PropTypes.instanceOf(Immutable.List)
}

I know I can use a custom function in ProtoTypes and use Immutable.isImmutable, but adding a function like that in every component that should receive immutable prop objects, will not be pretty.

@rtfeldman
Copy link
Owner

There isn't currently. I haven't tried making a custom PropTypes immutability checker, but if it can be done concisely without bringing in React as a dependency, I'd be open to a PR to add it!

@henit
Copy link
Author

henit commented Oct 13, 2015

I have not looked that close at the code yet, so just asking here.

Is there any base object that the immutable objects inherit from? Or is the original objects sent in to the Immutable() function changed directly? If there is some base object available, it could just be used for instanceof check in PropTypes and any other case where it might be required to check if an object is immutable..

Something like

propTypes: {
    myobject: React.PropTypes.instanceOf(Immutable())
}

given that the function without arguments (or something similar) return a base object

@rtfeldman
Copy link
Owner

That could work with objects, but not with arrays; they need to be instanceof Array in order to be backwards-compatible with normal arrays.

I'm open to adding a helper function intended to be used with React (as long as it doesn't necessitate a React depdendency!) but I don't want to add an extra step to the creation of every single object - even for people using seamless-immutable outside React - for the sake of a React convenience, if that makes sense.

@henit
Copy link
Author

henit commented Oct 14, 2015

Totally understandable. Unfortunately I don't have the time to contribute with the PR myself at the moment, but maybe in the future.

@holyjak
Copy link
Contributor

holyjak commented Jan 6, 2016

I guess we want something that can be used like this:

React.createClass({
  propTypes: {
    array: Immutable.PropTypes.array,
    object: Immutable.PropTypes.object
}});```

Then we can simply implement it this way: ```
function validate(type, props, propName, componentName) {
     componentName = comopnentName || 'ANONYMOUS';
      var msg = componentName + ".props." + propName + " is not ";
      if (type === Array && ! (props[propName] instanceof Array)) return new Error(msg + ' an array');
      if (type === Object && (! (props[propName] instanceof Object) || props[propName] instanceof Array)) return new Error(msg + 'an Object');
      if (!Immutable.isImmutable(props[propName])) {
        return new Error(msg + ' immutable');
      }
      return null;
    }
Immutable.PropTypes = {
  array: validate.bind(undefined, Array),
  object: validate.bind(undefined, Object)
};

(If we want to support also .isRequred then we can follow http://www.ian-thomas.net/custom-proptype-validation-with-react/ . Or/and it is possible to combine with the all(...arrayOfValidators) validator from react-prop-types)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants