I have the following DTO:
public class SomethingRequest {
public string Code { get; set; }
}
Code must be unique, so I created a validator that checks if there is already a record with the provided code, for example,
public class SomethingValidator: AbstractValidator<SomethingRequest>
{
public SomethingValidator(ISomethingRepository repo) {
RuleFor(something => something.Code).Must(BeUnique);
}
private bool BeUnique(string code) { ... uniqueness check... }
}
Since I use the validation function, the validator is automatically connected for all methods using SomethingRequest, which is really great.
If the condition fails, I would like to return an 409 ConflictHTTP status code, but it 400 Bad Requestalways returns.
So the questions are:
- Am I using the vaidation function incorrectly? (that is, automatic validators were not intended to be used to verify application logic).
- If this is not the case, are there any ways to override the status code
400 BadRequestfrom the validator?