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

readValidatedBody throws an error with inappropriate error message #592

Closed
hecateball opened this issue Dec 5, 2023 · 1 comment
Closed

Comments

@hecateball
Copy link

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.

function createValidationError(validateError?: any) {
throw createError({
status: 400,
message: validateError.message || "Validation Failed",

validateData actually calls createValidationError() with no parameter when ValidateFunction returns false.

export async function validateData<T>(
data: unknown,
fn: ValidateFunction<T>,
): Promise<T> {
try {
const res = await fn(data);
if (res === false) {
throw createValidationError();
}

Additional context

No response

Logs

No response

@pi0
Copy link
Member

pi0 commented Dec 7, 2023

Should be fixed by #594 in next release

@pi0 pi0 closed this as completed Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants