How can I hide the 'id' attribute in Loopback explorer?

≡放荡痞女 提交于 2019-12-07 19:18:14

问题


Is it possible to hide the id attribute in a method in swagger-ui generated by explorer in Strongloop Loopback? I don want the user to create a new resource and send the id attribute. I know that if the user send the id it can be ignored but I want to hide it in the explorer.


回答1:


In order to hide the 'id' attribute, you need declare this field as hidden.

In YOUR_MODEL.json file:

{
  "name": "YOUR_MODEL",
  .
  .
  .
  "properties": {
     // your custom properties
  },
  "hidden": ["id"], // this attribute specifies which attributes need to be hidden
  .
  .
  .
}

Be aware when a property declared as hidden:

  1. It's not exposed to the user
  2. Although hidden, if user sends provides a value with this property, the property won't be ignored by default, and will be handled with provided values. hence, need to be ignored manually.

For instance, if we have 'User' model as follows:

{
  "name": "User",
  .
  .
  .
  "properties": {
     "id": "string",
     "name": "string",
     "password": "string",

  },
  "hidden": ["id", "password"],
  .
  .
}

/api/User GET request will provide list of Users with only 'name' attribute

BUT, /api/User POST with body:

{
  "user" : "USER",
  "password": "PASS",
  "id" : "USER_PROVIDED_ID"
}

the user provided in the body will be saved with the values in it.



来源:https://stackoverflow.com/questions/35039816/how-can-i-hide-the-id-attribute-in-loopback-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!