adonisjs / validator

Schema based validator for AdonisJS
MIT License
115 stars 40 forks source link

Error 500 on validation 2 #159

Closed JonhnyDev closed 1 year ago

JonhnyDev commented 1 year ago

Prerequisites

We do our best to reply to all the issues on time. If you will follow the given guidelines, the turn around time will be faster.

Delete the above section and the instructions in the sections below before submitting

Package version

^5.8.0 core.

Node.js and npm version

v14.20.1 and 6.14.17

Sample Code (to reproduce the issue)

import Logger from '@ioc:Adonis/Core/Logger'
import HttpExceptionHandler from '@ioc:Adonis/Core/HttpExceptionHandler'
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'

export default class ExceptionHandler extends HttpExceptionHandler {
  protected disableStatusPagesInDevelopment = true
  constructor() {
    super(Logger)
  }
  public async handle(error: any, ctx: HttpContextContract) {
    console.log('test')
    if (error.code === 'E_VALIDATION_FAILURE') {
      return ctx.response.status(422).send(error.messages)
    }
    return super.handle(error, ctx)
  }
}
import Route from '@ioc:Adonis/Core/Route'
import { schema } from '@ioc:Adonis/Core/Validator'

Route.post('posts', async ({ request }) => {
  /**
   * Schema definition
   */
  const newPostSchema = schema.create({
    body: schema.string(),
  })

  /**
   * Validate request body against the schema
   */
  const payload = await request.validate({ schema: newPostSchema })
})

BONUS (a sample repo to reproduce the issue)

image

I don't know if this is an error when validating or configuring the expection because even with it configured the validation continues to return as an error 500, making it impossible to create tests with the wrong message body.

    **if (error.code === 'E_VALIDATION_FAILURE') {
      return ctx.response.status(422).send(error.messages)
    }** 

based on documentation, it was for validation errors to return status 422 but keeps returning 500.

158

thetutlage commented 1 year ago

The route you have shared is for path /posts, but you are making an HTTP request to /sendmessage. To avoid further back and forth, I recommend reproducing this issue in a fresh project and share the repo.

RomainLanz commented 1 year ago

Closing since no answer from the issue reporter.