使用节点 js 读取 JSON 中的多个属性
Reading Multiple Attribute in JSON using node js
我能够验证单个 JSON 对象,但我想验证一个 JSON 对象数组,如下所示,并控制无效的 Pincode 城市名称:
var RuleEngine = require("node-rules");
var R = new RuleEngine();
var fact =
[{
"name": "Person",
"website": "Udemy",
"transactionTotal": 400,
"cardType": "Credit Card",
"statuscode": 200,
"details": {
"city": "Kirochnaya ",
"pincode": 191015
}
},
{
"name": "Person2",
"website": "Udemy",
"transactionTotal": 900,
"cardType": "Credit Card",
"statuscode": 200,
"details": {
"city": "Kirochnaya ",
"pincode": 191015
}
},
{
"name": "Person3",
"website": "Udemy",
"transactionTotal": 800,
"cardType": "Credit Card",
"statuscode": 200,
"details": {
"city": "Saint Petersburg",
"pincode": 191123
}
}];
var rule = {
"condition": function (R) {
console.log(this);
R.when(this.details.city != "Kirochnaya");
},
"consequence": function (R) {
this.result = false;
this.reason = " Failed validation bcos city name is not matched";
R.stop();
}
};
R.register(rule);
R.execute(fact, function (data) {
if (data.result) {
console.log("Valid statuscode");
} else {
console.log("Blocked Reason:" + data.reason);
}
});
对于上面的代码,预期输出是:
验证失败 bcos 城市名称不匹配:圣彼得堡 191123
您可以遍历数组并为每个事实执行规则:
var rule = {
"condition": function (R) {
console.log(this);
R.when(this.details.city != "Kirochnaya");
},
"consequence": function (R) {
this.result = false;
this.reason = " Failed validation bcos city name is not matched: " + this.details.city + " " + this.details.pincode;
R.stop();
}
};
R.register(rule);
fact.forEach(check => {
R.execute(check, function (data) {
if (data.result) {
console.log("Valid statuscode");
} else {
console.log("Blocked Reason:" + data.reason);
}
});
});
我能够验证单个 JSON 对象,但我想验证一个 JSON 对象数组,如下所示,并控制无效的 Pincode 城市名称:
var RuleEngine = require("node-rules");
var R = new RuleEngine();
var fact =
[{
"name": "Person",
"website": "Udemy",
"transactionTotal": 400,
"cardType": "Credit Card",
"statuscode": 200,
"details": {
"city": "Kirochnaya ",
"pincode": 191015
}
},
{
"name": "Person2",
"website": "Udemy",
"transactionTotal": 900,
"cardType": "Credit Card",
"statuscode": 200,
"details": {
"city": "Kirochnaya ",
"pincode": 191015
}
},
{
"name": "Person3",
"website": "Udemy",
"transactionTotal": 800,
"cardType": "Credit Card",
"statuscode": 200,
"details": {
"city": "Saint Petersburg",
"pincode": 191123
}
}];
var rule = {
"condition": function (R) {
console.log(this);
R.when(this.details.city != "Kirochnaya");
},
"consequence": function (R) {
this.result = false;
this.reason = " Failed validation bcos city name is not matched";
R.stop();
}
};
R.register(rule);
R.execute(fact, function (data) {
if (data.result) {
console.log("Valid statuscode");
} else {
console.log("Blocked Reason:" + data.reason);
}
});
对于上面的代码,预期输出是:
验证失败 bcos 城市名称不匹配:圣彼得堡 191123
您可以遍历数组并为每个事实执行规则:
var rule = {
"condition": function (R) {
console.log(this);
R.when(this.details.city != "Kirochnaya");
},
"consequence": function (R) {
this.result = false;
this.reason = " Failed validation bcos city name is not matched: " + this.details.city + " " + this.details.pincode;
R.stop();
}
};
R.register(rule);
fact.forEach(check => {
R.execute(check, function (data) {
if (data.result) {
console.log("Valid statuscode");
} else {
console.log("Blocked Reason:" + data.reason);
}
});
});