Skip to content

feat(props): allow defining a required prop as null #9358

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

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
8 changes: 5 additions & 3 deletions src/core/util/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,19 +149,21 @@ function assertProp (

const simpleCheckRE = /^(String|Number|Boolean|Function|Symbol|BigInt)$/

function assertType (value: any, type: Function, vm: ?Component): {
function assertType (value: any, type: Function | null, vm: ?Component): {
valid: boolean;
expectedType: string;
} {
let valid
const expectedType = getType(type)
const expectedType = type ? getType(type) : 'null'
if (simpleCheckRE.test(expectedType)) {
const t = typeof value
valid = t === expectedType.toLowerCase()
// for primitive wrapper objects
if (!valid && t === 'object') {
valid = value instanceof type
}
} else if (expectedType === 'null') {
valid = value === null
} else if (expectedType === 'Object') {
valid = isPlainObject(value)
} else if (expectedType === 'Array') {
Expand Down Expand Up @@ -210,7 +212,7 @@ function getTypeIndex (type, expectedTypes): number {

function getInvalidTypeMessage (name, value, expectedTypes) {
let message = `Invalid prop: type check failed for prop "${name}".` +
` Expected ${expectedTypes.map(capitalize).join(', ')}`
` Expected ${expectedTypes.map(type => type === 'null' ? type : capitalize(type)).join(', ')}`
const expectedType = expectedTypes[0]
const receivedType = toRawType(value)
// check if we need to specify expected value
Expand Down
45 changes: 45 additions & 0 deletions test/unit/features/options/props.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,51 @@ describe('Options props', () => {
makeInstance(null, true)
expect(console.error.calls.count()).toBe(0)
})

it('required + null accepts anything', () => {
makeInstance({}, null, null, true)
makeInstance(null, null, null, true)
makeInstance(1, null, null, true)
makeInstance('foo', null, null, true)
makeInstance(() => {}, null, null, true)
makeInstance(undefined, null, null, true)
expect(console.error.calls.count()).toBe(0)
})

it('required + nullable object', () => {
makeInstance(null, [Object, null], null, true)
expect(console.error.calls.count()).toBe(0)
makeInstance({}, [Object, null], null, true)
expect(console.error.calls.count()).toBe(0)
makeInstance(undefined, [Object, null], null, true)
expect('Expected Object, null').toHaveBeenWarned()
})

it('non required + nullable object', () => {
makeInstance(null, [Object, null])
expect(console.error.calls.count()).toBe(0)
makeInstance({}, [Object, null])
expect(console.error.calls.count()).toBe(0)
makeInstance(undefined, [Object, null])
expect(console.error.calls.count()).toBe(0)
makeInstance('hello', [Object, null])
expect('Expected Object, null').toHaveBeenWarned()
})

it('required + nullable non object', () => {
makeInstance(null, [String, null], null, true)
expect(console.error.calls.count()).toBe(0)
makeInstance('foo', [String, null], null, true)
expect(console.error.calls.count()).toBe(0)
makeInstance(undefined, [String, null], null, true)
expect('Expected String, null').toHaveBeenWarned()
})

it('non-required nullable string fails with object', () => {
makeInstance({}, [String, null])
expect('Expected String, null').toHaveBeenWarned()
})

})

it('should work with v-bind', () => {
Expand Down