Nowadays validation on model throw error json like this:
code: "E_INVALID_NEW_RECORD"
details: "Missing value for required attribute `name`. Expected a string, but instead, got: undefined"
message: "The server could not fulfill this request (`POST /api/users`) due to a problem with the parameters that were sent. See the `details` for more info. **The following additional tip will not be shown in production**: Tip: Check your client-side code to make sure that the request data it sends matches the expectations of the corresponding attribues in your model. Also check that your client-side code sends data for every required attribute."
It's suck and there's some reasons:
- If there a form validation it hard to define which field is invalid. I should parse details and look for a field name just to mark field for an user.
- If there few errors it let you know only about first error in the details.
Is there a way to return error response as an object like below?
details: {
name: "Message",
anotherFieldName: "Another message"
}
UPD: model example:
/**
* User.js
*
* @description :: A model definition. Represents a database table/collection/etc.
* @docs :: https://sailsjs.com/docs/concepts/models-and-orm/models
*/
module.exports = {
attributes: {
name: {
type: 'string',
required: true,
minLength: 10
},
lastName: {
type: 'string',
required: true,
minLength: 10
},
username: {
type: 'string',
required: true
},
anotherFieldName: {
type: 'string',
required: true
}
},
};