Sails.js 모델에서 설정중인 필드를 제한합니다
문제
그래서 몇 가지 필드가있는 모델이 있습니다 :
// ...
slug: {
type: 'string',
required: true,
alphanumeric: true,
minLength: 3,
maxLength: 16
},
loggedinAt: 'date',
// ...
.
나는 항해 청사진 구조를 사용하고 있으므로 모든 것을 자동으로 매핑합니다.그러나 때로는 내부적으로 내부적 인 loggedinAt
와 같은 필드가 있으며 사용자가 설정할 수 없도록 원하지 않습니다.
loggedinAt
필드로 POST 요청을 만드는 것이 나타납니다.이것을 어떻게 제한 할 수 있습니까?
해결책
정책을 사용 하여이 동작을 제한 할 수 있습니다. API / 정책 / RestrictUserCreate.js :
module.exports = function (req, res, next) {
// Check for the "loggedInAt" field in the request
if (req.param('loggedInAt')) {
return res.badRequest("Nuh uh, you can't set that!");
}
return next();
}
.
또는 특정 필드 (돛 v0.10.x 만 해당)를 무시하려면 blacklist 을 사용하십시오.
module.exports = function (req, res, next) {
// Make sure blacklist object exists, and use existing one if possible,
// since this policy could be chained
req.options.values = req.options.values || {};
req.options.values.blacklist = req.options.values.blacklist || [];
// Add restricted fields to the blacklist
req.options.values.blacklist.push('loggedinAt');
return next();
}
.
다음 config / policies.js :
// Assuming your controller is "UserController"
UserController: {
create: "restrictUserCreate"
}
. 제휴하지 않습니다 StackOverflow