Socket
Socket
Sign inDemoInstall

react-immutable-proptypes

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-immutable-proptypes - npm Package Compare versions

Comparing version 0.1.5 to 0.1.6

166

dist/__tests__/ImmutablePropTypes-test.js

@@ -86,2 +86,118 @@ "use strict";

describe("MapOf Type", function () {
it("should support the mapOf propTypes", function () {
typeCheckPass(PropTypes.mapOf(React.PropTypes.number), new Immutable.Map({ 1: 1, 2: 2, 3: 3 }));
typeCheckPass(PropTypes.mapOf(React.PropTypes.string), new Immutable.Map({ 1: "a", 2: "b", 3: "c" }));
typeCheckPass(PropTypes.mapOf(React.PropTypes.oneOf(["a", "b"])), new Immutable.Map({ 1: "a", 2: "b" }));
});
it("should support mapOf with complex types", function () {
typeCheckPass(PropTypes.mapOf(React.PropTypes.shape({ a: React.PropTypes.number.isRequired })), new Immutable.Map({ 1: { a: 1 }, 2: { a: 2 } }));
typeCheckPass(PropTypes.mapOf(PropTypes.shape({ a: React.PropTypes.number.isRequired })), Immutable.fromJS({ 1: { a: 1 }, 2: { a: 2 } }));
function Thing() {}
typeCheckPass(PropTypes.mapOf(React.PropTypes.instanceOf(Thing)), new Immutable.Map({ 1: new Thing(), 2: new Thing() }));
});
it("should warn with invalid items in the map list", function () {
typeCheckFail(PropTypes.mapOf(React.PropTypes.number), new Immutable.Map({ 1: 1, 2: 2, 3: "b" }), "Invalid prop `2` of type `string` supplied to `testComponent`, " + "expected `number`.");
});
it("should warn with invalid complex types", function () {
function Thing() {}
var name = Thing.name || "<<anonymous>>";
typeCheckFail(PropTypes.mapOf(React.PropTypes.instanceOf(Thing)), new Immutable.Map({ 1: new Thing(), 2: "xyz" }), "Invalid prop `1` supplied to `testComponent`, expected instance of `" + name + "`.");
});
it("should warn when passed something other than an Immutable.Map", function () {
typeCheckFail(PropTypes.mapOf(React.PropTypes.number), { "0": "maybe-array", length: 1 }, "Invalid prop `testProp` of type `object` supplied to " + "`testComponent`, expected an Immutable.js Map.");
typeCheckFail(PropTypes.mapOf(PropTypes.number), 123, "Invalid prop `testProp` of type `number` supplied to " + "`testComponent`, expected an Immutable.js Map.");
typeCheckFail(PropTypes.mapOf(PropTypes.number), "string", "Invalid prop `testProp` of type `string` supplied to " + "`testComponent`, expected an Immutable.js Map.");
typeCheckFail(PropTypes.mapOf(PropTypes.number), [1, 2, 3], "Invalid prop `testProp` of type `array` supplied to " + "`testComponent`, expected an Immutable.js Map.");
});
it("should not warn when passing an empty object", function () {
typeCheckPass(PropTypes.mapOf(PropTypes.number), new Immutable.Map());
typeCheckPass(PropTypes.mapOf(PropTypes.number), new Immutable.Map({}));
});
it("should be implicitly optional and not warn without values", function () {
typeCheckPass(PropTypes.mapOf(PropTypes.number), null);
typeCheckPass(PropTypes.mapOf(PropTypes.number), undefined);
});
it("should warn for missing required values", function () {
typeCheckFail(PropTypes.mapOf(PropTypes.number).isRequired, null, requiredMessage);
typeCheckFail(PropTypes.mapOf(PropTypes.number).isRequired, undefined, requiredMessage);
});
});
describe("IterableOf Type", function () {
it("should support the iterableOf propTypes", function () {
typeCheckPass(PropTypes.iterableOf(React.PropTypes.number), new Immutable.List([1, 2, 3]));
typeCheckPass(PropTypes.iterableOf(React.PropTypes.string), new Immutable.List(["a", "b", "c"]));
typeCheckPass(PropTypes.iterableOf(React.PropTypes.oneOf(["a", "b"])), new Immutable.List(["a", "b"]));
typeCheckPass(PropTypes.iterableOf(React.PropTypes.number), new Immutable.Map({ 1: 1, 2: 2, 3: 3 }));
typeCheckPass(PropTypes.iterableOf(React.PropTypes.string), new Immutable.Map({ 1: "a", 2: "b", 3: "c" }));
typeCheckPass(PropTypes.iterableOf(React.PropTypes.oneOf(["a", "b"])), new Immutable.Map({ 1: "a", 2: "b" }));
});
it("should support iterableOf with complex types", function () {
function Thing() {}
typeCheckPass(PropTypes.iterableOf(React.PropTypes.shape({ a: React.PropTypes.number.isRequired })), new Immutable.List([{ a: 1 }, { a: 2 }]));
typeCheckPass(PropTypes.iterableOf(PropTypes.shape({ a: React.PropTypes.number.isRequired })), Immutable.fromJS([{ a: 1 }, { a: 2 }]));
typeCheckPass(PropTypes.iterableOf(React.PropTypes.instanceOf(Thing)), new Immutable.List([new Thing(), new Thing()]));
typeCheckPass(PropTypes.iterableOf(React.PropTypes.shape({ a: React.PropTypes.number.isRequired })), new Immutable.Map({ 1: { a: 1 }, 2: { a: 2 } }));
typeCheckPass(PropTypes.iterableOf(PropTypes.shape({ a: React.PropTypes.number.isRequired })), Immutable.fromJS({ 1: { a: 1 }, 2: { a: 2 } }));
typeCheckPass(PropTypes.iterableOf(React.PropTypes.instanceOf(Thing)), new Immutable.Map({ 1: new Thing(), 2: new Thing() }));
});
it("should warn with invalid items in the list", function () {
typeCheckFail(PropTypes.iterableOf(React.PropTypes.number), new Immutable.List([1, 2, "b"]), "Invalid prop `2` of type `string` supplied to `testComponent`, " + "expected `number`.");
typeCheckFail(PropTypes.iterableOf(React.PropTypes.number), new Immutable.Map({ 1: 1, 2: 2, 3: "b" }), "Invalid prop `2` of type `string` supplied to `testComponent`, " + "expected `number`.");
});
it("should warn with invalid complex types", function () {
function Thing() {}
var name = Thing.name || "<<anonymous>>";
typeCheckFail(PropTypes.iterableOf(React.PropTypes.instanceOf(Thing)), new Immutable.List([new Thing(), "xyz"]), "Invalid prop `1` supplied to `testComponent`, expected instance of `" + name + "`.");
typeCheckFail(PropTypes.iterableOf(React.PropTypes.instanceOf(Thing)), new Immutable.Map({ 1: new Thing(), 2: "xyz" }), "Invalid prop `1` supplied to `testComponent`, expected instance of `" + name + "`.");
});
it("should warn when passed something other than an Immutable.Iterable", function () {
typeCheckFail(PropTypes.iterableOf(React.PropTypes.number), { "0": "maybe-array", length: 1 }, "Invalid prop `testProp` of type `object` supplied to " + "`testComponent`, expected an Immutable.js Iterable.");
typeCheckFail(PropTypes.iterableOf(PropTypes.number), 123, "Invalid prop `testProp` of type `number` supplied to " + "`testComponent`, expected an Immutable.js Iterable.");
typeCheckFail(PropTypes.iterableOf(PropTypes.number), "string", "Invalid prop `testProp` of type `string` supplied to " + "`testComponent`, expected an Immutable.js Iterable.");
typeCheckFail(PropTypes.iterableOf(PropTypes.number), [1, 2, 3], "Invalid prop `testProp` of type `array` supplied to " + "`testComponent`, expected an Immutable.js Iterable.");
});
it("should not warn when passing an empty iterable", function () {
typeCheckPass(PropTypes.iterableOf(PropTypes.number), new Immutable.List());
typeCheckPass(PropTypes.iterableOf(PropTypes.number), new Immutable.List([]));
typeCheckPass(PropTypes.iterableOf(PropTypes.number), new Immutable.Map({}));
});
it("should be implicitly optional and not warn without values", function () {
typeCheckPass(PropTypes.iterableOf(PropTypes.number), null);
typeCheckPass(PropTypes.iterableOf(PropTypes.number), undefined);
});
it("should warn for missing required values", function () {
typeCheckFail(PropTypes.iterableOf(PropTypes.number).isRequired, null, requiredMessage);
typeCheckFail(PropTypes.iterableOf(PropTypes.number).isRequired, undefined, requiredMessage);
});
});
describe("Shape Types", function () {

@@ -150,52 +266,2 @@ it("should warn for non objects", function () {

});
describe("MapOf Type", function () {
it("should support the mapOf propTypes", function () {
typeCheckPass(PropTypes.mapOf(React.PropTypes.number), new Immutable.Map({ 1: 1, 2: 2, 3: 3 }));
typeCheckPass(PropTypes.mapOf(React.PropTypes.string), new Immutable.Map({ 1: "a", 2: "b", 3: "c" }));
typeCheckPass(PropTypes.mapOf(React.PropTypes.oneOf(["a", "b"])), new Immutable.Map({ 1: "a", 2: "b" }));
});
it("should support mapOf with complex types", function () {
typeCheckPass(PropTypes.mapOf(React.PropTypes.shape({ a: React.PropTypes.number.isRequired })), new Immutable.Map({ 1: { a: 1 }, 2: { a: 2 } }));
typeCheckPass(PropTypes.mapOf(PropTypes.shape({ a: React.PropTypes.number.isRequired })), Immutable.fromJS({ 1: { a: 1 }, 2: { a: 2 } }));
function Thing() {}
typeCheckPass(PropTypes.mapOf(React.PropTypes.instanceOf(Thing)), new Immutable.Map({ 1: new Thing(), 2: new Thing() }));
});
it("should warn with invalid items in the map list", function () {
typeCheckFail(PropTypes.mapOf(React.PropTypes.number), new Immutable.Map({ 1: 1, 2: 2, 3: "b" }), "Invalid prop `2` of type `string` supplied to `testComponent`, " + "expected `number`.");
});
it("should warn with invalid complex types", function () {
function Thing() {}
var name = Thing.name || "<<anonymous>>";
typeCheckFail(PropTypes.mapOf(React.PropTypes.instanceOf(Thing)), new Immutable.Map({ 1: new Thing(), 2: "xyz" }), "Invalid prop `1` supplied to `testComponent`, expected instance of `" + name + "`.");
});
it("should warn when passed something other than an Immutable.Map", function () {
typeCheckFail(PropTypes.mapOf(React.PropTypes.number), { "0": "maybe-array", length: 1 }, "Invalid prop `testProp` of type `object` supplied to " + "`testComponent`, expected an Immutable.js Map.");
typeCheckFail(PropTypes.mapOf(PropTypes.number), 123, "Invalid prop `testProp` of type `number` supplied to " + "`testComponent`, expected an Immutable.js Map.");
typeCheckFail(PropTypes.mapOf(PropTypes.number), "string", "Invalid prop `testProp` of type `string` supplied to " + "`testComponent`, expected an Immutable.js Map.");
typeCheckFail(PropTypes.mapOf(PropTypes.number), [1, 2, 3], "Invalid prop `testProp` of type `array` supplied to " + "`testComponent`, expected an Immutable.js Map.");
});
it("should not warn when passing an empty object", function () {
typeCheckPass(PropTypes.mapOf(PropTypes.number), new Immutable.Map());
typeCheckPass(PropTypes.mapOf(PropTypes.number), new Immutable.Map({}));
});
it("should be implicitly optional and not warn without values", function () {
typeCheckPass(PropTypes.mapOf(PropTypes.number), null);
typeCheckPass(PropTypes.mapOf(PropTypes.number), undefined);
});
it("should warn for missing required values", function () {
typeCheckFail(PropTypes.mapOf(PropTypes.number).isRequired, null, requiredMessage);
typeCheckFail(PropTypes.mapOf(PropTypes.number).isRequired, undefined, requiredMessage);
});
});
});

