- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试验证 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 */ })
oneOf
的验证我的
validate()
抛出
TypeError
说
validation.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/
这是builtin_owl2-rl.pie第361行 // Part 1 of cls_oo Id: cls_oo_1 c x ------------------
我正在尝试验证 PATCH为至少应具有 text 的 Todo 应用程序请求有效负载或 value属性(property)。我正在这样做 updateTodoValidators .这两个属性都是可选
如何设置 JSON Schema 规则以说明必须设置并且需要其中一个属性? 我尝试了各种方法来解决它,例如: { "id":"#", "required":true, "additi
以下是根据 http://jsonlint.com/ 的有效 JSON 模式和 http://jsonschemalint.com/draft4/# . { "$schema": "http:
我正在研究一个本体,我需要告诉 protege 一个类 x 只能具有值 y 和 z。 我想要这样的东西: Vegetable rdf:type owl:Class; owl:oneOf (car
我有一个示例 protobuf 消息: message testMessage{ oneof oneOfTest{ string test2 = 2; i
我正在使用 OpenAPISpec 文档生成 JavaClient。我使用 swagger-codegen 3.0 来生成代码。 OpenAPISpec 版本是 3.0.1。 下面是我遇到问题的 Op
在Pyparsing中,MatchFirst、Or和oneOf有什么区别 当字符串中有共享字符时 字,字,字 或(['单词','措辞','单词']) MatchFirst(['word', 'word
我想定义 PaymentMethod如下。 swagger.yaml 支持 oneOf 吗? PaymentMethod: oneOf: - $ref: '#/defini
是否可以使用 PropTypes.oneOf强制存在特定类型或字符串文字? 例子: display: PropTypes.oneOf([PropTypes.bool, 'autohide']), 或者
我收到以下规范的错误Data does not match any schemas from 'oneOf': { "info": { "version": "1.0.0", "t
我在 react 中指定“enum”类型的属性时遇到问题。根据此处的文档 React multiple components 以下代码片段应该有效: position : React.PropType
我正在创建一个复杂的 JSON 架构,但在验证“oneOf”构造时遇到问题。 我使用“oneOf”创建了一个非常简单的架构和一个简单的 JSON 文件来演示该问题。 JSON 架构: { "$sc
使用 swagger 在线编辑器遇到以下错误数据与“oneOf”中的任何架构都不匹配 '/tenants/tenant_id/groups/group_id': get: description:
有没有办法为 json 模式(tv4)设置自定义消息,以便在 oneOf 失败时 field ? 我看到大约一年前为自定义消息打开了一个问题 here和 here但是有没有办法让这项工作适用于这样的事
我正在创建一个复杂的 JSON 架构,但在验证“oneOf”构造时遇到问题。 我使用“oneOf”创建了一个非常简单的架构和一个简单的 JSON 文件来演示该问题。 JSON 架构: { "$sc
对于下面的 Protocol Buffer 消息(proto3),我如何确定设置了哪种类型?似乎没有像生成的 C++ 版本那样的“has_reply”方法。 message Event { one
Proto3 支持 oneof 功能,您可以在一条消息中包含多个字段,并且最多同时设置一个字段。 由于一次会设置一个字段,因此原型(prototype)模式中存在重复的字段名称是合理的。问题是原型(p
注意 - 使用可变参数,Phix 在下面提供了一个 super 解决方案。但是,它会在与整数一起使用时给出编译器警告,因为您将它们转换为 id,并且它不适用于例如 float 。 问题——如何编写一个
几个小时后,我仍然无法在 C++ 中使用反射在清晰(刚刚创建的)protobuf 消息中设置 oneof 字段。 我可以获得所需的OneOfDescriptor通过Descriptor的消息。但是当我
我是一名优秀的程序员,十分优秀!