Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
2 of 4 tasks
dwaynehua opened this issue Oct 11, 2023 · 2 comments
Labels

Comments

@dwaynehua
Copy link

Sorting

  • I'm submitting a ...

    • bug report
    • feature request
    • support request
  • I confirm that I

    • used the search to make sure that a similar issue hasn't already been submit

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"
}

Context (Environment)

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

  • Confirm you were using yarn not npm: [ ]

Detailed Description

Breaking change?

no

@github-actions
Copy link

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.👀

Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant