0.1.0 - ci-build

JavascriptExpressionsinFHIR, published by Hl7 International/ FHIR infrastructure. This guide is not an authorized publication; it is the continuous build for version 0.1.0 built by the FHIR (HL7® FHIR® Standard) CI Build. This version is based on the current content of https://github.com/bvdh/hl7-javascript-expression/ and changes regularly. See the Directory of published versions

Javascript in FHIR expressions

Official URL: http://hl7.org/fhir/uv/javascript/ImplementationGuide/hl7.fhir.uv.javascript Version: 0.1.0
Draft as of 2025-01-10 Computable Name: JavascriptExpressionsinFHIR

Javascript Expressions in FHIR

FHIR supports different types of of expression logic: fhirpath and CQL and FML.

The downside of these specialized languages is that they depend on specific libraries. It also requires authors to understand these languages. This makes them more cumbersome to use. An alternative to these specialized languages would be a commonly available, well accepted programming language that can be run (safely) in many different environments. A prime example of such language is javascript.

This implementation guide extends FHIR with support for javascript based expressions.

javascript profile

Javascript is a powerful languages with many features and capabilities. In order to allows for safe execution of untrusted code in when evaluating javascript expressions, the language is restricted.

Allowed functions

The javascript is restricted to the following commands:

"self", "proto", "defineGetter", "defineSetter", "lookupGetter", "lookupSetter", "constructor", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", "toLocaleString", "toString", "Array", "Boolean", "Date", "Function", "Object", "String", "undefined", "Infinity", "isFinite", "isNaN", "Math", "NaN", "Number", "parseFloat", "parseInt", "RegExp"

FHIRpath support

In addition, evaluation of FHIRpath expression is allowed using the following expression:

FHIRpath.evaluate( base, expression )

Upon execution, all variables available to FHIRpath and CQL expressions are provided on ENV.

I.e. when running these expressions from an SDC questionnaire the following context variables are available:

Variable description
ENV.resource Corresponds to %resource and refers to the QuestionnaireResponse
ENV.questionnaire Relates to the current Questionnaire resource

Libraries

It is possible to use Javascript libraries. These are defined as FHIR Library resources (see …). The Library can contain javascript or refer to a file where the javascript can be downloaded from.

Defining and executing javascript

The field Expression.expression will hold the javascript expression to be executed. When the expression refers to functions, these have to be defined in a Library resource.

The content of a library resources is concatenated to the expression logic. The complete file is executed.