gpt4 book ai didi

express - 强制运行 oneOf 验证?

转载 作者:行者123 更新时间:2023-12-04 10:48:55 24 4
gpt4 key购买 nike

我正在尝试验证 PATCH为至少应具有 text 的 Todo 应用程序请求有效负载或 value属性(property)。我正在这样做 updateTodoValidators .这两个属性都是可选的,但至少必须存在一个(因此 oneOf),如果存在,它必须作为 oneOf 之外的验证器有效。表明。

const validate = (validations) => {
return async (req, res, next) => {
await Promise.all(validations.map(validation => validation.run(req)));

const errors = validationResult(req)
if (errors.isEmpty()) {
return next();
}

res
.status(422)
.json({
payload: {
errors: errors.array()
},
message: "Validation error/s",
error: true
})
}
}

const updateTodoValidators = [
oneOf([
body('text').exists().withMessage('not specified'),
body('value').exists().withMessage('not specified')
]),
body('text').optional().trim().notEmpty().withMessage('cannot be empty'),
body('value').optional().isInt({ min: 1 }).withMessage('must be a valid positive number')
]

app.patch('/todos/:id', validate(updateTodoValidators), async (req, res, next) => { /* Route handler implementation */ })

我正在考虑按照 Running validations imperatively [docs] 中的说明强制运行验证。出于代码可读性的目的。我发现如果我有一些涉及 oneOf 的验证我的 validate()抛出 TypeErrorvalidation.run不是函数。波纹管是详细说明错误的堆栈跟踪:
(node:88997) UnhandledPromiseRejectionWarning: TypeError: validation.run is not a function
at Promise.all.validations.map.validation (/Users/alejandro/code/Production/server/validators/validate.js:6:64)
at Array.map (<anonymous>)
at /Users/alejandro/code/Production/server/validators/validate.js:6:35
at Layer.handle [as handle_request] (/Users/alejandro/code/Production/server/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/alejandro/code/Production/server/node_modules/express/lib/router/route.js:137:13)
at loginRequired (/Users/alejandro/code/Production/server/auth/helpers.js:18:24)
at Layer.handle [as handle_request] (/Users/alejandro/code/Production/server/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/alejandro/code/Production/server/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/Users/alejandro/code/Production/server/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/alejandro/code/Production/server/node_modules/express/lib/router/layer.js:95:5)
(node:88997) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:88997) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.


所以,我的问题是:当验证可能涉及一个或多个 oneOf 时,是否有任何方法可以强制运行验证? ?

最佳答案

我最终手动完成了。

做一个 .run()在两个参数检查中。然后验证 _errors两者的属性都是空的。

export const validateTokens = async (req, res, next) => {
// Check to make sure one of the header tokens is set and a UUID
const token1 = await header('token1')
.isUUID()
.run(req);
const token2 = await header('token2')
.isUUID()
.run(req);

// Manual oneOf
if (isEmpty(token1._errors) || isEmpty(token2._errors)) {
return next();
}

// Return error results
res.status(400).send('Must include either token1 or token2');
};

关于express - 强制运行 oneOf 验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59573378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com