Home > Software engineering >  Fluentvalidation on Enum Type fail by first Enum element (C#)
Fluentvalidation on Enum Type fail by first Enum element (C#)

Time:06-22

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

CodePudding user response:

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.

CodePudding user response:

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
}
  • Related