react-immutable-proptypes
Advanced tools
Comparing version 1.6.0 to 1.7.0
@@ -1,2 +0,5 @@ | ||
## 1.6.0 added mapContains type checker | ||
## 1.7.0 added mapContains type checker | ||
- thanks to [Brian Emil Hartz](https://github.com/hartzis) for adding this long requested feature. | ||
## 1.6.0 added stackOf type checker | ||
- thanks to [Alon Gubkin](https://github.com/alongubkin) for writing the `stackOf` type checker. | ||
@@ -3,0 +6,0 @@ |
@@ -22,4 +22,5 @@ /** | ||
recordOf: createRecordOfTypeChecker, | ||
shape: createShapeTypeChecker, | ||
contains: createShapeTypeChecker, | ||
shape: createShapeChecker, | ||
contains: createShapeChecker, | ||
mapContains: createMapContainsChecker, | ||
// Primitive Types | ||
@@ -165,9 +166,13 @@ list: createImmutableTypeChecker("List", Immutable.List.isList), | ||
function createShapeTypeChecker(shapeTypes) { | ||
var immutableClassName = arguments[1] === undefined ? "Iterable" : arguments[1]; | ||
var immutableClassTypeValidator = arguments[2] === undefined ? Immutable.Iterable.isIterable : arguments[2]; | ||
function validate(props, propName, componentName, location) { | ||
var propValue = props[propName]; | ||
var propType = getPropType(propValue); | ||
if (!Immutable.Iterable.isIterable(propValue)) { | ||
if (!immutableClassTypeValidator(propValue)) { | ||
var locationName = location; | ||
return new Error("Invalid " + locationName + " `" + propName + "` of type `" + propType + "` " + ("supplied to `" + componentName + "`, expected an Immutable.js Iterable.")); | ||
return new Error("Invalid " + locationName + " `" + propName + "` of type `" + propType + "` " + ("supplied to `" + componentName + "`, expected an Immutable.js " + immutableClassName + ".")); | ||
} | ||
var mutablePropValue = propValue.toObject(); | ||
for (var key in shapeTypes) { | ||
@@ -178,3 +183,2 @@ var checker = shapeTypes[key]; | ||
} | ||
var mutablePropValue = propValue.toObject(); | ||
var error = checker(mutablePropValue, key, componentName, location); | ||
@@ -189,2 +193,10 @@ if (error) { | ||
function createShapeChecker(shapeTypes) { | ||
return createShapeTypeChecker(shapeTypes); | ||
} | ||
function createMapContainsChecker(shapeTypes) { | ||
return createShapeTypeChecker(shapeTypes, "Map", Immutable.Map.isMap); | ||
} | ||
module.exports = ImmutablePropTypes; |
{ | ||
"name": "react-immutable-proptypes", | ||
"version": "1.6.0", | ||
"version": "1.7.0", | ||
"description": "PropType validators that work with Immutable.js.", | ||
@@ -5,0 +5,0 @@ "main": "dist/ImmutablePropTypes.js", |
@@ -63,2 +63,4 @@ # react-immutable-proptypes | ||
ImmutablePropTypes.record // instanceof Record | ||
ImmutablePropTypes.contains // Immutable.Iterable.isIterable - contains(shape) | ||
ImmutablePropTypes.mapContains // Immutable.Map.isMap - contains(shape) | ||
``` | ||
@@ -68,3 +70,3 @@ | ||
* `ImmutablePropTypes.mapOf` is basically the same as `listOf`, but it is specific to `Immutable.Map`. | ||
* `ImmutablePropTypes.mapOf` is basically the same as `listOf`, but it is specific to `Immutable.Map` It will check that the prop is an Immutable.Map and that the values are of the specified type. | ||
@@ -90,22 +92,32 @@ * `ImmutablePropTypes.orderedMapOf` is basically the same as `listOf`, but it is specific to `Immutable.OrderedMap`. | ||
* `ImmutablePropTypes.contains` (formerly `shape`) is based on `React.PropTypes.shape` and will try to work with any `Immutable.Iterable`. In practice, I would recommend limiting this to `Immutable.Map` or `Immutable.OrderedMap`. However, it is possible to abuse `contains` to validate an array via `Immutable.List`. | ||
* `ImmutablePropTypes.contains` (formerly `shape`) is based on `React.PropTypes.shape` and will try to work with any `Immutable.Iterable`. In practice, I would recommend limiting this to `Immutable.Map` or `Immutable.OrderedMap`. However, it is possible to abuse `contains` to validate an array via `Immutable.List`. That said, please, just... don't. | ||
```es6 | ||
// ... | ||
aList: ImmutablePropTypes.contains({ | ||
0: React.PropTypes.number.isRequired, | ||
1: React.PropTypes.string.isRequired, | ||
2: React.PropTypes.string | ||
aMap: ImmutablePropTypes.contains({ | ||
aList: ImmutablePropTypes.contains({ | ||
0: React.PropTypes.number, | ||
1: React.PropTypes.string, | ||
2: React.PropTypes.number.isRequired, | ||
}).isRequired, | ||
}) | ||
// ... | ||
<SomeComponent aList={Immutable.List([1, '2'])} /> | ||
<SomeComponent aList={Immutable.fromJS({aList: [1, 'two', 3]})} /> | ||
``` | ||
That said, don't do this. Please, just... don't. | ||
* `ImmutablePropTypes.mapContains` is based on `React.PropTypes.shape` and will only work with `Immutable.Map`. | ||
```es6 | ||
// ... | ||
aMap: ImmutablePropTypes.mapContains({ | ||
aList: ImmutablePropTypes.list.isRequired, | ||
}) | ||
// ... | ||
<SomeComponent aList={Immutable.fromJS({aList: [1, 2]})} /> | ||
``` | ||
These two validators cover the output of `Immutable.fromJS` on standard JSON data sources. | ||
## RFC | ||
Please send a message or, better yet, create an issue/pull request if you know a better solution, find bugs, or want a feature. For example, should `listOf` work with `Immutable.Seq` or `Immutable.Range`. I can think of reasons it should, but it is not a use case I have at the present, so I'm less than inclined to implement it. Alternatively, we could add a validator for sequences and/or ranges. |
@@ -20,4 +20,5 @@ /** | ||
recordOf: createRecordOfTypeChecker, | ||
shape: createShapeTypeChecker, | ||
contains: createShapeTypeChecker, | ||
shape: createShapeChecker, | ||
contains: createShapeChecker, | ||
mapContains: createMapContainsChecker, | ||
// Primitive Types | ||
@@ -175,13 +176,14 @@ list: createImmutableTypeChecker('List', Immutable.List.isList), | ||
// there is some irony in the fact that shapeTypes is a standard hash and not an immutable collection | ||
function createShapeTypeChecker(shapeTypes) { | ||
function createShapeTypeChecker(shapeTypes, immutableClassName = 'Iterable', immutableClassTypeValidator = Immutable.Iterable.isIterable) { | ||
function validate(props, propName, componentName, location) { | ||
var propValue = props[propName]; | ||
var propType = getPropType(propValue); | ||
if (!Immutable.Iterable.isIterable(propValue)) { | ||
if (!immutableClassTypeValidator(propValue)) { | ||
var locationName = location; | ||
return new Error( | ||
`Invalid ${locationName} \`${propName}\` of type \`${propType}\` ` + | ||
`supplied to \`${componentName}\`, expected an Immutable.js Iterable.` | ||
`supplied to \`${componentName}\`, expected an Immutable.js ${immutableClassName}.` | ||
); | ||
} | ||
var mutablePropValue = propValue.toObject(); | ||
for (var key in shapeTypes) { | ||
@@ -192,3 +194,2 @@ var checker = shapeTypes[key]; | ||
} | ||
var mutablePropValue = propValue.toObject(); | ||
var error = checker(mutablePropValue, key, componentName, location); | ||
@@ -203,3 +204,10 @@ if (error) { | ||
function createShapeChecker(shapeTypes) { | ||
return createShapeTypeChecker(shapeTypes); | ||
} | ||
function createMapContainsChecker(shapeTypes) { | ||
return createShapeTypeChecker(shapeTypes, 'Map', Immutable.Map.isMap); | ||
} | ||
module.exports = ImmutablePropTypes; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24613
358
121