2

如何克隆一个express-validator链并使其成为可选,而不影响原始链?例如,考虑这个例子:

var checkPassword = body('password').trim().isLength({min: 10});
var checkPasswordOptional = checkPassword.optional();

// Creates an account.
router.post('/accounts', checkPassword, createAccount);

// Updates an account. Only provided fields are updated.
router.put('/accounts/:id', checkPasswordOptional, updateAccount);

但有了这个,两者都checkPassword成为checkPasswordOptional可选的。

4

1 回答 1

2

克隆链还不是受支持的功能。

但是,您可以使用工厂函数来生成相同的验证器链,只要您愿意:

const createPasswordCheck = () => body('password').trim().isLength({ min: 10 });

// Uses the default password validation chain    
router.post('/accounts', createPasswordCheck(), createAccount);

// In addition to the default set of password validations,
// updating an account also makes it optional.
router.put('/accounts/:id', createPasswordCheck().optional(), updateAccount);

免责声明:这里是 express-validator 维护者。

于 2018-01-24T03:52:39.510 回答