1
votes

I'm coming up short, trying to use Fluent Validation in the ServiceStack DTOs, when the model have properties nested several levels.

Ex.: The model is structured like this A => B => C => D => E

To validate B without risking a nullref. exception, I need to (successfully) validate A.

To Validate C, A and B must be not null.

D and E have cross dependencies so to validate E then A, B, C AND D must not be null.

The only way that is seemingly available for this scenario, is wrapping the entire thing in when/dependent blocks, but that quickly becomes unmanageable.

Any ideas would be appreciated!

Thanks,

1

1 Answers

0
votes

Alternatives of using multiple validators constrained by When() blocks is to either use a "Must" predicate validator, create a Custom Validator or avoid using Fluent Validation for these complex validations and validate them in your Service where you can use full C# to validate them as normal.

Throwing a ArgumentException(message,paramName) in your Service using the name of the Request DTO property will populate the Error in the structured ResponseStatus Errors collection.