-
Notifications
You must be signed in to change notification settings - Fork 358
mapOf and setOf validators #289
base: main
Are you sure you want to change the base?
Conversation
One of the things that I'm sure is worth discussing is the structure of the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, the tests are great!
It's not yet certain that this is something that will be added, and it's blocked on my comments below, but having the PR is helpful.
EDIT: I'm dumb, the secret lives in a lib module. I can just use it. |
I'm going to add a |
@ljharb I guess I can't dismiss your review as stale, but I believe I have made the changes that you requested, including your improved method for detecting Please don't ship this yet though. I have to finish writing the tests. I'm just waiting to do it until I have more of an idea whether you would accept these changes. |
ce9f899
to
2a391b0
Compare
OK I take back what I said. All the tests are here. CHANGELOG and README are updated. When you get to looking at this, it is ready to merge from my perspective. |
@@ -1,3 +1,6 @@ | |||
## 15.7.3 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adding a new thing is semver-minor
## 15.7.3 | |
## 15.8.0 |
@@ -269,6 +288,107 @@ module.exports = function(isValidElement, throwOnDirectAccess) { | |||
} | |||
return createChainableTypeChecker(validate); | |||
} | |||
|
|||
function createTupleOfTypeChecker(typeCheckerTuple) { | |||
var argsInvalid = !Array.isArray(typeCheckerTuple) || !typeCheckerTuple.every(function (checker) { return typeof checker === 'function'}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var argsInvalid = !Array.isArray(typeCheckerTuple) || !typeCheckerTuple.every(function (checker) { return typeof checker === 'function'}); | |
var argsInvalid = !Array.isArray(typeCheckerTuple) || typeCheckerTuple.some(function (checker) { return typeof checker !== 'function'}); |
return new PropTypeError('Property `' + propFullName + '` of component `' + componentName + '` has invalid PropType notation inside iterableOf.'); | ||
} | ||
if (!(Symbol && Symbol.iterator)) { | ||
return new PropTypeError('Cannot use `PropTypes.iterableOf` if Symbol.iterator is not present'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
technically this isn't true; you could use Array.from
exclusively, which can be polyfilled.
var keyChecker = typeCheckers[0]; | ||
var valueChecker = typeCheckers[1]; | ||
if (typeof Map !== 'function') { | ||
return new PropTypeError('Cannot use `PropTypes.mapOf` if the Map data structure is not present'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this necessary? if Map
isn't present, isMap
will fail.
|
||
function validate(props, propName, componentName, location, propFullName) { | ||
if (typeof Set !== 'function') { | ||
return new PropTypeError('Cannot use `PropTypes.setOf` if the Set data structure is not present'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
@@ -0,0 +1,9 @@ | |||
module.exports = function forEach(iterable, callback) { | |||
var iterator = iterable[Symbol.iterator](); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this utility should use Array.from
, and fall back to explicit array/string iteration
Fixes #190
I also intend to add iterableOf to this.