Skip to content

readValidatedBody throws an error with inappropriate error message #592

@hecateball

Description

@hecateball

Environment

h3: 1.9.0
Node.js: v20.5.1

Reproduction

Call the readValidatedBody() function with validate that returns false.

details:
Initialize a nitro project(v2.8.1) and add a route file with following contents:

// routes/index.post.ts
export default defineEventHandler(async (event) => {
  const validate = () => false
  await readValidatedBody(event, validate)
})

Run the application and invoke the endpoint with curl -X POST 'http://localhost:3000'

Describe the bug

readValidatedBody(event, validate) throw an error with message "Cannot read properties of undefined (reading 'message')" if the validate returns false.
(Expected message here might be "Validation Failed")

Internal function createValidationError() accepts undefined as parameter, uses it without checking if it is undefined.
https://github.com/unjs/h3/blob/afc4183b513d517bbe83449f1a3dd5291f230e6c/src/utils/internal/validate.ts#L38-L41

validateData actually calls createValidationError() with no parameter when ValidateFunction returns false.
https://github.com/unjs/h3/blob/afc4183b513d517bbe83449f1a3dd5291f230e6c/src/utils/internal/validate.ts#L20-L28

Additional context

No response

Logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions