public IEnumerable <ValidationFailure> Validate(PropertyValidatorContext context) { EnsureArg.IsNotNull(context, nameof(context)); if (context.PropertyValue is ResourceElement resourceElement) { var results = new List <ValidationResult>(); if (!_modelAttributeValidator.TryValidate(resourceElement, results, recurse: false)) { foreach (var error in results) { yield return(new ValidationFailure(error.MemberNames?.FirstOrDefault(), error.ErrorMessage)); } } } }
public override IEnumerable <ValidationFailure> Validate(PropertyValidatorContext context) { EnsureArg.IsNotNull(context, nameof(context)); if (context.PropertyValue is ResourceElement resourceElement) { var results = new List <ValidationResult>(); if (!_modelAttributeValidator.TryValidate(resourceElement, results, false)) { foreach (var error in results) { var fullFhirPath = resourceElement.InstanceType; fullFhirPath += string.IsNullOrEmpty(error.MemberNames?.FirstOrDefault()) ? string.Empty : "." + error.MemberNames?.FirstOrDefault(); yield return(new ValidationFailure(fullFhirPath, error.ErrorMessage)); } } } }
public void GivenAMissingAttribute_WhenProcessingAResource_ThenAValidationMessageWithAFhirPathIsCreated() { var defaultObservation = Samples.GetDefaultObservation().ToPoco <Observation>(); defaultObservation.StatusElement = null; var results = new List <ValidationResult>(); bool isValid = _modelAttributeValidator.TryValidate(defaultObservation.ToResourceElement(), results, recurse: false); Assert.False(isValid); List <ValidationResult> validationFailures = results ?? results.ToList(); Assert.Single(validationFailures); var error = validationFailures.FirstOrDefault(); var actualPartialFhirPath = string.IsNullOrEmpty(error.MemberNames?.FirstOrDefault()) ? string.Empty : error.MemberNames?.FirstOrDefault(); // TODO: Expected value should be "status" once actual path is fixed. Assert.Equal("StatusElement", actualPartialFhirPath); }
public override FluentValidation.Results.ValidationResult Validate(ValidationContext <ResourceElement> context) { EnsureArg.IsNotNull(context, nameof(context)); var failures = new List <ValidationFailure>(); if (context.InstanceToValidate is ResourceElement resourceElement) { var results = new List <ValidationResult>(); if (!_modelAttributeValidator.TryValidate(resourceElement, results, false)) { foreach (var error in results) { var fullFhirPath = resourceElement.InstanceType; fullFhirPath += string.IsNullOrEmpty(error.MemberNames?.FirstOrDefault()) ? string.Empty : "." + error.MemberNames?.FirstOrDefault(); var validationFailure = new ValidationFailure(fullFhirPath, error.ErrorMessage); failures.Add(validationFailure); } } } failures.ForEach(x => context.AddFailure(x)); return(new FluentValidation.Results.ValidationResult(failures)); }