Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Fluentvalidation on Enum Type fail by first Enum element (C#)

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading