You could annotate your request class & use Validator.TryValidateObjects
to get the validation errors (if any):
using System.ComponentModel.DataAnnotations;
Model m = new Model {
Name = "12345678910"
};
ICollection<ValidationResult> results = new List<ValidationResult>();
if (!Validator.TryValidateObject(m, new ValidationContext(m), results, validateAllProperties: true)) {
foreach (ValidationResult result in results) {
Console.WriteLine(result.ErrorMessage);
}
}
class Model {
[Required]
[StringLength(10)]
public string Name { get; set; }
}
Here's the list of available data annotations you can use:
https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations?view=net-7.0