question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom error message from prop validator

See original GitHub issue

What 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:open
  • Created 5 years ago
  • Reactions:12
  • Comments:20 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
antoniandrecommented, Mar 28, 2021

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:

selectableRows: {
      validator: value => {
        if (![undefined, true, false, 1, '1', ''].includes(value)) {
          console.error(
            'Wrong value for the `selectableRows` prop. ' +
            `Given: "${value}", expected one of: [undefined, true, false, 1, '1', ''].`
          )
        }
        return true
      }
    }
2reactions
chrisvfritzcommented, Mar 12, 2019

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom Error Messages - express-validator
Custom Error Messages ... express-validator's default error message is a simple Invalid value . That's enough to cover all fields without being too...
Read more >
Vue.js Custom Prop Validation
In this lesson, we'll create a custom validator for our post prop, and also throw errors in the console if the post is...
Read more >
About Validation Error Messages - Contentstack
The Validation Error Message property lets you define a custom error message to display if the validation checks specified in the Validation (Regex)...
Read more >
Custom Validators - Vuelidate
Custom error messages #. While validators from the @vuelidate/validators package come with basic error messages, you may want to override them or define ......
Read more >
How to create a custom validation error message that contains ...
Describes the steps to create an inline error message from the user interface or from a script. The inline message has a custom...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found