Skip to content

Instantly share code, notes, and snippets.

@lgolubyev
Created August 8, 2022 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgolubyev/8460bc9317bf4a420b8fc508e7a38f8f to your computer and use it in GitHub Desktop.
Save lgolubyev/8460bc9317bf4a420b8fc508e7a38f8f to your computer and use it in GitHub Desktop.
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