0

Getting the below exception when running the code:

     FhirContext ctx = FhirContext.forR4();

    // Create a FhirInstanceValidator and register it to a validator
    FhirValidator validator = ctx.newValidator();
    FhirInstanceValidator instanceValidator = new FhirInstanceValidator();
    validator.registerValidatorModule(instanceValidator);

    /*
     * If you want, you can configure settings on the validator to adjust
     * its behaviour during validation
     */
    instanceValidator.setAnyExtensionsAllowed(true);
    // input is Patient resource in String https://www.hl7.org/fhir/patient-example.json.html 
    ValidationResult result = validator.validateWithResult(input); 

I am using Hapi Library to validate a resource (if i am not wrong this is a Patient resource https://www.hl7.org/fhir/patient-example.json.html ). I have stored this Patient Json in a string and trying to Validate its :

1: Structure -> i think using Parse Validation it can be achieved and i did the same. 2: Cardinality -> I created two "active:true" Json key-value pair thinking that it will throw cardinality error but neither of SchemxxxValidator / ParseValidator / InstanceValidator working. ...

How to validate a resource against properties listed here https://www.hl7.org/fhir/validation.html (structure ,cardinality , ValueDomains ...) , Do i have to use all three ways That is Parser , FhirInstanceValidator and SchemaBaseValidator / SchematronBaseValidator .

Please Help as i am new to FHIR and excuse for lame question.

15:58| INFO | VersionUtil.java 72 | HAPI FHIR version 4.1.0 - Rev 03163c2cf5
15:58| INFO | FhirContext.java 174 | Creating new FHIR context for FHIR version [R4]
15:58| INFO | DefaultProfileValidationSupport.java 227 | Loading structure definitions from classpath: /org/hl7/fhir/r4/model/profile/profiles-resources.xml
15:58| INFO | DependencyLogImpl.java 75 | FHIR XML procesing will use StAX implementation 'Woodstox' version '5.1.0'
15:58| INFO | DefaultProfileValidationSupport.java 227 | Loading structure definitions from classpath: /org/hl7/fhir/r4/model/profile/profiles-types.xml
15:58| INFO | DefaultProfileValidationSupport.java 227 | Loading structure definitions from classpath: /org/hl7/fhir/r4/model/profile/profiles-others.xml
15:58| INFO | DefaultProfileValidationSupport.java 227 | Loading structure definitions from classpath: /org/hl7/fhir/r4/model/extension/extension-definitions.xml
15:58| ERROR | FhirInstanceValidator.java 222 | Failure during validation
java.lang.UnsupportedOperationException
    at org.hl7.fhir.r4.hapi.ctx.HapiWorkerContext.generateSnapshot(HapiWorkerContext.java:242)
    at org.hl7.fhir.r4.elementmodel.ParserBase.getDefinition(ParserBase.java:122)
    at org.hl7.fhir.r4.elementmodel.JsonParser.parse(JsonParser.java:123)
    at org.hl7.fhir.r4.validation.InstanceValidator.validate(InstanceValidator.java:539)
    at org.hl7.fhir.r4.validation.InstanceValidator.validate(InstanceValidator.java:531)
    at org.hl7.fhir.r4.hapi.validation.FhirInstanceValidator.validate(FhirInstanceValidator.java:220)
    at org.hl7.fhir.r4.hapi.validation.FhirInstanceValidator.validate(FhirInstanceValidator.java:242)
    at org.hl7.fhir.r4.hapi.validation.BaseValidatorBridge.doValidate(BaseValidatorBridge.java:20)
    at org.hl7.fhir.r4.hapi.validation.BaseValidatorBridge.validateResource(BaseValidatorBridge.java:43)
    at org.hl7.fhir.r4.hapi.validation.FhirInstanceValidator.validateResource(FhirInstanceValidator.java:33)
    at ca.uhn.fhir.validation.FhirValidator.validateWithResult(FhirValidator.java:243)
    at ca.uhn.fhir.validation.FhirValidator.validateWithResult(FhirValidator.java:198)
    at com.json.schema.validator.InstanceValidatorEx.instanceValidator(InstanceValidatorEx.java:223)
    at com.json.schema.validator.InstanceValidatorEx.main(InstanceValidatorEx.java:191)
Exception in thread "main" ca.uhn.fhir.rest.server.exceptions.InternalErrorException: Unexpected failure while validating resource
    at org.hl7.fhir.r4.hapi.validation.FhirInstanceValidator.validate(FhirInstanceValidator.java:223)
    at org.hl7.fhir.r4.hapi.validation.FhirInstanceValidator.validate(FhirInstanceValidator.java:242)
    at org.hl7.fhir.r4.hapi.validation.BaseValidatorBridge.doValidate(BaseValidatorBridge.java:20)
    at org.hl7.fhir.r4.hapi.validation.BaseValidatorBridge.validateResource(BaseValidatorBridge.java:43)
    at org.hl7.fhir.r4.hapi.validation.FhirInstanceValidator.validateResource(FhirInstanceValidator.java:33)
    at ca.uhn.fhir.validation.FhirValidator.validateWithResult(FhirValidator.java:243)
    at ca.uhn.fhir.validation.FhirValidator.validateWithResult(FhirValidator.java:198)
    at com.json.schema.validator.InstanceValidatorEx.instanceValidator(InstanceValidatorEx.java:223)
    at com.json.schema.validator.InstanceValidatorEx.main(InstanceValidatorEx.java:191)
Caused by: java.lang.UnsupportedOperationException
    at org.hl7.fhir.r4.hapi.ctx.HapiWorkerContext.generateSnapshot(HapiWorkerContext.java:242)
    at org.hl7.fhir.r4.elementmodel.ParserBase.getDefinition(ParserBase.java:122)
    at org.hl7.fhir.r4.elementmodel.JsonParser.parse(JsonParser.java:123)
    at org.hl7.fhir.r4.validation.InstanceValidator.validate(InstanceValidator.java:539)
    at org.hl7.fhir.r4.validation.InstanceValidator.validate(InstanceValidator.java:531)
    at org.hl7.fhir.r4.hapi.validation.FhirInstanceValidator.validate(FhirInstanceValidator.java:220)
user2734915
  • 85
  • 1
  • 10

1 Answers1

1

Cardinality -> I created two "active:true" Json key-value pair thinking that it will throw cardinality error but neither of SchemxxxValidator / ParseValidator / InstanceValidator working. ...

That's an issue in HAPI - it validates the objects it loads from the JSON, and the JSON parser silently drops the duplicate property key. If you use the validator directly, this won't happen. I believe that this is going to be addressed at some stage

generateSnapshot failed

that's a real issue - I'm not sure why that's not set up, but the validator can't work if snapshots are not being generated

Grahame Grieve
  • 3,538
  • 3
  • 15
  • 17
  • what do you mean when you say "If you use the validator directly, this won't happen" , if you meant not to use parser before validator then i haven't , else can you point me to references where it can be more clear . – user2734915 Dec 09 '19 at 07:26
  • Using the jar - per https://confluence.hl7.org/display/FHIR/Using+the+FHIR+Validator – Grahame Grieve Dec 09 '19 at 11:12
  • As per your comment at https://stackoverflow.com/questions/24407744/how-to-run-fhir-validator-java-jar the above Validator jar doesn't support json . Then how to validate a fhir resource in json format. – user2734915 Dec 19 '19 at 15:44
  • that was a long time ago. It just supports json automatically now – Grahame Grieve Dec 19 '19 at 19:10