blob: 425e9e9da20b28d4e08ad40b6745406951cd89db (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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;
}
}
}
|