Javascript 将regex与expressjsonschema一起使用以验证数组的内容
我正在使用ExpressJSONSchema验证JSONHTTPPOST请求的模式。这是模式:Javascript 将regex与expressjsonschema一起使用以验证数组的内容,javascript,regex,node.js,express,Javascript,Regex,Node.js,Express,我正在使用ExpressJSONSchema验证JSONHTTPPOST请求的模式。这是模式: var massiveReportSchema = { type: 'object', properties: { email: { type: 'string', required: true }, author: { type: 'string',
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帐户”
。我想我可以用正则表达式,但我不知道怎么用。这是一个示例请求的主体:
{
"email": "test.doe@mail.com",
"author": "John Doe",
"userId" : [ "100 500",
"101 default",
"102 600"]
}
您可以使用以下方法验证
userId
中的每个元素
var res={
“电子邮件”:“测试”。doe@mail.com",
“作者”:“约翰·多伊”,
“用户ID”:[
"100 500",
“101默认”,
"105900",
"102 600"
]
};
res.userId.forEach(函数(e){
var结果='验证'+e+''|状态:'+/\w++\s\w++/.test(e);
控制台日志(结果);
});代码>您可以使用以下方法验证userId
中的每个元素
var res={
“电子邮件”:“测试”。doe@mail.com",
“作者”:“约翰·多伊”,
“用户ID”:[
"100 500",
“101默认”,
"105900",
"102 600"
]
};
res.userId.forEach(函数(e){
var结果='验证'+e+''|状态:'+/\w++\s\w++/.test(e);
控制台日志(结果);
});代码>很简单
var massiveReportSchema = {
type: 'object',
properties: {
email: {
type: 'string',
required: true
},
author: {
type: 'string',
required: true
},
userId: {
type: 'array',
required: true,
items: {type: 'string', pattern: "\d+\s\d+"}
}
}
}
更多信息:
这很简单
var massiveReportSchema = {
type: 'object',
properties: {
email: {
type: 'string',
required: true
},
author: {
type: 'string',
required: true
},
userId: {
type: 'array',
required: true,
items: {type: 'string', pattern: "\d+\s\d+"}
}
}
}
更多信息:
用户ID中的每个项必须与此正则表达式匹配:\w+\s\w+
;除了字符串中的userId和account之间的空格字符外,还有其他条件吗?userId
中的每个项必须与此正则表达式匹配:\w+\s\w+
;字符串中userId和account之间是否有空格字符以外的条件?谢谢!有没有直接使用ExpressJSONSchema的方法?@JuanDavid可能是的,不确定。从未使用过express jsonschema。这是可能的。见下面的答案。谢谢你的帮助,谢谢!有没有直接使用ExpressJSONSchema的方法?@JuanDavid可能是的,不确定。从未使用过express jsonschema。这是可能的。见下面的答案。谢谢你的帮助。