Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: Validating enums with a custom FluentValidator validator

$
0
0

The reason for the custom validator is that you can cast any int to an enum:

    enum DaysOfWeek { Mon=0, Tue, Wed, Thu, Fri, Sat, Sun }
    DaysOfWeek thisisallowed = (DaysOfWeek)100;
    DaysOfWeek soisthis = (DaysOfWeek)(-1);

This is problematic when the int comes from a GET/POST, and is subject to tampering. So I want to ensure that it is not just an int, but that it is within the enum's actual range. For DaysOfWeek its range is 0..6. If the input is 7 then the validation should fail.

This is important because these sort of ints are used as IDs in repository methods, etc. So the value must be in the appropriate range.

Though I can't even get that far, as (see Q), I can't it to work generically. Not sure why?


Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>