我正在尝试验证一些相互依赖的温度值,但我也想接受它们的空值。例如,我有以下代码:
VadliationRules.customRule(
"minT",
(value, obj: Object, min) => {
let converted = parseFloat(value)
return converted >= min && converted < obj.MaxTemperature
},
'');
ValidationRules.customRule(
"maxT",
(value, obj: Object, max) => {
let converted = parseFloat(value)
return converted <= max && converted > obj.MinTemperature
},
'');
ValidationRules
.ensure((p: Object) => p.MinTemperature)
.satisfiesRule('minT', -15.0)
.withMessageKey('min_temp')
.ensure((p: Object) => p.MaxTemperature)
.satisfiesRule('maxT', 50.0)
.withMessageKey('max_temp')
.on(Object)
let MinTemperature : number;
let MaxTemperature : number;
我尝试了以下选项,但它们似乎不起作用(我想是因为依赖于其他属性(
// Example 1
VadliationRules.customRule(
"minT",
(value, obj: Object, min) => {
let converted = parseFloat(value)
return converted >= min && converted < obj.MaxTemperature || converted == undefined || obj.MaxTemperature == null
},
'');
// Example 2
VadliationRules.customRule(
"minT",
(value, obj: Object, min) => {
let converted = parseFloat(value)
return converted >= min && converted < obj.MaxTemperature || converted == undefined || converted.toString() == ""
},
'');
如何在满足对其他属性的依赖性的同时接受两个字段的空值?
尝试以下
return converted >= min && (converted < obj.MaxTemperature || obj.MaxTemperature == null)
- 当不使用括号时,当obj.MaxTemperature==null时,无论第一次检查如何,它都会通过
- 仅供参考,obj.MaxTemperature==null还检查未定义的部分,因此可以省略该部分(请参阅http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3(