prop-types icon indicating copy to clipboard operation
prop-types copied to clipboard

Return value from checkPropTypes?

Open madcapnmckay opened this issue 7 years ago • 6 comments

Is there a good reason not to return false if checkPropTypes fails any of the validations? I can't think of one myself. If no one disagrees I can do a PR.

This would be useful for those of us that want to use checkPropTypes for our own validation needs outside of React.

madcapnmckay avatar Mar 01 '18 21:03 madcapnmckay

agreed, right now I work around it by passing this as getStack():

	let error
	PropTypes.checkPropTypes(...,
				() => {
					const error = new Error('options do not match definition')
					return error.stack
				}
	)
	if (error) throw error

EDIT:

actually that was annoying in tests - so now I mock console.error:

const t = console.error
console.error = msg => {throw new Error(msg)}
checkPropTypes(...)
console.error = t

wmertens avatar Aug 15 '18 12:08 wmertens

@madcapnmckay no one seems to disagree

samburgers avatar Jan 07 '20 02:01 samburgers

Would very much enjoy this, I don't see much of a reason to not provide an API to capture the errors that are being emitted to the console.

JimmayVV avatar May 07 '20 20:05 JimmayVV

+1 Yeah I was just testing a problem using this function, and was confused why it wouldn't return some kind of information, even if just a boolean.

markymc avatar Dec 14 '20 05:12 markymc

This seems like an obvious good change to make, but I am concerned that someone is relying on checkPropTypes returning undefined, or a falsy value - meaning, returning false is fine but returning true might break someone.

ljharb avatar Dec 22 '21 22:12 ljharb

Still open? I shall take this on?

frostzt avatar Jun 04 '22 07:06 frostzt