summaryrefslogtreecommitdiff
path: root/Validators
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2022-09-20 20:22:18 -0500
committerHombreLaser <sebastian-440@live.com>2022-09-20 20:22:18 -0500
commitf4f9762085b9858eacd7906188841e06e20a893a (patch)
treeb5dc1d74628f06e305afc453c4676e29359f1ba8 /Validators
parent766d9635dc09886707213cdbafa8a783a143ce84 (diff)
Añadidos validadores
Diffstat (limited to 'Validators')
-rw-r--r--Validators/ISBNAttribute.cs26
1 files changed, 26 insertions, 0 deletions
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