Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a ValidationAttribute to Validate Model Property Against SmartEnum Names #446

Closed
sdepouw opened this issue Oct 16, 2023 · 0 comments
Closed
Assignees

Comments

@sdepouw
Copy link
Collaborator

sdepouw commented Oct 16, 2023

In this scenario, an incoming data model may contain a SmartEnum name. Ideally, one could easily validate it against a particular SmartEnum to make sure the value is among the enum's names.

public sealed class Vehicle : SmartEnum<Vehicle>
{
    public static readonly Vehicle Car = new Vehicle(nameof(Car), 1);
    public static readonly Vehicle Boat = new Vehicle(nameof(Boat), 2);
    public static readonly Vehicle Plane = new Vehicle(nameof(Plane), 3);

    private VehcileTypeEnum(string name, int value) : base(name, value) { }
}

public class ExampleModel
{
    [Required]
    [SmartEnumName(typeof(Vehicle))]
    public string VehicleName { get; set; } // Can only be "Car", "Boat", or "Plane"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants