1

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:

  1. 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.
  2. 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
    }
  },

};
Iworb
  • 522
  • 7
  • 29
  • could you paste your model over here? – TGW Feb 02 '18 at 12:46
  • @TGW here's my model example (post updated) – Iworb Feb 02 '18 at 12:53
  • seems when you're passing {name:"SomeName",lastName:"SomeLastName",...} the name field itself is not being sent into the insert method, log and check what exactly is going into the insert method. – TGW Feb 02 '18 at 12:55
  • @TGW because I didn't pass it. Did you read topic? I didn't ask where am I wrong, because I knew that, I asked how to retrieve errors not as a sigle string, but object for each field with error. – Iworb Feb 02 '18 at 12:57
  • opss right sorry my bad, for custom validation sails isn't providing anything implicitly, but there is this library which might help you create your own custom messages. [sails-hook-validations](https://www.npmjs.com/package/sails-hook-validation) – TGW Feb 02 '18 at 13:07
  • @TGW first of all - it doesn't work with sails v1 and I bielive it doesn't provide all messages, it just replace default messages with your own. – Iworb Feb 02 '18 at 13:17
  • In that case I fear you'll have to create your custom handler. – TGW Feb 02 '18 at 13:19
  • @TGW how to do that in sails? `Validation` documentation has information only about handler by itself, but there's no any information about custom validation for a whole model object. – Iworb Feb 02 '18 at 13:22
  • Found this related question: https://stackoverflow.com/questions/25824967/how-can-i-validate-a-record-only-without-saving-in-nodejs-sailsjs-waterline. I think you still have to write logic to generate your own messages, but it may at least give you a full list of attributes that fail. – arbuthnott Feb 02 '18 at 15:43
  • I am not sure if you can add the custom messages, but you can use `beforeValidate` hook and write code to pass different messages on different empty parameter key. – khushalbokadey Feb 05 '18 at 12:23

0 Answers0