Updated address validating logic

This commit is contained in:
Nick Goupinets 2021-04-27 18:27:38 -04:00
parent 7cef07b0c1
commit a1604c95c9
1 changed files with 23 additions and 5 deletions

View File

@ -126,13 +126,31 @@ public class AddressValidatingInterceptor {
if (!theRequest.getHeaders(ADDRESS_VALIDATION_DISABLED_HEADER).isEmpty()) { if (!theRequest.getHeaders(ADDRESS_VALIDATION_DISABLED_HEADER).isEmpty()) {
ourLog.debug("Address validation is disabled for this request via header"); ourLog.debug("Address validation is disabled for this request via header");
return;
} }
FhirContext ctx = theRequest.getFhirContext(); FhirContext ctx = theRequest.getFhirContext();
getAddresses(theResource, ctx) List<IBase> addresses = getAddresses(theResource, ctx)
.stream() .stream()
.filter(this::isValidating) .filter(this::isValidating)
.forEach(a -> validateAddress(a, ctx)); .collect(Collectors.toList());
if (!addresses.isEmpty()) {
validateAddresses(theRequest, theResource, addresses);
}
}
/**
* Validates specified child addresses for the resource
*
* @return Returns true if all addresses are valid, or false if there is at least one invalid address
*/
protected boolean validateAddresses(RequestDetails theRequest, IBaseResource theResource, List<IBase> theAddresses) {
boolean retVal = true;
for (IBase address : theAddresses) {
retVal &= validateAddress(address, theRequest.getFhirContext());
}
return retVal;
} }
private boolean isValidating(IBase theAddress) { private boolean isValidating(IBase theAddress) {
@ -140,15 +158,13 @@ public class AddressValidatingInterceptor {
if (ext == null) { if (ext == null) {
return true; return true;
} }
if (ext.getValue() == null || ext.getValue().isEmpty()) { if (ext.getValue() == null || ext.getValue().isEmpty()) {
return true; return true;
} }
return !"false".equals(ext.getValue().toString()); return !"false".equals(ext.getValue().toString());
} }
protected void validateAddress(IBase theAddress, FhirContext theFhirContext) { protected boolean validateAddress(IBase theAddress, FhirContext theFhirContext) {
try { try {
AddressValidationResult validationResult = getAddressValidator().isValid(theAddress, theFhirContext); AddressValidationResult validationResult = getAddressValidator().isValid(theAddress, theFhirContext);
ourLog.debug("Validated address {}", validationResult); ourLog.debug("Validated address {}", validationResult);
@ -160,11 +176,13 @@ public class AddressValidatingInterceptor {
} else { } else {
ourLog.info("Validated address is not provided - skipping update on the target address instance"); ourLog.info("Validated address is not provided - skipping update on the target address instance");
} }
return validationResult.isValid();
} catch (Exception ex) { } catch (Exception ex) {
ourLog.warn("Unable to validate address", ex); ourLog.warn("Unable to validate address", ex);
IBaseExtension extension = ExtensionUtil.getOrCreateExtension(theAddress, getExtensionUrl()); IBaseExtension extension = ExtensionUtil.getOrCreateExtension(theAddress, getExtensionUrl());
IBaseExtension errorValue = ExtensionUtil.getOrCreateExtension(extension, "error"); IBaseExtension errorValue = ExtensionUtil.getOrCreateExtension(extension, "error");
errorValue.setValue(TerserUtil.newElement(theFhirContext, "string", ex.getMessage())); errorValue.setValue(TerserUtil.newElement(theFhirContext, "string", ex.getMessage()));
return false;
} }
} }