tsoa icon indicating copy to clipboard operation
tsoa copied to clipboard

`fields` stringify'd in some Validation responses

Open cnuss opened this issue 3 years ago • 8 comments

Quick question...

(cc: @WoH as the original author)

I was trying out some of the Validation work, and I was wondering...

In the following examples, is there a reason it's JSON.stringify'ing the field errors to the message instead of setting it as a property?

Examples:

https://github.com/lukeautry/tsoa/blob/641f12c32440b1b146c59ba7fc74f7f54cf1d383/packages/runtime/src/routeGeneration/templateHelpers.ts#L483 https://github.com/lukeautry/tsoa/blob/641f12c32440b1b146c59ba7fc74f7f54cf1d383/packages/runtime/src/routeGeneration/templateHelpers.ts#L515 https://github.com/lukeautry/tsoa/blob/641f12c32440b1b146c59ba7fc74f7f54cf1d383/packages/runtime/src/routeGeneration/templateHelpers.ts#L559 https://github.com/lukeautry/tsoa/blob/641f12c32440b1b146c59ba7fc74f7f54cf1d383/packages/runtime/src/routeGeneration/templateHelpers.ts#L548

Then when I catch the ValidateError, err.fields has the following structure

      "login": {
        "message": "Could not match the union against any of the items. Issues: [{\"login.provider\":{\"message\":\"'provider' is required\"},\"login.email\":{\"message\":\"'email' is required\"}},{\"login.provider\":{\"message\":\"'provider' is required\"},\"login.email\":{\"message\":\"'email' is required\"}}]",
        "value": {}
      }

The stringify'd JSON isn't incredibly handy, what are your thoughts on a updated structure being:

      "login": {
        "message": "Could not match the union against any of the items.",
        "fields": [
                         {
                            "login.provider":{
                               "message":"'provider' is required"
                            },
                            "login.email":{
                               "message":"'email' is required"
                            }
                         }
                      ]
        "value": {}
      }

cnuss avatar May 19 '21 20:05 cnuss

Also FYI I'm happy to make a PR if you think this is a good idea.

cnuss avatar May 19 '21 21:05 cnuss

The reason is that the errors can affect the same property.

WoH avatar Jun 19 '21 10:06 WoH

We can make a breaking change here, but I'd have to see a format that works for all the cases involved.

WoH avatar Jun 19 '21 10:06 WoH

I'll take a stab at making a PR so we can make the determination if it is a breaking change

thanks @WoH

cnuss avatar Jun 19 '21 18:06 cnuss

any update about this ?

thomasskk avatar Oct 04 '21 02:10 thomasskk

I was looking into this as well. Simply stringifying the validator error makes the message incomprehensible. @cnuss have you worked on this issue at all?

buffolander avatar May 17 '22 17:05 buffolander

Sorry no I never got around to it l!

cnuss avatar May 17 '22 17:05 cnuss

@WoH does repeating the the same key in the values array breaks something? In case of #1170 the error would be:

[
  {
    "requestBody.$0.email": {
      "message": "maxLength 10",
      "value": "PCPNPsdfsdfsdfsdfsfgsdgf"
    }
  },
  {
    "requestBody.$0.email": {
      "message": "should be one of the following; [null]",
      "value": "PCPNPsdfsdfsdfsdfsfgsdgf"
    }
  }
]

but would still be more informative.

igarioshka avatar May 26 '22 13:05 igarioshka