Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fluent validation add validation errors mannually and rethrow errors

I have defined an:

IValidator<SomeClass> _myValidator

I can do:

_myValidator.ValidateAndThrow(someObject);

instead I would like to:

var errors = _myValidator.Validate(entity);

add some errors manually and then re-throw the errors. Is this possible?

like image 686
cs0815 Avatar asked May 21 '26 15:05

cs0815


1 Answers

If you want to store the errors in a variable, you'll have to return the error, or a list of errors, instead of throwing them.

IList<Exception> ValidateAndThrow(object someObject){

    IList<Exception> errors = new List<Exception>();

    try{
        SomethingGoesWrong();
    } catch (Exception e){
        errors.Add(e);
    }

    return errors;
}
like image 80
xlecoustillier Avatar answered May 23 '26 06:05

xlecoustillier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!