@SuppressWarnings("unused") private static void validateFiles() throws Exception { // START SNIPPET: validateFiles FhirContext ctx = FhirContext.forDstu3(); // Create a validator and configure it FhirValidator validator = ctx.newValidator(); validator.setValidateAgainstStandardSchema(true); validator.setValidateAgainstStandardSchematron(true); // Get a list of files in a given directory String[] fileList = new File("/home/some/dir").list(new WildcardFileFilter("*.txt")); for (String nextFile : fileList) { // For each file, load the contents into a string String nextFileContents = IOUtils.toString(new FileReader(nextFile)); // Parse that string (this example assumes JSON encoding) IBaseResource resource = ctx.newJsonParser().parseResource(nextFileContents); // Apply the validation. This will throw an exception on the first // validation failure ValidationResult result = validator.validateWithResult(resource); if (result.isSuccessful() == false) { throw new Exception("We failed!"); } } // END SNIPPET: validateFiles }
FhirValidator val = ctx.newValidator(); val.registerValidatorModule(instanceValidator);
bundle.setType(BundleType.TRANSACTION); FhirValidator val = ctx.newValidator(); val.registerValidatorModule(new FhirInstanceValidator(new DefaultProfileValidationSupport()));
bundle.setType(org.hl7.fhir.r4.model.Bundle.BundleType.TRANSACTION); FhirValidator val = ctx.newValidator(); val.registerValidatorModule(new org.hl7.fhir.r4.hapi.validation.FhirInstanceValidator(new org.hl7.fhir.r4.hapi.ctx.DefaultProfileValidationSupport()));
FhirValidator val = ctx.newValidator();
public void validationIntro() { // START SNIPPET: validationIntro FhirContext ctx = FhirContext.forDstu3(); // Ask the context for a validator FhirValidator validator = ctx.newValidator(); // Create some modules and register them IValidatorModule module1 = new SchemaBaseValidator(ctx); validator.registerValidatorModule(module1); IValidatorModule module2 = new SchematronBaseValidator(ctx); validator.registerValidatorModule(module2); // Pass a resource in to be validated. The resource can // be an IBaseResource instance, or can be a raw String // containing a serialized resource as text. Patient resource = new Patient(); ValidationResult result = validator.validateWithResult(resource); String resourceText = "<Patient.....>"; ValidationResult result2 = validator.validateWithResult(resourceText); // The result object now contains the validation results for (SingleValidationMessage next : result.getMessages()) { System.out.println(next.getLocationString() + " " + next.getMessage()); } // END SNIPPET: validationIntro }
FhirValidator val = ctx.newValidator();
FhirValidator validator = ctx.newValidator(); FhirInstanceValidator instanceValidator = new FhirInstanceValidator(); validator.registerValidatorModule(instanceValidator);
FhirValidator validator = ctx.newValidator();
FhirValidator validator = ctx.newValidator(); FhirInstanceValidator instanceValidator = new FhirInstanceValidator(); validator.registerValidatorModule(instanceValidator);
FhirValidator validator = theRequestDetails.getServer().getFhirContext().newValidator(); if (myValidatorModules != null) { for (IValidatorModule next : myValidatorModules) {
FhirValidator validator = getContext().newValidator();
FhirValidator validator = getContext().newValidator();
FhirValidator validator = getContext().newValidator();
@Override protected void validateResourceForStorage(QuestionnaireResponse theResource, ResourceTable theEntityToSave) { super.validateResourceForStorage(theResource, theEntityToSave); if (!myValidateResponses) { return; } QuestionnaireResponse qa = theResource; if (qa == null || qa.getQuestionnaire() == null || qa.getQuestionnaire().getReference() == null || qa.getQuestionnaire().getReference().isEmpty()) { return; } FhirValidator val = myRefImplCtx.newValidator(); val.setValidateAgainstStandardSchema(false); val.setValidateAgainstStandardSchematron(false); FhirQuestionnaireResponseValidator module = new FhirQuestionnaireResponseValidator(); module.setResourceLoader(new JpaResourceLoader()); module.setWorkerContext(new HapiWorkerContext(myRefImplCtx, myValidationSupport)); val.registerValidatorModule(module); ValidationResult result = val.validateWithResult(myRefImplCtx.newJsonParser().parseResource(getContext().newJsonParser().encodeResourceToString(qa))); if (!result.isSuccessful()) { IBaseOperationOutcome oo = getContext().newJsonParser().parseResource(OperationOutcome.class, myRefImplCtx.newJsonParser().encodeResourceToString(result.toOperationOutcome())); throw new UnprocessableEntityException(getContext(), oo); } }
public static void main(String[] args) { String input = "<Encounter xmlns=\"http://hl7.org/fhir\"></Encounter>"; // Create a new validator FhirContext ctx = FhirContext.forDstu3(); FhirValidator validator = ctx.newValidator(); // Did we succeed? ValidationResult result = validator.validateWithResult(input); System.out.println("Success: " + result.isSuccessful()); // What was the result OperationOutcome outcome = (OperationOutcome) result.toOperationOutcome(); IParser parser = ctx.newXmlParser().setPrettyPrint(true); System.out.println(parser.encodeResourceToString(outcome)); } }
@Override public void validateRequest(FhirContext context, Object payload, Map<String, Object> parameters) { Bundle transactionBundle = (Bundle) payload; validateTransactionBundle(transactionBundle); validateBundleConsistency(transactionBundle); for (Bundle.BundleEntryComponent entry : transactionBundle.getEntry()) { Class<? extends IBaseResource> clazz = entry.getResource().getClass(); if (VALIDATORS.containsKey(clazz)) { FhirValidator validator = context.newValidator(); validator.registerValidatorModule(VALIDATORS.get(clazz)); ValidationResult validationResult = validator.validateWithResult(entry.getResource()); if (!validationResult.isSuccessful()) { IBaseOperationOutcome operationOutcome = validationResult.toOperationOutcome(); throw FhirUtils.exception(UnprocessableEntityException::new, operationOutcome, "Validation Failed"); } } } }
public static void main(String[] args) { // Create an incomplete encounter (status is required) Encounter enc = new Encounter(); enc.addIdentifier().setSystem("http://acme.org/encNums").setValue("12345"); // Create a new validator FhirContext ctx = FhirContext.forDstu3(); FhirValidator validator = ctx.newValidator(); // Did we succeed? ValidationResult result = validator.validateWithResult(enc); System.out.println("Success: " + result.isSuccessful()); // What was the result OperationOutcome outcome = (OperationOutcome) result.toOperationOutcome(); IParser parser = ctx.newXmlParser().setPrettyPrint(true); System.out.println(parser.encodeResourceToString(outcome)); } }
public static void main(String[] args) { // Create an incomplete encounter (status is required) Encounter enc = new Encounter(); enc.addIdentifier().setSystem("http://acme.org/encNums").setValue("12345"); // Create a new validator FhirValidator validator = FhirContext.forDstu3().newValidator(); // Cache this object! Supplies structure definitions DefaultProfileValidationSupport support = new DefaultProfileValidationSupport(); // Create the validator FhirInstanceValidator module = new FhirInstanceValidator(support); validator.registerValidatorModule(module); // Did we succeed? IParser parser = FhirContext.forDstu3().newXmlParser().setPrettyPrint(true); System.out.println(parser.encodeResourceToString(validator.validateWithResult(enc).toOperationOutcome())); } }
/** * Create FHIR context, validator, and validation support. */ public ValidationResources() { // Only support for dstu3 for now ctx = FhirContext.forDstu3(); validator = ctx.newValidator(); instanceValidator = new FhirInstanceValidator(); IValidationSupport valSupport = new ValidationSupport(); ValidationSupportChain support = new ValidationSupportChain(valSupport, new DefaultProfileValidationSupport()); instanceValidator.setValidationSupport(support); validator.registerValidatorModule(instanceValidator); }