GithubHelp home page GithubHelp logo

Comments (5)

rtfeldman avatar rtfeldman commented on June 21, 2024

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!

from seamless-immutable.

henit avatar henit commented on June 21, 2024

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

from seamless-immutable.

rtfeldman avatar rtfeldman commented on June 21, 2024

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.

from seamless-immutable.

henit avatar henit commented on June 21, 2024

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

from seamless-immutable.

holyjak avatar holyjak commented on June 21, 2024

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)

from seamless-immutable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.