Skip to content

Instantly share code, notes, and snippets.

@marc-rutkowski
Last active April 20, 2017 11:35
Show Gist options
  • Save marc-rutkowski/12c16f77f8b135cc42092be6bbdeb970 to your computer and use it in GitHub Desktop.
Save marc-rutkowski/12c16f77f8b135cc42092be6bbdeb970 to your computer and use it in GitHub Desktop.
/**
* Custom PropTypes validator for Immutable JS data
*
* This helper performs the following operations
* 1. Check that prop is immutable
* 2. Convert value toJS() then check it against the prop type specs.
*/
import PropTypes from 'prop-types';
import { isImmutable } from 'immutable';
const ImmutableType = (typeSpecs) => (props, propName, componentName) => { // eslint-disable-line consistent-return
const propValue = props[propName];
if (!isImmutable(propValue)) {
return new Error(`Prop ${propName} supplied to ${componentName} is not immutable.`);
}
const value = propValue.toJS();
PropTypes.checkPropTypes({ [propName]: typeSpecs }, { [propName]: value }, propName, componentName);
};
export default ImmutableType;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment