Skip to content

Commit

Permalink
feat: rename extendValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
btea committed Jan 19, 2025
1 parent bec7292 commit 942328e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
17 changes: 11 additions & 6 deletions packages/runtime-core/__tests__/componentProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ describe('component props', () => {
})
})

test('validator should allow custom warning messages', async () => {
test('extendValidator custom warn message', async () => {
let warnMsg = ''
vi.spyOn(console, 'warn').mockImplementation(msg => {
warnMsg = msg
Expand All @@ -342,11 +342,16 @@ describe('component props', () => {
props: {
foo: {
type: Number,
validator: (value, props) => {
if (!Number.isInteger(value)) {
return `Invalid prop: ${props.foo}. Expected an integer.`
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.`)
}
return true
},
},
bar: {
Expand All @@ -360,7 +365,7 @@ describe('component props', () => {
// on the fly
const root = document.createElement('div')
domRender(h(Comp, { foo: 1.1, bar: 2 }), root)
expect(warnMsg).toMatch(`Invalid prop: 1.1. Expected an integer.`)
expect(warnMsg).toMatch(`Invalid prop: foo. Expected an integer.`)
})

//#12011
Expand Down
26 changes: 15 additions & 11 deletions 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 @@ -56,7 +57,13 @@ export interface PropOptions<T = any, D = T> {
type?: PropType<T> | true | null
required?: boolean
default?: D | DefaultFactory<D> | null | undefined | object
validator?(value: unknown, props: Data): boolean | string
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,15 +712,12 @@ function validateProp(
}
}
// custom validator
if (validator) {
const validatorResult = validator(value, props)
let msg = `'Invalid prop: custom validator check failed for prop "${name}".'`
if (typeof validatorResult === 'string') {
msg += ` Reason: ${validatorResult}.`
warn(msg)
} else if (!validatorResult) {
warn(msg)
}
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

0 comments on commit 942328e

Please sign in to comment.