I am using FluentValidation version 9.2.2. I am getting this following message:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Json:
{ "geographyInfo": { "CountryCode": "UK" } }
RuleFor(x => x.Request.GeographyInfo)
.NotEmpty()
.WithMessage("GeographyInfo missing. Expected: object of type GeoInfo.")
.WithErrorCode("123")
.WithName("geographyInfo");
RuleFor(x => x.Request.GeographyInfo.CountryCode)
.NotEmpty()
.WithMessage("Field geographyInfo.CountryCode is missing. Expected: 3 digit code")
.WithErrorCode("13")
.WithName("countryCode");
Problem is, if I send a json like this: Json:
{ }
(with no geo info), I am getting a NullReferenceException, while I expect the "GeographyInfo missing. Expected: object of type GeoInfo."
What happens is I think FluentValidation goes ahead and checks also the 2nd rule: on field x.Request.GeographyInfo.CountryCode, but we don't have x.Request.GeographyInfo in this case, so it doesn't make sense to further reference the CountryCode.
How do I tell FluentValidation not to check rules for subfields of fields he doesnt find? (not included in the request)