第一个枚举元素(C#)失败的枚举类型的Fluntvalidation失败

发布于 2025-02-09 17:25:33 字数 1037 浏览 1 评论 0原文

我正在使用FulentValidation在存储之前验证用户输入。每当用户选择枚举的第一个元素时,验证都会不断下降。

设想: 我的objecktmodel在其他2种枚举类型中使用了以下定义:

    public enum Koerperschaft_enum
    {
        Privat_Person,
        Vereint,
        Firma,
        Stiftung
    }
    public enum MitgliedStatus_enum
    {
        Mitglied,
        Freispender
    }

我的验证看起来像这样

  public partial class MitgliedValidator : AbstractValidator<MitgliedModel>
    {
        public MitgliedValidator()
        {
            RuleFor(m => m.MitgliedStatus)
                .NotEmpty()
                .NotNull()
                .IsInEnum();

            RuleFor(m => m.Koerperschaft)
                .NotEmpty()
                .NotNull()
                .IsInEnum();
         }

    }

,因为您可以看到成员失败的验证 “在此处输入图像说明”

我的对象将其成员设置为正确的枚举元素 “在此处输入映像说明”

如果输入不是枚举类型的第一个元素,则相同的验证通过。任何人都可以将我带到错误。谢谢

I’m using the fluentvalidation to validate user input before storing them. The Validation keep falling whenever the user selects the first element of the an Enum.

Scenario:
My ObjecktModel uses among other 2 Enum types as defined below:

    public enum Koerperschaft_enum
    {
        Privat_Person,
        Vereint,
        Firma,
        Stiftung
    }
    public enum MitgliedStatus_enum
    {
        Mitglied,
        Freispender
    }

My Validation looks like this

  public partial class MitgliedValidator : AbstractValidator<MitgliedModel>
    {
        public MitgliedValidator()
        {
            RuleFor(m => m.MitgliedStatus)
                .NotEmpty()
                .NotNull()
                .IsInEnum();

            RuleFor(m => m.Koerperschaft)
                .NotEmpty()
                .NotNull()
                .IsInEnum();
         }

    }

As you can see the validation failed by member not empty
enter image description here

My Object however has its members set to the right Enum element
enter image description here

The same validation pass if the input is not the first element of the Enum type. Can anyone please direct me to the mistake. Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

天暗了我发光 2025-02-16 17:25:33

notempty(notempty()验证器检查如果不默认值(<代码> 0未明确指定时枚举):

notempty验证器

确保指定的属性不是null,一个空字符串或
whitespace(或值类型的默认值,例如,int为0)。
当在iEnumerable上使用(例如数组,集合,列表,
等),验证器确保iEnumerable不是空的。

在您的枚举中,您没有指定的明确值,因此默认值为0

将枚举更改为:

public enum Koerperschaft_enum
{
    Privat_Person = 1, // if 1 is not set the default value is 0
    Vereint,
    Firma,
    Stiftung
}
public enum MitgliedStatus_enum
{
    Mitglied = 1, // if 1 is not set the default value is 0
    Freispender
}

或删除nonepty验证器:

public MitgliedValidator()
{
    RuleFor(m => m.MitgliedStatus)
        .NotNull()
        .IsInEnum();

    RuleFor(m => m.Koerperschaft)
        .NotNull()
        .IsInEnum();
 }

如果在模型.notnull()中也无法删除枚举。

NotEmpty() validator checks if value is not default (0 for enums when not specified explicitly):

NotEmpty Validator

Ensures that the specified property is not null, an empty string or
whitespace (or the default value for value types, e.g., 0 for int).
When used on an IEnumerable (such as arrays, collections, lists,
etc.), the validator ensures that the IEnumerable is not empty.

In your enums you don't have explicit values specified so default is 0.

Change your enums to:

public enum Koerperschaft_enum
{
    Privat_Person = 1, // if 1 is not set the default value is 0
    Vereint,
    Firma,
    Stiftung
}
public enum MitgliedStatus_enum
{
    Mitglied = 1, // if 1 is not set the default value is 0
    Freispender
}

Or delete NonEmpty validators:

public MitgliedValidator()
{
    RuleFor(m => m.MitgliedStatus)
        .NotNull()
        .IsInEnum();

    RuleFor(m => m.Koerperschaft)
        .NotNull()
        .IsInEnum();
 }

If enum is not nullable in your model .NotNull() can be deleted as well.

溺深海 2025-02-16 17:25:33

如果您使用的是非空验证器,则应注意这一点;

确保指定的属性不是null,一个空字符串或空格(或值类型的默认值,例如,int为0)。当在iEnumerable上使用(例如数组,集合,列表等)时,验证器可确保iEnumerable不是空的。

因此,您的枚举必须从1开始。如果您不设置它,默认值为0。

public enum Koerperschaft_enum
{
    Privat_Person = 1, 
    Vereint,
    Firma,
    Stiftung
}

public enum MitgliedStatus_enum
{
    Mitglied = 1 , 
    Freispender
}

或者您可以添加一个

public enum Koerperschaft_enum
{
    None,
    Privat_Person, 
    Vereint,
    Firma,
    Stiftung
}

public enum MitgliedStatus_enum
{
    None,
    Mitglied, 
    Freispender
}

if you are using NonEmpty validator, you should pay attention to this;

Ensures that the specified property is not null, an empty string or whitespace (or the default value for value types, e.g., 0 for int). When used on an IEnumerable (such as arrays, collections, lists, etc.), the validator ensures that the IEnumerable is not empty.

Therefore your enums must start at 1. If you don't set it, the default is 0.

public enum Koerperschaft_enum
{
    Privat_Person = 1, 
    Vereint,
    Firma,
    Stiftung
}

public enum MitgliedStatus_enum
{
    Mitglied = 1 , 
    Freispender
}

Or you can add None

public enum Koerperschaft_enum
{
    None,
    Privat_Person, 
    Vereint,
    Firma,
    Stiftung
}

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