@@ -16,2 +16,3 @@ /**

mapOf: createMapOfTypeChecker,
iterableOf: createIterableOfTypeChecker,
shape: createShapeTypeChecker

@@ -53,9 +54,9 @@ };

function createListOfTypeChecker(typeChecker) {
function createIterableTypeChecker(typeChecker, immutableClassName, immutableClassTypeValidator) {
function validate(props, propName, componentName, location) {
var propValue = props[propName];
if (!Immutable.List.isList(propValue)) {
if (!immutableClassTypeValidator(propValue)) {
var locationName = location;
var propType = getPropType(propValue);
return new Error("Invalid " + locationName + " `" + propName + "` of type " + ("`" + propType + "` supplied to `" + componentName + "`, expected an Immutable.js List."));
return new Error("Invalid " + locationName + " `" + propName + "` of type " + ("`" + propType + "` supplied to `" + componentName + "`, expected an Immutable.js " + immutableClassName + "."));
}

@@ -73,21 +74,14 @@ var propValues = propValue.toArray();

function createListOfTypeChecker(typeChecker) {
return createIterableTypeChecker(typeChecker, "List", Immutable.List.isList);
}
function createMapOfTypeChecker(typeChecker) {
function validate(props, propName, componentName, location) {
var propValue = props[propName];
if (!Immutable.Map.isMap(propValue)) {
var locationName = location;
var propType = getPropType(propValue);
return new Error("Invalid " + locationName + " `" + propName + "` of type " + ("`" + propType + "` supplied to `" + componentName + "`, expected an Immutable.js Map."));
}
var propValues = propValue.toArray();
for (var i = 0, len = propValues.length; i < len; i++) {
var error = typeChecker(propValues, i, componentName, location);
if (error instanceof Error) {
return error;
}
}
}
return createChainableTypeChecker(validate);
return createIterableTypeChecker(typeChecker, "Map", Immutable.Map.isMap);
}
function createIterableOfTypeChecker(typeChecker) {
return createIterableTypeChecker(typeChecker, "Iterable", Immutable.Iterable.isIterable);
}
// there is some irony in the fact that shapeTypes is a standard hash and not an immutable collection

@@ -94,0 +88,0 @@ function createShapeTypeChecker(shapeTypes) {

{
"name": "react-immutable-proptypes",
"version": "0.1.5",
"version": "0.1.6",
"description": "PropType validators that work with Immutable.js.",

@@ -5,0 +5,0 @@ "main": "dist/ImmutablePropTypes.js",

@@ -44,4 +44,6 @@ # react-immutable-proptypes

* `ImmutablePropTypes.mapOf` is basically the same as `listOf`, but it is specific to `Immutable.Map`. I am open to creating an `ImmutablePropTypes.iterableOf` that would validate against any `Immutable.Iterable` if anybody wants it.
* `ImmutablePropTypes.mapOf` is basically the same as `listOf`, but it is specific to `Immutable.Map`.
* `ImmutbalePropTypes.iterableOf` is the generic form of listOf/mapOf. It is useful when there is no need to validate anything other than Immutable.js compatible (ie. `Immutable.Iterable`). Continue to use `listOf` and/or `mapOf` when you know the type.
* `ImmutablePropTypes.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 `shape` to validate an array via `Immutable.List`.

@@ -48,0 +50,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc