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

Fix for #5468: Validate proptype definitions sooner #6316

Merged
merged 6 commits into from
Jun 29, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/isomorphic/classic/types/ReactPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var ReactPropTypeLocationNames = require('ReactPropTypeLocationNames');

var emptyFunction = require('emptyFunction');
var getIteratorFn = require('getIteratorFn');
var warning = require('warning');

/**
* Collection of methods that allow declaration and validation of props that are
Expand Down Expand Up @@ -226,11 +227,16 @@ function createInstanceTypeChecker(expectedClass) {

function createEnumTypeChecker(expectedValues) {
if (!Array.isArray(expectedValues)) {
return createChainableTypeChecker(function() {
return new Error(
`Invalid argument supplied to oneOf, expected an instance of array.`
);
});
if (__DEV__) {
warning(false, 'Invalid argument supplied to oneOf, expected an instance of array.');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We would still want to return a function for consistency—it would just be a no-op.

Copy link
Contributor Author

@troydemonbreun troydemonbreun Jun 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaearon no-oped, or should I return false?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s use emptyFunction.thatReturnsNull (if I recall correctly)

return emptyFunction.thatReturnsNull;
} else {
return createChainableTypeChecker(function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with DEV behavior, let’s return emptyFunction.thatReturnsNull here as well. This way we treat bad argument as early DEV warning, and since we can’t really check propTypes, we don’t.

Not that all these semantics really matter because nobody should be calling prop types in prod but it’s best to have them behave the same way until we explicitly disallow it.

Copy link
Contributor Author

@troydemonbreun troydemonbreun Jun 27, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaearon oh, so don't call createChainableTypeChecker as an alternate branch flow, always call warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaearon pushed my interpretation of your comments, thanks

return new Error(
`Invalid argument supplied to oneOf, expected an instance of array.`
);
});
}
}

function validate(props, propName, componentName, location, propFullName) {
Expand Down Expand Up @@ -288,11 +294,16 @@ function createObjectOfTypeChecker(typeChecker) {

function createUnionTypeChecker(arrayOfTypeCheckers) {
if (!Array.isArray(arrayOfTypeCheckers)) {
return createChainableTypeChecker(function() {
return new Error(
`Invalid argument supplied to oneOfType, expected an instance of array.`
);
});
if (__DEV__) {
warning(false, 'Invalid argument supplied to oneOfType, expected an instance of array.');
return emptyFunction.thatReturnsNull;
} else {
return createChainableTypeChecker(function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. So basically both of these blocks should become

warning(...)
return emptyFunction.thatReturnsNull;

__DEV__ check will be added automatically when you call warning.

return new Error(
`Invalid argument supplied to oneOfType, expected an instance of array.`
);
});
}
}

function validate(props, propName, componentName, location, propFullName) {
Expand Down
24 changes: 14 additions & 10 deletions src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -591,11 +591,13 @@ describe('ReactPropTypes', function() {

describe('OneOf Types', function() {
it('should fail for invalid argument', function() {
typeCheckFail(
PropTypes.oneOf('red', 'blue'),
'red',
'Invalid argument supplied to oneOf, expected an instance of array.'
);
spyOn(console, ['error']);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to spyOn(console, 'error') like the rest of the codebase does?


PropTypes.oneOf('red', 'blue');

expect(console.error).toHaveBeenCalled();
expect(console.error.calls.argsFor(0)[0])
.toContain('Invalid argument supplied to oneOf, expected an instance of array.');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let’s additionally test that type check passes (since that’s the behavior we have now).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gaearon feedback implemented, thanks

});

it('should warn for invalid values', function() {
Expand Down Expand Up @@ -652,11 +654,13 @@ describe('ReactPropTypes', function() {

describe('Union Types', function() {
it('should fail for invalid argument', function() {
typeCheckFail(
PropTypes.oneOfType(PropTypes.string, PropTypes.number),
'red',
'Invalid argument supplied to oneOfType, expected an instance of array.'
);
spyOn(console, ['error']);

PropTypes.oneOfType(PropTypes.string, PropTypes.number);

expect(console.error).toHaveBeenCalled();
expect(console.error.calls.argsFor(0)[0])
.toContain('Invalid argument supplied to oneOfType, expected an instance of array.');
});

it('should warn if none of the types are valid', function() {
Expand Down