谷神后端数据校验:基础

基础数据校验:map
/**
 * checkMap
 * 基础数据校验:map。
 *
 * @param $map:map:数据。
 * @param $options:map:校验规则。
 * @param $rowIndex:number:行索引。
 * 
 * @return $map
 *
 */
#function checkMap($map, $options, $rowIndex)
	#if ($vs.util.isMap($map))
		#if ($vs.util.isString($options))
			#set($options = $vs.util.fromJson($options))
		#end
		#if (!$vs.util.isMap($options))
			return null
		#end
		#set($rowable = false)
		#if ($vs.util.isNumeric($rowIndex))
			#set($rowable = true)
		#end
		#set($result = $vs.util.newMap())
		#set($optionsMap = $vs.util.newMap())
		#set($result.optionsMap = $optionsMap)
		#set($errors = $vs.util.newList())
		#set($result.errors = $errors)
		#foreach ($key in $vs.util.getMapKeys($options))
			#set($keyMap = $vs.util.newMap())
			$optionsMap.put($key, $keyMap)
			#set($value = $map.get($key))
			#set($option = $options.get($key))
			#set($name = $option.name)
			#if ($option.required)
				#if ($vs.util.isNull($value))
					$keyMap.put('required', false)
					#set($msg = '')
					#if ($rowable)
						#set($msg = $msg + '第' + $rowIndex + '行')
					#end
					#set($msg = $msg + $name + '不能为空!')
					$errors.add($msg)
				#else
					$keyMap.put('required', true)
				#end
			#end
			#if ($vs.util.isNotNull($value) and $option.containsKey('maxLength'))
				#set($maxLength = $option.get('maxLength'))
				#if ($vs.util.strLen($value) > $maxLength)
					$keyMap.put('maxLength', false)
					#set($msg = '')
					#if ($rowable)
						#set($msg = $msg + '第' + $rowIndex + '行')
					#end
					#set($msg = $msg + $name + '最大长度为' + $maxLength + '!')
					$errors.add($msg)
				#else
					$keyMap.put('maxLength', true)
				#end
			#end
			#if ($vs.util.isNotNull($value) and $option.containsKey('isNumeric'))
				#if (!$vs.util.isNumeric($value))
					$keyMap.put('isNumeric', false)
					#set($msg = '')
					#if ($rowable)
						#set($msg = $msg + '第' + $rowIndex + '行')
					#end
					#set($msg = $msg + $name + '应为数字!')
					$errors.add($msg)
				#else
					$keyMap.put('isNumeric', true)
				#end
			#end
			#if ($vs.util.isNotNull($value) and $vs.util.isNumeric($value) and $option.containsKey('decimalPlace'))
				#set($decimalPlace = $option.get('decimalPlace'))
				#if ($value ne $vs.util.round($value, $decimalPlace))
					$keyMap.put('decimalPlace', false)
					#set($msg = '')
					#if ($rowable)
						#set($msg = $msg + '第' + $rowIndex + '行')
					#end
					#set($msg = $msg + $name + '最多保留' + $decimalPlace + '位小数!')
					$errors.add($msg)
				#else
					$keyMap.put('decimalPlace', true)
				#end
			#end
			#if ($vs.util.isNotNull($value) and $vs.util.isNumeric($value) and $option.containsKey('min'))
				#set($min = $option.get('min'))
				#if ($vs.util.numberCompare($value, $min) eq -1)
					$keyMap.put('min', false)
					#set($msg = '')
					#if ($rowable)
						#set($msg = $msg + '第' + $rowIndex + '行')
					#end
					#set($msg = $msg + $name + '最小值为' + $min + '!')
					$errors.add($msg)
				#else
					$keyMap.put('min', true)
				#end
			#end
			#if ($vs.util.isNotNull($value) and $vs.util.isNumeric($value) and $option.containsKey('max'))
				#set($max = $option.get('max'))
				#if ($vs.util.numberCompare($value, $max) eq 1)
					$keyMap.put('max', false)
					#set($msg = '')
					#if ($rowable)
						#set($msg = $msg + '第' + $rowIndex + '行')
					#end
					#set($msg = $msg + $name + '最大值为' + $max + '!')
					$errors.add($msg)
				#else
					$keyMap.put('max', true)
				#end
			#end
			#if ($vs.util.isNotNull($value) and $option.containsKey('pattern'))
				#set($pattern = $option.get('pattern'))
				#set($match = $vs.regexp.match($value, $pattern))
				#if (!$match.matches())
					$keyMap.put('pattern', false)
					#set($msg = '')
					#if ($rowable)
						#set($msg = $msg + '第' + $rowIndex + '行')
					#end
					#set($msg = $msg + $name + '格式不正确!')
					$errors.add($msg)
				#else
					$keyMap.put('pattern', true)
				#end
			#end
			#if ($option.containsKey('codes'))
				#set($strCodeType = $option.get('codes'))
				#if ($vs.util.isNotNull($value))
					#if (@checkEnumCodesValue($value, $strCodeType))
						$keyMap.put('codes', false)
						#set($msg = '')
						#if ($rowable)
							#set($msg = $msg + '第' + $rowIndex + '行')
						#end
						#set($msg = $msg + $name + '数据字典不正确!')
						$errors.add($msg)
					#else
						$keyMap.put('codes', true)
					#end
				#end
			#end
		#end
		return $result
	#end
	return null
#end
公共引用
/**
 * checkEnumCodesValue
 * 谷神数据字典枚举校验:value。
 *
 * @param:$value:string:名称。
 * @param:$strCodeType:string:数据字典类型。
 *
 * @return boolean
 *
 */
#function checkEnumCodesValue($value, $strCodeType)
	#if ($vs.util.isNotNull($value))
		#set($list = $vs.cache.getCodes($strCodeType, false))
		#set($result = true)
		#foreach ($row in $list)
			#if ($row.value eq $value)
				#set($result = false)
				#break
			#end
		#end
		return $result
	#end
	return null
#end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值