javascript - 使用正则表达式和express-jsonschema来验证数组的内容

标签 javascript regex node.js express

我正在使用 express-jsonschema 来验证 JSON HTTP Post 请求的架构。这是架构:

var massiveReportSchema = {
    type: 'object',
    properties: {
        email: {
            type: 'string',
            required: true
        },
        author: {
            type: 'string',
            required: true
        },
        userId: {
            type: 'array',
            required: true,
            items: {type: 'string'}
        }
    }
}

我想验证 userId 中的每个元素是否具有 “userId account” 格式。我想我可以使用正则表达式,但我不知道如何使用。这是示例请求的正文:

{
    "email": "test.doe@mail.com",
    "author": "John Doe",
    "userId" : [    "100 500", 
                    "101 default", 
                    "102 600"]

}

最佳答案

您可以使用以下方式验证 userId 中的每个元素...

var res = {
    "email": "test.doe@mail.com",
    "author": "John Doe",
    "userId" : [
      "100 500",
      "101 default",
      "105900",
      "102 600"
    ]
};
res.userId.forEach(function(e) {
  var result = 'Validating "' + e + '" | Status : ' + /\w+\s\w+/.test(e);
  console.log(result);
});

关于javascript - 使用正则表达式和express-jsonschema来验证数组的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42681888/

相关文章:

javascript - 我对 jQuery 的 next 扩展的表现出乎意料

javascript - 通过 document.createElement 创建一个带有选项的下拉列表?

javascript - 如何通过互联网向用户公开交互式 neo4j 可视化(安全地)?

c++ - 如何在qt中使用QRegularExpression匹配字符串开头的空格?

node.js - Node JS。无法读取未定义的属性

node.js - Vscode API : read clipboard text content

javascript - 无法从 angularjs 中的 json 文件获取数据?

javascript - 使用词边界求值的正则表达式

javascript - Jest + super 测试|开 Jest 检测打开的句柄

python - 正则表达式不以相同的方式拆分文本