I am trying to pass an inherited attribute into the IsValid() error message in a custom attribute validator. However, I can't seem to figure out how to do so after searching online for this.
I have a class named MarketConfiguration
public class MarketConfiguration : BaseConfiguration
{
[Required]
[AcceptedStores(0,2,11)]
public int Store {get; set;}
}
where BaseConfiguration
public class BaseConfiguration
{
private string _manufactureId;
[Required]
public string ManufactureId
{
get { return _manufactureId; }
set
{
_manufactureId = value.Trim();
}
}
}
where my custom validator contains
public class AcceptedStores : ValidationAttribute
{
private readonly int[] _store;
private readonly int _manufacturerId;
public AllowedIntegers(int[] store)
{
_store = store;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
int storesToValidate = (int)value;
if(!_store.Contains(storesToValidate))
{
return new ValidationResult($"For manufacturerID {_manufacturerId}, the store {_store} does not match. Please enter correct values.");
}
return ValidationResult.Success;
}
}
but the error message does not return the manufacturerId.. only the store value.
I pass this in json body in postman
[
{
"manufacturerId": "7893",
"store": 93
}
]
Is there a way to pass manufacturerId into the error message that is in the custom validator for Store?