我正在使用 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/