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.

Show only one error message

See original GitHub issue

For my field validation, I have

yup.string()
  .matches(
    /^[A-Z]/,
    {
      message: 'Name must start with a capital letter',
      excludeEmptyString: true,
    },
  )
  .matches(
    /^.[a-zA-Z0-9_]+$/,
    {
      message: 'Alphanumeric characters or underscores only',
      excludeEmptyString: true,
    },
  )
  .required('Name is requried.'),

Sometimes, both errors from the matches(), will show. This makes sense since both errors triggered. However, the way it’s displayed can be a bit messy:

Name must start with a capital letter., Alphanumeric characters or underscores only.

Is there any way to only show one message at a time? Or possibly replace the comma with a period?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
kendallrothcommented, Mar 2, 2018

Thanks @jquense, I think this will work (grabbing first error field and using as error):

let errors = [];

try {
    isValid = invitationSchema.validateSync(row, { abortEarly: false });
} catch (e) {
  const nameError = e.inner.find((ve) => ve.path === 'name')
    ? { name: e.inner.find((ve) => ve.path === 'name').message }
    : null;

  errors.push({
    ...nameError,
  });

  // result: { name: 'First message for name' }
}

1reaction
kendallrothcommented, Mar 2, 2018

@jquense Understood. I just double checked and it appears that changing the order of the chained name validation methods will change the order in which the errors appear. For instance, switching to Yup.string().required().min() will display the required message before the min messages, whereas Yup.string().min().required() will display it afterward.

Or is this just a lucky coincidence because the first one just happaned to start before the other? Also, if validateSync is synchronous, do the validations still run in parallel?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Displaying only one error message per field - Stack Overflow
Inspired by new validation methods in Rails 3.0 I'm adding this tiny Validator. I call it ReduceValidator . lib/reduce_validator.rb : # show ......
Read more >
Display all validation rule errors at once - IdeaExchange
If you click on the Edit button and trigger more than one validation rule, all the messages appear. If I inline edit, only...
Read more >
Displaying Errors - VeeValidate
Typically you would want to display one error at a time for your fields, you can do this using errors.first('fieldName') method. ... VeeValidate...
Read more >
Showing only one message each time - FormValidation
This event is triggered when the field doesn't pass a particular validator. In the following registration form, the username field must pass three...
Read more >
UX Design: Four ways to display error messages - Nomensa
One example of an unclear error message is on the Hotmail registration ... Before the year 2000, it was common to use only...
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