Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

disallow null #57

Open
lukescott opened this issue May 18, 2017 · 6 comments · May be fixed by #90
Open

disallow null #57

lukescott opened this issue May 18, 2017 · 6 comments · May be fixed by #90
Labels
new validator request Requests for a new kind of validator.

Comments

@lukescott
Copy link

When a type is optional it allows undefined or null. Unfortuantly prop-types eats the null and doesn't run any validators, so there doesn't seem to be a way to create a custom validator to disallow nulls.

@jharris4
Copy link

I have a similar but opposite requirement. I want to allow null but not allow undefined.

facebook/react#3163 (comment)

I was thinking of adding isDefined to complement isRequired

@jharris4 jharris4 linked a pull request Jul 13, 2017 that will close this issue
@ljharb
Copy link
Collaborator

ljharb commented Jul 13, 2017

airbnb-prop-types has explicitNull for this use case.

@lukescott
Copy link
Author

@ljharb Perhaps I'm missing something, but explicitNull requires null. In my case I disallow null and allow undefined only when not required. Currently prop-types allows null or undefined when not required. What we're trying to achieve is choosing one or the other, not both.

@ljharb
Copy link
Collaborator

ljharb commented Jul 13, 2017

@lukescott ah, true. explicitNull does indeed require null - if you'd file an issue on airbnb-prop-types, i can add the ability to disallow it :-)

(Forbidding undefined is the same as "requiring null" or "requiring some other value", or the composition of those two, so it really doesn't need its own API imo)

@lukescott
Copy link
Author

lukescott commented Jul 13, 2017

@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.

@ljharb
Copy link
Collaborator

ljharb commented Jul 13, 2017

That's never required - change it to return (props, propName, componentName, location, propFullName, ...rest) => { and props, propName, componentName, location, propFullName, ...rest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new validator request Requests for a new kind of validator.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants