[英]Validating decimal precision
Hi I am trying to a calculation and I am using the fluent validation pack to ensure that my rules are adhered to嗨,我正在尝试计算,我正在使用流畅的验证包来确保我的规则得到遵守
The validation is firing and going into the rules ok but stil not working 100%验证正在触发并进入规则,但仍然无法 100% 工作
SageOrderMixB sageOrder = new SageOrderMixB();
var validator = new MixValidator();
var results = validator.Validate(sageOrder);
public class MixValidator : AbstractValidator<SageOrderMixB>
{
public MixValidator()
{
RuleFor(x => x.WeightInTons).LessThanOrEqualTo(28).ScalePrecision(2, 2).WithMessage("Weight cannot be greater than 28 and must be a precison of 2"); ;
}
}
However, If I enter the weight as 29 it is still bypassing and failing my units tests但是,如果我将权重输入为 29,它仍然会绕过并未能通过我的单元测试
Here you will see me traping before i add to my model在这里你会看到我在添加到我的模型之前诱捕
if (results.IsValid == false)
{
sberror.Clear();
foreach (var item in results.Errors)
{
sberror.Append(item.ErrorMessage);
}
MessageBox.Show("Please correct the following " + sberror.ToString(), "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
sageOrdersList.Add(sageOrder);
Here:这里:
.ScalePrecision(2,2)
The first 2
is the number of decimal places.前2
是小数位数。 The second 2
is number of numeric positions.第二个2
是数字位置的数量。
0.45 <-- this is valid
1.45 <-- this is invalid
0.45 <-- this is valid
1.45 <-- this is invalid
eg例如
.ScalePosition(2,4)
27.24 <-- this is valid
Source: https://docs.fluentvalidation.net/en/latest/built-in-validators.html#scaleprecision-validator来源: https : //docs.fluentvalidation.net/en/latest/built-in-validators.html#scaleprecision-validator
For me, it was the position of where I was doing this ensure you have it after where you set you values对我来说,这是我做这件事的位置,确保你在你设定价值之后拥有它
var results = validator.Validate(sageOrder); var 结果 = validator.Validate(sageOrder);
我认为,您需要为此使用正则表达式。
[RegularExpression(@"^\d+(\.\d{2,2})$")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.