동창 회 애플 릿 개발 노트 14: 애플 릿 클 라 우 드 개발 백 엔 드 데이터 검증 시스템 의 디자인 과 구현
9664 단어 애플 릿 클 라 우 드 개발
통일 검사 루트 입구
function check(data, rules, that) {
let returnData = {};
for (let k in rules) {
let arr = rules[k].split('|');
let desc = '';
//
for (let i = 0; i < arr.length; i++) {
if (arr[i].indexOf('name=') > -1) {
desc = arr[i].replace('name=', '');
break;
}
}
//
let formName = arr[0];
let val = data[formName];
if (val === undefined) val = '';
if (!Array.isArray(val))
val = String(val).trim(); //
returnData[k] = val;
for (let i = 1; i < arr.length; i++) {
let result = '';
let rules = arr[i].split(':');
//
if (rules[0] != 'required' && val == '') continue;
switch (rules[0]) {
case 'required':
result = checkRequired(val, desc);
break;
case 'array':
result = checkArray(val, desc);
break;
case 'date':
result = checkDate(val, desc);
break;
case 'time':
result = checkTime(val, desc);
break;
case 'datetime':
result = checkDatimeTime(val, desc);
break;
case 'min':
result = checkMin(val, Number(rules[1]), desc);
break;
case 'max':
result = checkMax(val, Number(rules[1]), desc);
break;
case 'len':
result = checkLen(val, Number(rules[1]), desc);
break;
case 'in':
result = checkIn(val, rules[1], desc);
break;
case 'email':
result = checkEmail(val, desc);
break;
case 'mobile':
result = checkMobile(val, desc);
break;
case 'int':
result = checkInt(val, desc);
break;
case 'id':
result = checkId(val, desc);
break;
case 'letter':
result = checkLetter(val, desc);
break;
case 'letter_num':
result = checkLetterNum(val, desc);
break;
}
if (result) {
wx.showModal({
title: ' ',
content: result,
showCancel: false,
success(res) {
//
if (that)
that.setData({
[formName + 'Focus']: true
});
}
});
return false;
} else {
if (that) {
//
if (helper.isDefined(that.data[formName + 'Focus'])) {
that.setData({ //TODO delete?
[formName + 'Focus']: false
});
}
}
}
}
}
return returnData;
}
항목 별 데이터 검증 실현
/**
*
* @param {*} value
* @param {*} min
* @param {*} max
*/
function isCheckLen(value, min, max) { //TODO
if (!helper.isDefined(value)) return false;
if (typeof (value) != 'string') return false;
if (value.length < min || value.length > max) return false;
return true;
}
/**
*
* @param {*} value
* @param {*} min
* @param {*} max
*/
function isCheckM(value, min, max) {
if (!helper.isDefined(value)) return false;
if (typeof (value) == 'string' && /^[0-9]+$/.test(value))
value = Number(value);
if (typeof (value) != 'number') return false;
if (value < min || value > max) return false;
return true;
}
function checkMin(value, len, desc) {
if (value.length < len)
return desc + ' ' + len + ' ';
};
function checkMax(value, len, desc) {
if (value.length > len)
return desc + ' ' + len + ' ';
};
function checkLen(value, len, desc) {
if (value.length != len)
return desc + ' ' + len + ' ';
};
function checkMobile(value, desc) {
if (value == '') return '';
if (!/(^1[3|5|8][0-9]{9}$)/.test(value))
return desc + ' ';
}
function checkInt(value, desc) {
if (value == '') return '';
if (!/^[0-9]+$/.test(value))
return desc + ' ';
}
function checkLetter(value, desc) {
if (value == '') return;
if (!/^[A-Za-z]+$/.test(value))
return desc + ' ';
}
function checkLetterNum(value, desc) {
if (value == '') return;
if (!/^\w+$/.test(value))
return desc + ' , ';
}
function checkId(value, desc, min = 1, max = 32) {
if (value == '') return;
if (value.length < min || value.length > max) return false;
if (!/^\w+$/.test(value))
return desc + ' ID ';
}
function isCheckId(value, min = 1, max = 32) {
if (!helper.isDefined(value)) return false;
if (typeof (value) != 'string') return false;
if (value.length < min || value.length > max) return false;
if (!/^\w+$/.test(value))
return false;
return true;
}
//
function checkEmail(value, desc) {
if (value == '') return;
let hint = desc + ' ';
let reg = /^[A-Za-z0-9+]+[A-Za-z0-9\.\_\-+]*@([A-Za-z0-9\-]+\.)+[A-Za-z0-9]+$/;
if (!reg.test(value)) return hint;
}
// , (2008-07-22)
function checkDate(value, desc) {
if (value == '') return;
let hint = ' ' + desc;
if (value.length != 10) return hint;
let r = value.match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
if (r == null) return hint;
let d = new Date(r[1], r[3] - 1, r[4]);
let chk = d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4];
if (!chk)
return hint;
}
// , (13:04:06)
function checkTime(value, desc) {
if (value == '') return;
let hint = desc + ' ';
if (value.length != 8) return hint;
let a = value.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/);
if (a == null) return hint;
if (a[1] > 24 || a[3] > 60 || a[4] > 60) return hint;
}
// , (2008-07-22 13:04:06)
function checkDatimeTime(value, desc) {
if (value == '') return;
let hint = desc + ' ';
if (value.length != 19) return hint;
var reg = /^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2}) (\d{1,2}):(\d{1,2}):(\d{1,2})$/;
var r = value.match(reg);
if (r == null) return hint;
var d = new Date(r[1], r[3] - 1, r[4], r[5], r[6], r[7]);
let chk = d.getFullYear() == r[1] && (d.getMonth() + 1) == r[3] && d.getDate() == r[4] && d.getHours() == r[5] && d.getMinutes() == r[6] && d.getSeconds() == r[7];
if (!chk) return hint;
}
function checkArray(value, desc) {
if (value == '') return;
if (!Array.isArray(value))
return desc + ' ';
}
function checkIn(value, ref, desc) {
if (value == '') return;
let arr = ref.split(',');
if (!arr.includes(value) && !arr.includes(value + ''))
return desc + ' ';
}
/**
*
* @param {*} value
* @param {*} ref 1,2,3
*/
function isCheckIn(value, ref) {
if (!helper.isDefined(value)) return false;
let arr = ref.split(',');
if (!arr.includes(value) && !arr.includes(value + '')) return false; // ,
return true;
}
교우 록 애플 릿 에서 의 사용 시범
//
let rules = {
search: 'string|min:1|max:30|name= ',
sortType: 'string|name= ',
sortVal: 'name= ',
orderBy: 'object|name= ',
whereEx: 'object|name= ',
page: 'required|int|default=1',
size: 'int',
isTotal: 'bool',
oldTotal: 'int',
};
//
let input = this.validateData(rules);
let service = new GroupService();
let result = await service.getMyGroupList(this._userId, input);
https://gitee.com/minzonetech...