Skip to content

Commit 23d4fb5

Browse files
authored
fix(runtime-core): avoid symbol coercion during props validation (#8539)
close #8487
1 parent 090b2e3 commit 23d4fb5

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

packages/runtime-core/__tests__/componentProps.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ describe('component props', () => {
406406
props: {
407407
bool: { type: Boolean },
408408
str: { type: String },
409+
symStr: { type: String },
410+
sym: { type: Symbol },
409411
num: { type: Number },
410412
arr: { type: Array },
411413
obj: { type: Object },
@@ -422,6 +424,8 @@ describe('component props', () => {
422424
h(Comp, {
423425
bool: 'true',
424426
str: 100,
427+
symStr: Symbol(),
428+
sym: 'symbol',
425429
num: '100',
426430
arr: {},
427431
obj: 'false',
@@ -438,6 +442,12 @@ describe('component props', () => {
438442
expect(
439443
`Invalid prop: type check failed for prop "str". Expected String with value "100", got Number with value 100.`,
440444
).toHaveBeenWarned()
445+
expect(
446+
`Invalid prop: type check failed for prop "symStr". Expected String, got Symbol`,
447+
).toHaveBeenWarned()
448+
expect(
449+
`Invalid prop: type check failed for prop "sym". Expected Symbol, got String with value "symbol".`,
450+
).toHaveBeenWarned()
441451
expect(
442452
`Invalid prop: type check failed for prop "num". Expected Number with value 100, got String with value "100".`,
443453
).toHaveBeenWarned()

packages/runtime-core/src/componentProps.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
isOn,
2222
isReservedProp,
2323
isString,
24+
isSymbol,
2425
makeMap,
2526
toRawType,
2627
} from '@vue/shared'
@@ -777,7 +778,7 @@ function getInvalidTypeMessage(
777778
if (
778779
expectedTypes.length === 1 &&
779780
isExplicable(expectedType) &&
780-
!isBoolean(expectedType, receivedType)
781+
isCoercible(expectedType, receivedType)
781782
) {
782783
message += ` with value ${expectedValue}`
783784
}
@@ -793,7 +794,9 @@ function getInvalidTypeMessage(
793794
* dev only
794795
*/
795796
function styleValue(value: unknown, type: string): string {
796-
if (type === 'String') {
797+
if (isSymbol(value)) {
798+
return value.toString()
799+
} else if (type === 'String') {
797800
return `"${value}"`
798801
} else if (type === 'Number') {
799802
return `${Number(value)}`
@@ -813,6 +816,9 @@ function isExplicable(type: string): boolean {
813816
/**
814817
* dev only
815818
*/
816-
function isBoolean(...args: string[]): boolean {
817-
return args.some(elem => elem.toLowerCase() === 'boolean')
819+
function isCoercible(...args: string[]): boolean {
820+
return args.every(elem => {
821+
const value = elem.toLowerCase()
822+
return value !== 'boolean' && value !== 'symbol'
823+
})
818824
}

0 commit comments

Comments
 (0)