In HL7 FHIR ® is quite easy to validate – to check the syntactic correctness – a resource by means of a FHIR server.
In order to keep this example simple we will use the FHIR CLI (Command line interface) to build the resources and to interact with the FHIR server.
Validation of a resource in XML format
Start FHIR CLI typing in a python interpreter…
from client import * cli = init_client()
Store the resource in a variable called “res”
res = '''<Patient xmlns="http://hl7.org/fhir"> <id value="pat1"/> <text> <status value="generated"/> <div xmlns="http://www.w3.org/1999/xhtml"> <p>Patient Donald DUCK @ Acme Healthcare, Inc. MR = 654321</p> </div> </text> <identifier> <use value="usual"/> <type> <coding> <system value="http://hl7.org/fhir/v2/0203"/> <code value="MR"/> </coding> </type> <system value="urn:oid:0.1.2.3.4.5.6.7"/> <value value="654321"/> </identifier> <active value="true"/> <name> <use value="official"/> <family value="Donald"/> <given value="Duck"/> </name> <gender value="male"/> </Patient>'''
Now execute the FHIR validate operation
resp = validate(cli, resource="Patient", par=res, format_acc="xml")
See the response code in order to check the operation result
resp.resp_code()
or simply type the variable name “resp” to display the output of the command
resp
You will see something like this:
<?xml version="1.0" encoding="UTF-8"?><OperationOutcome xmlns="http://hl7.org/fhir"><text><status value="generated"/><div xmlns="http://www.w3.org/1999/xhtml"><p><b>Operation Outcome for :Validate resource </b></p><p>All OK</p></div></text></OperationOutcome> POST Url: http://fhir3.healthintersections.com.au/open/Patient/$validate?async=false Output code: 200
The operation “POST Url: http://fhir3.healthintersections.com.au/open/Patient/$validate?async=false” was All OK
The resource is validated.
Validation of a resource in json format
In this example we will build a resource using the resource constructor (Patient()), but, if you prefer, you can store directly the json object in a variable and validate it in the previously seen way.
pa = Patient({"resourceType":"Patient", "id": "pat1", "name":[{"family":["Donald"],"given":["TestName"],"use":"official"}]}) # check the content of pa simply entering pa variable pa
and validate it.
Pay attention: to obtain the json representation from Patient resource stored in “pa” variable, use pa.json
resp = validate(cli, resource="Patient", par=pa.json, format_acc="json")
See the response code in order to check the operation result
resp.resp_code()
Pierfrancesco Ghedini
Quest’opera è distribuita con Licenza Creative Commons Attribuzione 3.0 Italia.