Sats js自定义验证响应

时间:2018-02-02 12:42:27

标签: node.js sails.js

现在对模型抛出错误json的验证如下:

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."

很糟糕,有一些原因:

  1. 如果有表单验证,则很难定义哪个字段无效。我应该解析细节并查找字段名称,以便为用户标记字段。
  2. 如果错误很少,只会让您知道细节中的第一个错误。
  3. 有没有办法将错误响应作为下面的对象返回?

    details: {
      name: "Message",
      anotherFieldName: "Another message"
    }
    

    UPD:模型示例:

    /**
     * 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
        }
      },
    
    };
    

0 个答案:

没有答案