Custom error message from prop validator
See original GitHub issueWhat problem does this feature solve?
Currently, if a custom validator fails, we get a console error log saying Invalid prop: custom validator check failed for prop 'email'
which is not helpful if you’re using a third-party component. The only way to find out what failed is to jump into the source code of the component and try to understand what does this custom validator do. If the custom validator can provide a custom message that immensely changes developer experience e.g. Instead of Invalid prop: custom validator check failed for prop 'email'
, it can say, Invalid prop: the prop 'email' should be a valid GMail address.
What does the proposed API look like?
No change in API signature only behavior of validator
function. If a validator function throws an error, use it as a custom message for prop validation. Also, allow {{name}}
interpolation in error message. So the email
can be defined as:
...
props: {
email: {
validator(value) {
if (!value.endsWith('@gmail.com')) throw new Error('the prop '{{name}}' should be a valid GMail address.')
return true
}
}
...
Issue Analytics
- State:
- Created 5 years ago
- Reactions:12
- Comments:20 (9 by maintainers)
Top GitHub Comments
The simplest way for now to print a custom error message and not produce a double error is to raise a console error (in my case I don’t want to throw an Error) and return true in the validator function. Like in this example:
When the returned value from a validator is truthy, I wonder if it would be simpler to check if what’s returned is a string, then if it is, make that the warning message. Thoughts?