wip: optimize props validation

This commit is contained in:
Evan You 2024-12-05 17:50:09 +08:00
parent fc9aa62248
commit 4baaa7bca3
No known key found for this signature in database
GPG Key ID: 00E9AB7A6704CE0A
2 changed files with 21 additions and 20 deletions

View File

@ -701,15 +701,16 @@ export function validateProps(
resolvedProps = toRaw(resolvedProps)
const camelizePropsKey = Object.keys(rawProps).map(key => camelize(key))
for (const key in options) {
let opt = options[key]
if (opt == null) continue
validateProp(
key,
resolvedProps[key],
opt,
__DEV__ ? shallowReadonly(resolvedProps) : resolvedProps,
!camelizePropsKey.includes(key),
)
const opt = options[key]
if (opt != null) {
validateProp(
key,
resolvedProps[key],
opt,
resolvedProps,
!camelizePropsKey.includes(key),
)
}
}
}
@ -750,7 +751,10 @@ function validateProp(
}
}
// custom validator
if (validator && !validator(value, resolvedProps)) {
if (
validator &&
!validator(value, __DEV__ ? shallowReadonly(resolvedProps) : resolvedProps)
) {
warn('Invalid prop: custom validator check failed for prop "' + key + '".')
}
}

View File

@ -1,12 +1,4 @@
import {
EMPTY_ARR,
NO,
YES,
camelize,
extend,
hasOwn,
isFunction,
} from '@vue/shared'
import { EMPTY_ARR, NO, YES, camelize, hasOwn, isFunction } from '@vue/shared'
import type { VaporComponent, VaporComponentInstance } from './component'
import {
type NormalizedPropsOptions,
@ -239,7 +231,12 @@ export function setupPropsValidation(instance: VaporComponentInstance): void {
const rawProps = instance.rawProps
if (!rawProps) return
renderEffect(() => {
const mergedRawProps = extend({}, rawProps)
const mergedRawProps: Record<string, any> = {}
for (const key in rawProps) {
if (key !== '$') {
mergedRawProps[key] = rawProps[key]()
}
}
if (rawProps.$) {
for (const source of rawProps.$) {
const isDynamic = isFunction(source)