valid
function valid(obj, options){
if (Array.isArray(obj)) {
return obj.every(item => {
return valid(item, options)
})
} else {
options = expandField(options, 'fieldId')
for (option of options) {
let {fieldId, fieldName, maxLength} = option
let value = obj[fieldId]
let length = value?.length ?? 0
if (value && maxLength && length > maxLength) {
gUtil.error(`${fieldName}最大长度为${maxLength}个字符!`)
return false
}
if (!isNaN(parseFloat(value))) {
let numberValue = parseFloat(value)
if (min && numberValue < min) {
gUtil.error(`${fieldName}最小值为${min}!`)
return false
}
if (max && numberValue > max) {
gUtil.error(`${fieldName}最大值为${max}!`)
return false
}
}
}
return true
}
}
if (!valid(content, [
{
fieldId: 'fieldId',
fieldName: 'fieldName',
maxLength: 0,
min: 0,
max: 0,
}
])) {
return
}
expandField
function expandField (obj, field) {
let result = []
if (Array.isArray(obj)) {
obj.forEach(item => {
result.push(expandField (item, field))
})
} else {
if (obj[field] && Array.isArray(obj[field])) {
obj[field].forEach(fieldItem => {
let objItem = {...obj}
objItem[field] = fieldItem
result.push(objItem)
})
} else {
result.push(obj)
}
}
return result
}