Last active
April 20, 2017 11:35
-
-
Save marc-rutkowski/12c16f77f8b135cc42092be6bbdeb970 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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