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 Symbol to String conversion in ReactPropTypes.oneOf #9202

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/isomorphic/classic/types/ReactPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,15 @@ function createEnumTypeChecker(expectedValues) {
return emptyFunction.thatReturnsNull;
}

function replaceSymbol(key, value) {
var valueType = getPropType(value);
if (valueType === 'symbol') {
return value.toString();
} else {
return value;
}
}

function validate(props, propName, componentName, location, propFullName) {
var propValue = props[propName];
for (var i = 0; i < expectedValues.length; i++) {
Expand All @@ -326,9 +335,11 @@ function createEnumTypeChecker(expectedValues) {
}
}

var valuesString = JSON.stringify(expectedValues);
// eslint-disable-next-line react-internal/no-primitive-constructors
var propValueString = String(propValue);
var valuesString = JSON.stringify(expectedValues, replaceSymbol);
return new PropTypeError(
`Invalid ${location} \`${propFullName}\` of value \`${propValue}\` ` +
`Invalid ${location} \`${propFullName}\` of value \`${propValueString}\` ` +
`supplied to \`${componentName}\`, expected one of ${valuesString}.`,
);
}
Expand Down
13 changes: 13 additions & 0 deletions src/isomorphic/classic/types/__tests__/ReactPropTypes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -872,12 +872,25 @@ describe('ReactPropTypes', () => {
'Invalid prop `testProp` of value `false` supplied to ' +
'`testComponent`, expected one of [0,"false"].',
);
typeCheckFail(
PropTypes.oneOf(['red', 'blue']),
Symbol('red'),
'Invalid prop `testProp` of value `Symbol(red)` supplied to ' +
'`testComponent`, expected one of ["red","blue"].',
);
typeCheckFail(
PropTypes.oneOf([0, Symbol.for('blue')]),
Symbol.for('red'),
'Invalid prop `testProp` of value `Symbol(red)` supplied to ' +
'`testComponent`, expected one of [0,"Symbol(blue)"].',
);
});

it('should not warn for valid values', () => {
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'red');
typeCheckPass(PropTypes.oneOf(['red', 'blue']), 'blue');
typeCheckPass(PropTypes.oneOf(['red', 'blue', NaN]), NaN);
typeCheckPass(PropTypes.oneOf([0, Symbol.for('red')]), Symbol.for('red'));
});

it('should be implicitly optional and not warn without values', () => {
Expand Down