public override IEnumerable <ValidationFailure> Validate(PropertyValidatorContext context) { EnsureArg.IsNotNull(context, nameof(context)); if (context.PropertyValue is ResourceElement resourceElement) { var fhirContext = _contextAccessor.FhirRequestContext; var profileValidation = _runProfileValidation; if (fhirContext.RequestHeaders.ContainsKey(KnownHeaders.ProfileValidation) && fhirContext.RequestHeaders.TryGetValue(KnownHeaders.ProfileValidation, out var value)) { if (bool.TryParse(value, out bool headerValue)) { profileValidation = headerValue; } } if (profileValidation) { var errors = _profileValidator.TryValidate(resourceElement.Instance); foreach (var error in errors.Where(x => x.Severity == IssueSeverity.Error || x.Severity == IssueSeverity.Fatal)) { yield return(new FhirValidationFailure( resourceElement.InstanceType, error.DetailsText, error)); } } foreach (var baseError in base.Validate(context)) { yield return(baseError); } } }
/// <summary> /// Handles validation requests. /// </summary> /// <param name="request">The request</param> /// <param name="cancellationToken">The CancellationToken</param> public async Task <ValidateOperationResponse> Handle(ValidateOperationRequest request, CancellationToken cancellationToken) { if (await _authorizationService.CheckAccess(DataActions.ResourceValidate, cancellationToken) != DataActions.ResourceValidate) { throw new UnauthorizedFhirActionException(); } var outcome = _profileValidator.TryValidate(request.Resource.Instance, request.Profile?.ToString()); if (outcome.Length == 0) { return(new ValidateOperationResponse(ValidationPassed)); } return(new ValidateOperationResponse(outcome)); }
public override ValidationResult Validate(ValidationContext <ResourceElement> context) { EnsureArg.IsNotNull(context, nameof(context)); var failures = new List <ValidationFailure>(); if (context.InstanceToValidate is ResourceElement resourceElement) { var fhirContext = _contextAccessor.RequestContext; var profileValidation = _runProfileValidation; if (fhirContext.RequestHeaders.ContainsKey(KnownHeaders.ProfileValidation) && fhirContext.RequestHeaders.TryGetValue(KnownHeaders.ProfileValidation, out var hValue)) { if (bool.TryParse(hValue, out bool headerValue)) { profileValidation = headerValue; } } if (profileValidation) { var errors = _profileValidator.TryValidate(resourceElement.Instance); foreach (var error in errors.Where(x => x.Severity == IssueSeverity.Error || x.Severity == IssueSeverity.Fatal)) { var validationFailure = new FhirValidationFailure( resourceElement.InstanceType, error.DetailsText, error); failures.Add(validationFailure); } } var baseValidation = base.Validate(context); failures.AddRange(baseValidation.Errors); } failures.ForEach(x => context.AddFailure(x)); return(new ValidationResult(failures)); }