如何在loopback远程方法中验证属性?

时间:2018-01-15 09:40:15

标签: express loopback

最近我开始学习Loopback。我试图添加必需,最小和最大。

这是我在带有参数的json文件中的远程方法:

{
  "name": "registration",
  "methods": {
    "registrationIn": {
      "accepts": [
        {
          "arg": "firstname",
          "type": "string",
          "min": 1,
          "max": 25,       // same for here 
          "required": true,
          "description": "Firstname of the person.",
          "http": {
            "source": "form"
          }
        }
        {
          "arg": "mobile",
          "type": "number",
          "min": 1,   // since required it correct but not checking i removed required
          "max": 10, // not working 
          "required": true,  // working
          "description": "",
          "http": {
            "source": "form"
          }
        }
      ],
      "returns": [],
      "description": "This method used to registration.",
      "http": [{
        "path": "/registrationIn",
        "verb": "post"
      }]
    }
}

必需的属性工作正常,但最小和最大不起作用。 任何人都可以指导我到哪里做错了吗?

谢谢

1 个答案:

答案 0 :(得分:0)

请参阅此链接validation

您可以在模型上添加验证。

    module.exports = function(user) {
      user.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}});
 }