企业库验证块 - 验证可为空的属性?
我正在尝试对可为空的属性(例如 int)进行验证。
示例
[RangeValidator(0, RangeBoundaryType.Inclusive, 1, RangeBoundaryType.Inclusive)]
int? Age { get; set; }
但是,如果我将 Age
设置为 null 验证会失败,因为它不在范围内,我知道我还需要一个 [ValidatorComposition(CompositionType.Or)]
,但我还应该使用什么?
I am trying to come up with a validation for a nullable property, like int.
Example
[RangeValidator(0, RangeBoundaryType.Inclusive, 1, RangeBoundaryType.Inclusive)]
int? Age { get; set; }
However if I set Age
to null validation fails because it doesn't fall in the range, I know I need an [ValidatorComposition(CompositionType.Or)]
as well, but what else should I use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然可以添加
IgnoreNulls
属性,但在返回验证结果时,它会导致复杂的ValidationResults
。 这是因为验证块隐式地将验证器包装到 OrCompositeValidator 中 - 该属性可以为 null,也可以是指定范围内的整数。当验证失败时,顶级验证结果针对 OrCompositeValidator。 要获取实际的
RangeValidator
验证结果,您现在需要深入了解ValidationResult
对象上的NestedValidationResults
属性。处理验证结果消息似乎需要做很多工作,所以在我看来必须有更好的方法。
这就是我所做的。
IgnoreNullStringLengthValidator
的类,该类继承自StringLengthValidator
(此处您将继承RangeValidator
)。重写
DoValidate
方法并检查空值 - 在这里您可以编写:base.DoValidate(...)
。IgnoreNullStringLengthValidatorAttribute
的属性类,它返回新的IgnoreNullStringLengthValidator
。 在这里,您将创建一个IgnoreNullRangeValidatorAttribute
类。生成的验证结果更符合您的预期,因为它不会隐式嵌套您的验证器。
While the
IgnoreNulls
attribute can be added, it leads to convolutedValidationResults
when validation results are returned. This is because the validation block implicitly wraps the validators into anOrCompositeValidator
- the property can be null OR it can be an integer in the range specified.When the validation fails, the top-level validation result is for the
OrCompositeValidator
. To get the actualRangeValidator
validation result, you now need to drill down into theNestedValidationResults
property on theValidationResult
object.This seems like a lot of work to process validation result messages, so it seemed to me that there had to be a better way.
Here is what I did.
IgnoreNullStringLengthValidator
that inherits from theStringLengthValidator
(here you would inherit theRangeValidator
).Override the
DoValidate
method and check for a null value - here you would write:base.DoValidate(...)
.IgnoreNullStringLengthValidatorAttribute
that returns the newIgnoreNullStringLengthValidator
. Here, you would create anIgnoreNullRangeValidatorAttribute
class.The resulting validation result is much more in line with what you'd expect because it does not nest your validators implicitly.
您可以添加 IgnoreNulls 属性:
You could add the IgnoreNulls attribute:
是的,但是如果
RangeValidator
导致ValidationResult
,那么出于某种原因,我最终会得到两个ValidationResults
...其中一个正确地用于范围验证问题,但另一个神秘的问题是:这很愚蠢,我不想让
IgnoreNulls
验证器导致验证结果! 它真的是用来修改别人的,不是吗? 再加上使用多态性时缺乏真正的验证继承和功能以及许多其他事情,并且 VAB 属性存在许多“小”问题,我发现它无法用于除琐碎之外的任何事情。Yes but then if the
RangeValidator
causes aValidationResult
, then for some reason I end up with twoValidationResults
... one correctly for the range validation problem but then another cryptic one that says:This is silly, I don't EVER want the
IgnoreNulls
validator to cause a validation result! Its really there to modify the others, isn't it? Add this to the lack of real Validation inheritance and functionality when using polymorphism, and so so many other things, and there are so many "small" problems with the VAB attributes that I find it unusable for anything beyond the trivial.