diff options
Diffstat (limited to 'Validators')
-rw-r--r-- | Validators/NameAttribute.cs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/Validators/NameAttribute.cs b/Validators/NameAttribute.cs new file mode 100644 index 0000000..2f1a5ae --- /dev/null +++ b/Validators/NameAttribute.cs @@ -0,0 +1,20 @@ +using System.ComponentModel.DataAnnotations; +using LibraryAPI.Models; + +namespace LibraryAPI.Validators { + public class NameAttribute : ValidationAttribute { + public string[] valid_names = { "regular", "librarian" }; + public string GetErrorMessage() { + return "Group name must be \"regular\" or \"librarian\""; + } + + protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) { + var group = (Group) validationContext.ObjectInstance; + + if(Array.Exists(valid_names, s => s == group.Name)) + return ValidationResult.Success; + + return new ValidationResult(GetErrorMessage()); + } + } +} |