lukeautry / tsoa

Build OpenAPI-compliant REST APIs using TypeScript and Node
MIT License
3.54k stars 499 forks source link

Complicated error response when validating payload that is of a type that is a union of many types #1494

Closed dwaynehua closed 11 months ago

dwaynehua commented 1 year ago

Sorting

Expected Behavior

Attempt to figure out which type a payload most conforms to amongst a union of many types before doing validation.

OR

as a workaround (for me) export validationService in the hapi.hbs template. I would like to be able to use this service directly to validate some things manually so the error response is more meaningful

Current Behavior

The issue is that some of our apis allow payloads of a of a type that is union of many types. When validation fails, the response is unhelpful because for each incorrect param, it lists out all of the types that it could be amongst the union of all types. In our pre-response handler, i'd like to intercept those validation errors and essentially revalidate the payload against the intended type.

Possible Solution

the ideal solution would be for the validator to see that the incoming payload has type "COW", so validate it against the Animal1 interface. (this is referencing the simplified code example below)

the other solution is to just expose the validationService in hapi.hbs so that i can manually do this check and show a meaningful error message.

Steps to Reproduce

simplified example:

// type definitions
interface Animal1 {
  type: 'COW';
  legs: number;
  spots: number;
}

interface Animal2 {
  type: 'CHICKEN';
  legs: number;
  feathers: number
}

interface Animal3 {
  type: 'DOG';
  legs: number;
  barks: boolean;
}

type Animal = Animal1 | Animal2 | Animal3;

// api definition in a controller
@Post('animal')
updateAnimal ( @Body() body: Animal ) {
 // do stuff
}
curl -X 'POST' \
  'http://localhost:3000/animal' \
  -H 'accept: */*' \
  -H 'Content-Type: application/json' \
  -d '{
  "type": "COW",
  "legs": 0
}'

error response:

{
  "name": "ValidateError",
  "fields": {
    "body": {
      "message": "Could not match the union against any of the items. Issues: [{\"body.spots\":{\"message\":\"'spots' is required\"}},{\"body.type\":{\"message\":\"should be one of the following; ['CHICKEN']\",\"value\":\"COW\"},\"body.feathers\":{\"message\":\"'feathers' is required\"}},{\"body.type\":{\"message\":\"should be one of the following; ['DOG']\",\"value\":\"COW\"},\"body.barks\":{\"message\":\"'barks' is required\"}}]",
      "value": {
        "type": "COW",
        "legs": 0
      }
    }
  },
  "message": "Internal Server Error"
}
      1. 4.

Context (Environment)

Version of the library: 3.14.1 Version of NodeJS: v18.16.0

Detailed Description

Breaking change?

no

github-actions[bot] commented 1 year ago

Hello there dwaynehua 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

github-actions[bot] commented 11 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days