#Custom Error Message for Validation Failure

7 messages · Page 1 of 1 (latest)

verbal mortarBOT
#

Any good solution off setting a custom error message when beforeValidate fails? Its not that much worth to just get this message:

quick gobletBOT
#

Original message from @tribal coral - Moved from #general message

#
grim pond
#

Hey @tribal coral

Yep iirc this is possible. You're going to want to throw an error instead of returning a string I think. Like this:

if (article._status !== 'published') {
  throw new ValidationError({
      collection: 'posts',
      id: data.id,
      errors: [{ message: 'My custom error msg', path: '_status' }],
  })
}

Give that a shot

tribal coral
#

Im still getting this after changing my code to this:

if (article._status !== "published") {
    throw new ValidationError({
        collection: collection?.slug,
        id: 1,
        errors: [
            { message: "Article must be published", path: "_status" },
        ],
    });
}

This is how it looks:

{
  collection: 'feeds',
  id: 1,
  errors: [ { message: 'Article must be published', path: '_status' } ]
}

(I've also tried)

id: data?.id,
grim pond
#

Hmm, I guess since that field is hidden, your editor will have no way to actually see the msg in the tooltip

#

Maybe try throwin an APIError instead?