Route validation failed


#1

I am having a strange problem with a route validator.
I am always getting this message when I try to make a request to my endpoint:
Validation failed. Make sure you have filled all fields correctly

cannot understand why I am not getting the error messages for each field.

this is my validator request file

'use strict'

class LocationRequest {
  get rules () {
    return {
      name: 'required|string',
      lat: 'required|string',
      lng: 'required|string',
      location: 'required|string|min:10',
      details: 'required|string|min:10',
      city_id: 'required:integer',
      category_id: 'required|integer'
    }
  }
}

module.exports = LocationRequest

any thoughts?
I’ve done this before for other route and there it showed me the validation errors for each field, but now I am getting this message and I don’t know why.


#2

The exceptions raised by the route Validator looks for the Accept header before forming the response type for a given request.

If your request doesn’t accept application/json type, the validator will set the error messages in the session.

If you are creating the JSON API and want messages back in the JSON format, make sure to set the Accept = application/json header from the client making the request.

If your server is always going to deal with JSON, then you simply set the header yourself by creating a global middleware.

  1. Create a middleware SpoofAccept with following content.
class SpoofAccept {
  async handle ({ request }, next) {
    request.request.headers['accept'] = 'application/json'
    await next()
  }
}

module.exports = SpoofAccept
  1. Set it as the first middleware in the global middleware list.

#3

Thank you @virk for the explanation.
I do create a json api, but I did not thought to set the Accept header to application/json

Will try to set the header when I get back home and will let you know how it went.


#4

Works like a charm :slight_smile:
I’ve also check my first request where the validation succeeded and indeed there I have the Accept header set to applciation/json

thank you again @virk