企业库验证块 - 验证可为空的属性?

发布于 2024-07-12 07:24:05 字数 309 浏览 7 评论 0原文

我正在尝试对可为空的属性(例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷了相思 2024-07-19 07:24:05

虽然可以添加 IgnoreNulls 属性,但在返回验证结果时,它会导致复杂的 ValidationResults。 这是因为验证块隐式地将验证器包装到 OrCompositeValidator 中 - 该属性可以为 null,也可以是指定范围内的整数。

当验证失败时,顶级验证结果针对 OrCompositeValidator。 要获取实际的 RangeValidator 验证结果,您现在需要深入了解 ValidationResult 对象上的 NestedValidationResults 属性。

处理验证结果消息似乎需要做很多工作,所以在我看来必须有更好的方法。

这就是我所做的。

  1. 我创建了一个名为 IgnoreNullStringLengthValidator 的类,该类继承自 StringLengthValidator(此处您将继承 RangeValidator)。
  2. 创建支持基本构造函数所需的所有构造函数。
  3. 重写DoValidate方法并检查空值 - 在这里您可以编写:

    if (!objectToValidate.HasValue) return; 
      
  4. 确保您的下一行代码调用 base.DoValidate(...)
  5. 创建了一个名为 IgnoreNullStringLengthValidatorAttribute 的属性类,它返回新的 IgnoreNullStringLengthValidator。 在这里,您将创建一个 IgnoreNullRangeValidatorAttribute 类。

生成的验证结果更符合您的预期,因为它不会隐式嵌套您的验证器。

While the IgnoreNulls attribute can be added, it leads to convoluted ValidationResults when validation results are returned. This is because the validation block implicitly wraps the validators into an OrCompositeValidator - 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 actual RangeValidator validation result, you now need to drill down into the NestedValidationResults property on the ValidationResult 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.

  1. I created a class called IgnoreNullStringLengthValidator that inherits from the StringLengthValidator (here you would inherit the RangeValidator).
  2. Create all the constructors needed to support the base constructors.
  3. Override the DoValidate method and check for a null value - here you would write:

    if (!objectToValidate.HasValue) return;
    
  4. Make sure your next line of code calls base.DoValidate(...).
  5. Created an attribute class called IgnoreNullStringLengthValidatorAttribute that returns the new IgnoreNullStringLengthValidator. Here, you would create an IgnoreNullRangeValidatorAttribute class.

The resulting validation result is much more in line with what you'd expect because it does not nest your validators implicitly.

黄昏下泛黄的笔记 2024-07-19 07:24:05

您可以添加 IgnoreNulls 属性:

[IgnoreNulls()]
[RangeValidator(0, RangeBoundaryType.Inclusive, 1, RangeBoundaryType.Inclusive)]
int? Age { get; set; }

You could add the IgnoreNulls attribute:

[IgnoreNulls()]
[RangeValidator(0, RangeBoundaryType.Inclusive, 1, RangeBoundaryType.Inclusive)]
int? Age { get; set; }
绅士风度i 2024-07-19 07:24:05

是的,但是如果 RangeValidator 导致 ValidationResult,那么出于某种原因,我最终会得到两个 ValidationResults...其中一个正确地用于范围验证问题,但另一个神秘的问题是:

该值不为空,并且未通过密钥 Age 的所有验证规则。

这很愚蠢,我不想让 IgnoreNulls 验证器导致验证结果! 它真的是用来修改别人的,不是吗? 再加上使用多态性时缺乏真正的验证继承和功能以及许多其他事情,并且 VAB 属性存在许多“小”问题,我发现它无法用于除琐碎之外的任何事情。

Yes but then if the RangeValidator causes a ValidationResult, then for some reason I end up with two ValidationResults... one correctly for the range validation problem but then another cryptic one that says:

The value is not null and failed all its validation rules for key Age.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文