I am doing a validation task for my Web API project. It exposes APIs to the front-end. The front-end should input valid values in the json file, but sometimes, if the invalid values coming it cause exceptions in my project.
For example, someId
cannot be 0
or other invalid values. Because if 0 was set to someId
, the DB will yell the foreign key constraints error.
My solution to this is using the attribute validation
[Range(1, 10, ErrorMessage = "{0} Values must be in range {1} to {2}")]
public int? someId { get; set; }
So it will prevent the invalid values from UI. But the little hassle is I need to hard code the valid data range in each fields. So I am asking if it has a better solution than mine?