Add extendValidator
prop or extending validator parameters
#740
btea
started this conversation in
RFC Discussions
Replies: 2 comments 24 replies
-
Can't you just add console.warn in normal validator? |
Beta Was this translation helpful? Give feedback.
13 replies
-
I personally don't use validators nor ever use them extensible, I mostly rely on Typescript to statically check things. IMO this can be done in a eg: export function doValidation<T extends Record<string, any>, N extends keyof T>(props: T, name: N, validator: (name: N, value: T[N], props: T) => void) {
watch(() => props[name], (v) => {
validator(name, v, props)
}, {
immediate: true,
flush: 'sync'
})
}
export function extendedNumberValidator<T extends Record<string, any>, N extends keyof T & string, V extends T[N]>(name: N, value: V, props: T) {
if (typeof value !== 'number') {
warn(`[Custom Component]: Invalid prop: custom validator check failed for prop "${name}", expected type number, but got ${typeof value}`);
} else if (!Number.isInteger(value)) {
warn(`[Custom Component]: Invalid prop: custom validator check failed for prop "${name}", expected an integer.`)
}
}
const props = defineProps({
num: Number
})
doValidation(props, 'num', extendedNumberValidator) |
Beta Was this translation helpful? Give feedback.
11 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
Expand the warning information of validator verification to provide clearer error prompts.
Basic example
Motivation
In custom components, we usually validate many props for a better user experience. Currently, the validator function will only return true or false. If it is false, it means that the validation has failed and a warning message will be thrown inside vue.
However, the warning message only indicates that a prop check failed, but users cannot customize and expand more specific error descriptions.
Detailed design
In order not to affect the original users, it may be a good idea to add a new function to give users more customization space.
refer #Basic example
Drawbacks
In most cases, the current validator function will suffice. (This proposal is merely to optimize the development experience based on the original one.)
Alternatives
Add new parameters based on the original validator and make different processing according to different parameters.
Adoption strategy
Unresolved questions
N/A
Beta Was this translation helpful? Give feedback.
All reactions