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

feat: add extendValidator #12738

Open
wants to merge 2 commits into
base: main
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
35 changes: 35 additions & 0 deletions packages/runtime-core/__tests__/componentProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,41 @@ describe('component props', () => {
})
})

test('extendValidator custom warn message', async () => {
let warnMsg = ''
vi.spyOn(console, 'warn').mockImplementation(msg => {
warnMsg = msg
})
const Comp = defineComponent({
props: {
foo: {
type: Number,
extendValidator: (name, value, props, warn) => {
if (typeof value !== 'number') {
warn(
'Invalid prop: custom validator check failed for prop "' +
name +
'".',
)
} else if (!Number.isInteger(value)) {
warn(`Invalid prop: ${name}. Expected an integer.`)
}
},
},
bar: {
type: Number,
},
},
template: `<div />`,
})

// Note this one is using the main Vue render so it can compile template
// on the fly
const root = document.createElement('div')
domRender(h(Comp, { foo: 1.1, bar: 2 }), root)
expect(warnMsg).toMatch(`Invalid prop: foo. Expected an integer.`)
})

//#12011
test('replace camelize with hyphenate to handle props key', () => {
const Comp = {
Expand Down
13 changes: 12 additions & 1 deletion packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
toRawType,
} from '@vue/shared'
import { warn } from './warning'
import type { WarnFunction } from './warning'
import {
type ComponentInternalInstance,
type ComponentOptions,
Expand Down Expand Up @@ -57,6 +58,12 @@ export interface PropOptions<T = any, D = T> {
required?: boolean
default?: D | DefaultFactory<D> | null | undefined | object
validator?(value: unknown, props: Data): boolean
extendValidator?: (
name: string,
value: unknown,
props: Data,
warn: WarnFunction,
) => unknown
/**
* @internal
*/
Expand Down Expand Up @@ -678,7 +685,7 @@ function validateProp(
props: Data,
isAbsent: boolean,
) {
const { type, required, validator, skipCheck } = prop
const { type, required, validator, skipCheck, extendValidator } = prop
// required!
if (required && isAbsent) {
warn('Missing required prop: "' + name + '"')
Expand All @@ -705,6 +712,10 @@ function validateProp(
}
}
// custom validator
if (extendValidator) {
extendValidator(name, value, props, warn)
return
}
if (validator && !validator(value, props)) {
warn('Invalid prop: custom validator check failed for prop "' + name + '".')
}
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/src/warning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ export function warn(msg: string, ...args: any[]): void {
isWarning = false
}

export type WarnFunction = typeof warn

export function getComponentTrace(): ComponentTraceStack {
let currentVNode: VNode | null = stack[stack.length - 1]
if (!currentVNode) {
Expand Down
Loading