-
Notifications
You must be signed in to change notification settings - Fork 358
disallow null #57
Comments
I have a similar but opposite requirement. I want to allow I was thinking of adding |
|
@ljharb Perhaps I'm missing something, but |
@lukescott ah, true. (Forbidding |
@ljharb This was the best stand-alone work-around I could come up with: // Usage: notNull(PropType.string)
if (process.env.NODE_ENV !== "production") {
var secret = require("prop-types/lib/ReactPropTypesSecret")
}
function notNull(validator) {
if (process.env.NODE_ENV !== "production") {
if (!validator.isRequired) {
return validator
}
return (props, propName, componentName, location, propFullName) => {
const propValue = props[propName]
if (propValue === null) {
return new Error(
"The " + location +
" `" + propFullName +
"` in `" + componentName +
"` must not be `null`."
)
}
return validator(
props, propName, componentName, location, propFullName, secret
)
}
}
}
module.exports = function noNulls(types) {
if (process.env.NODE_ENV !== "production") {
const newTypes = {}
Object.keys(types).forEach(key => {
newTypes[key] = notNull(types[key])
})
return newTypes
} else {
return types
}
} The goal of the PR was to get something built-in though. Especially since my work-around relies on ReactPropTypesSecret. |
That's never required - change it to |
When a type is optional it allows
undefined
ornull
. Unfortuantly prop-types eats thenull
and doesn't run any validators, so there doesn't seem to be a way to create a custom validator to disallow nulls.The text was updated successfully, but these errors were encountered: