我有如下企業的資料:
{
"business": {
"type": [
"LLC",
"Corporation"
],
"LLC": {
"status": "active",
"profits": 1000000,
"period": "yearly"
},
"corporation": {},
"partnership": {}
}
}
如何驗證“corporation”物件,所以如果“type”陣列包含字串“Corporation”,“corporation”物件不能為空?
我試過使用 validate.js 來檢查它是否為空,它可以作業,但我不能專門將字串放在型別陣列中來驗證它。
const validate = require('validate.js');
if (validate.isEmpty(business.LLC)) {
return wrapper.error('fail', 'Object is empty');
}
非常感謝您的幫助????
uj5u.com熱心網友回復:
我想通了,也沒有意識到,謝謝你的回答!
所以我需要做的是先檢查陣列,然后檢查下面的物件。
if (payload.business.type.includes('Corporation')) {
if (validate.isEmpty(payload.business.corporation)) {
return wrapper.error('fail', 'Object is empty');
}
}
uj5u.com熱心網友回復:
let object = {
"business": {
"type": [
"LLC",
"Corporation"
],
"LLC": {
"status": "active",
"profits": 1000000,
"period": "yearly"
},
"corporation": {},
"partnership": {}
}
}
使用包含檢查陣列內的元素,
object.business.type.includes("Corporation")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/445424.html
標籤:javascript 验证
上一篇:避免decimal.TryParse拋出OverflowException
下一篇:驗證陳述句難以閱讀
