From f4f9762085b9858eacd7906188841e06e20a893a Mon Sep 17 00:00:00 2001 From: HombreLaser Date: Tue, 20 Sep 2022 20:22:18 -0500 Subject: AƱadidos validadores MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Validators/ISBNAttribute.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Validators/ISBNAttribute.cs (limited to 'Validators') diff --git a/Validators/ISBNAttribute.cs b/Validators/ISBNAttribute.cs new file mode 100644 index 0000000..425e9e9 --- /dev/null +++ b/Validators/ISBNAttribute.cs @@ -0,0 +1,26 @@ +using System.ComponentModel.DataAnnotations; +using LibraryAPI.Models; + +namespace LibraryAPI.Validators { + public class ISBNAttribute : ValidationAttribute { + public int ISBNlength { get; } + + public ISBNAttribute(int isbn_length) { + ISBNlength = isbn_length; + } + + public string GetErrorMessage() { + return $"ISBN strings must be {ISBNlength} characters long"; + } + + protected override ValidationResult? IsValid(object? value, ValidationContext validationContext) { + var book = (Book) validationContext.ObjectInstance; + + if(book.ISBN.Length != ISBNlength) { + return new ValidationResult(GetErrorMessage()); + } + + return ValidationResult.Success; + } + } +} \ No newline at end of file -- cgit v1.2.3