-
-
Save lgolubyev/8460bc9317bf4a420b8fc508e7a38f8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SaladChefValidatorAttribute : ValidationAttribute | |
{ | |
protected override ValidationResult IsValid(object value, ValidationContext validationContext) | |
{ | |
var saladChef = validationContext.GetRequiredService<SaladChef>(); | |
if (saladChef.ThingsYouCanPutInASalad.Contains(value.ToString())) | |
{ | |
return ValidationResult.Success; | |
} | |
return new ValidationResult("You should not put that in a salad!"); | |
} | |
} | |
// Simple class configured as a service for dependency injection | |
public class SaladChef | |
{ | |
public string[] ThingsYouCanPutInASalad = { "Strawberries", "Pineapple", "Honeydew", "Watermelon", "Grapes" }; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment