Type-graphql: Custom validation error

Created on 18 Apr 2019  路  4Comments  路  Source: MichalLytek/type-graphql

Currently when I add validators to the input type using the class-validator library and there is validation error, I get the following error message: Argument Validation Error. Which is ok, but we can provide custom message to each validator:

@IsNotEmpty({ message: "Title can't be empty" })

What about using this message to give use more info about what caused error?

Question Solved

Most helpful comment

@19majkel94 ah right it's deep in the errors structure that's why I've assumed that it's not there. I didn't dig deep enough. Just to get validation error I have to dig as deep as this one:
errors[0].extensions.exception.validationErrors[0].constrains.isNotEmpty
Before I've just though about displaying the first validation error that occurred in the form of snackbar. But having all the validation errors I can actually display them next to the form fields which is nice.
Thanks!

All 4 comments

ArgumentValidationError has a validationErrors propety that has all info that class-validator returns:
https://github.com/19majkel94/type-graphql/blob/68689fd848af0348b70171142da9f6bb35cb8512/src/errors/ArgumentValidationError.ts#L4

And it's returned to the client:
https://github.com/19majkel94/type-graphql/issues/258#issuecomment-466637130

@19majkel94 ah right it's deep in the errors structure that's why I've assumed that it's not there. I didn't dig deep enough. Just to get validation error I have to dig as deep as this one:
errors[0].extensions.exception.validationErrors[0].constrains.isNotEmpty
Before I've just though about displaying the first validation error that occurred in the form of snackbar. But having all the validation errors I can actually display them next to the form fields which is nice.
Thanks!

I have validations on my Args and none of the messages are passing through. :(

{
  "errors": [
    {
      "message": "Argument Validation Error",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "signup"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "exception": {
          "validationErrors": {},
          "stacktrace": [
            "Error: Argument Validation Error",
...
        }
      }
    }
  ],
  "data": null
}

@reggi show your code

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maplesteve picture maplesteve  路  3Comments

robertchung97 picture robertchung97  路  3Comments

winuxue picture winuxue  路  4Comments

MichalLytek picture MichalLytek  路  3Comments

avkonst picture avkonst  路  3Comments