2017-03-08 8 views
0

express-jsonschemaを使用してJSON HTTP Postリクエストのスキーマを検証しています。express-jsonschemaでregexを使用して配列の内容を確認する

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"を持っていることを検証する:これはスキーマです。私は正規表現を使用することができると思うが、私はどのようにわからない。これは、例えばリクエストのボディです:あなたは、次の方法を使用してuserIdの各要素を検証することができ

{ 
    "email": "[email protected]", 
    "author": "John Doe", 
    "userId" : [ "100 500", 
        "101 default", 
        "102 600"] 

} 
+0

userId' 'の各項目は、この正規表現に一致しなければならない:+ \ S \ W + W' '\と、文字列のuserIdとaccountの間のスペース文字以外の条件はありますか? – MohaMad

答えて

1

...

var res = { 
 
    "email": "[email protected]", 
 
    "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); 
 
});

+0

ありがとうございます! express-jsonschemaを使って直接行う方法はありますか? –

+0

@JuanDavidかもしれません。 express-jsonschemaは使わなかった。 – m87

+0

可能です。以下の回答を参照してください。ご協力いただきありがとうございます。 –

関連する問題