-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.js
More file actions
199 lines (175 loc) · 6.54 KB
/
Copy pathexample.js
File metadata and controls
199 lines (175 loc) · 6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const FhirValidator = require('./fhir-validator');
async function main() {
// Get JAR path from environment variable or use default
const jarPath = process.env.FHIR_VALIDATOR_JAR_PATH || './validator_cli.jar';
console.log(`Using validator JAR path: ${jarPath}`);
// Initialize the validator with path to validator.jar
const validator = new FhirValidator(jarPath);
try {
// Example: Check for updates and download if needed (without starting service)
console.log('Checking for validator updates...');
const downloadResult = await validator.ensureValidator();
console.log(`Validator version: ${downloadResult.version}`);
console.log(`Downloaded: ${downloadResult.downloaded}`);
console.log(`Updated: ${downloadResult.updated}`);
// Start the validator service
// autoDownload: true (default) will automatically download/update the JAR
// skipUpdateCheck: true skips the GitHub API call if JAR already exists
await validator.start({
version: '5.0.0',
txServer: 'http://tx.fhir.org/r5',
txLog: './txlog.txt',
igs: [
'hl7.fhir.us.core#6.0.0',
'hl7.fhir.uv.sdc#3.0.0'
],
port: 8080,
timeout: 60000, // Wait up to 60 seconds for startup
autoDownload: true, // Automatically download/update JAR (default)
skipUpdateCheck: true // Skip update check since we just checked above
});
console.log('Validator service started successfully');
// Example 1: Validate a JSON resource string
const patientJson = `{
"resourceType": "Patient",
"id": "example",
"active": true,
"name": [{
"use": "official",
"family": "Doe",
"given": ["John"]
}],
"gender": "male",
"birthDate": "1974-12-25"
}`;
console.log('\\nValidating Patient resource...');
const result1 = await validator.validate(patientJson);
console.log('Validation result:', JSON.stringify(result1, null, 2));
// Example 2: Validate with specific profiles
console.log('\\nValidating with US Core Patient profile...');
const result2 = await validator.validate(patientJson, {
profiles: ['http://hl7.org/fhir/us/core/StructureDefinition/us-core-patient'],
resourceIdRule: 'OPTIONAL',
anyExtensionsAllowed: true,
bpWarnings: 'Warning',
displayOption: 'Check'
});
console.log('Profile validation result:', JSON.stringify(result2, null, 2));
// Example 3: Validate a resource object
const observationObject = {
resourceType: 'Observation',
id: 'example-obs',
status: 'final',
category: [{
coding: [{
system: 'http://terminology.hl7.org/CodeSystem/observation-category',
code: 'vital-signs'
}]
}],
code: {
coding: [{
system: 'http://loinc.org',
code: '85354-9',
display: 'Blood pressure panel with all children optional'
}]
},
subject: {
reference: 'Patient/example'
},
effectiveDateTime: '2023-01-01T10:00:00Z',
valueQuantity: {
value: 120,
unit: 'mmHg',
system: 'http://unitsofmeasure.org',
code: 'mm[Hg]'
}
};
console.log('\\nValidating Observation object...');
const result3 = await validator.validateObject(observationObject);
console.log('Object validation result:', JSON.stringify(result3, null, 2));
// Example 4: Validate bytes (useful for file uploads)
const xmlResource = `<?xml version="1.0" encoding="UTF-8"?>
<Patient xmlns="http://hl7.org/fhir">
<id value="xml-example"/>
<active value="true"/>
<name>
<use value="official"/>
<family value="Smith"/>
<given value="Jane"/>
</name>
<gender value="female"/>
<birthDate value="1980-05-15"/>
</Patient>`;
console.log('\\nValidating XML resource from bytes...');
const xmlBytes = Buffer.from(xmlResource, 'utf8');
const result4 = await validator.validateBytes(xmlBytes, 'xml');
console.log('XML validation result:', JSON.stringify(result4, null, 2));
// Example 5: Load additional IG at runtime
console.log('\\nLoading additional Implementation Guide...');
const igResult = await validator.loadIG('hl7.fhir.uv.ips', '1.1.0');
console.log('IG load result:', JSON.stringify(igResult, null, 2));
// Example 6: Error handling
try {
console.log('\\nTesting error handling with invalid resource...');
const invalidResource = '{ "resourceType": "InvalidType" }';
await validator.validate(invalidResource);
} catch (error) {
console.log('Expected validation error:', error.message);
}
} catch (error) {
console.error('Error:', error.message);
} finally {
// Always stop the validator service when done
console.log('\\nStopping validator service...');
await validator.stop();
console.log('Validator service stopped');
}
}
// Alternative example: Just download/update without starting service
async function downloadOnly() {
const jarPath = process.env.FHIR_VALIDATOR_JAR_PATH || './validator_cli.jar';
const validator = new FhirValidator(jarPath);
console.log('Checking for latest FHIR validator...');
try {
// Check what's available
const latest = await validator.getLatestRelease();
console.log(`Latest version available: ${latest.version}`);
console.log(`Published: ${latest.publishedAt}`);
// Check what's installed
const installed = validator.getInstalledVersion();
if (installed) {
console.log(`Currently installed: ${installed}`);
} else {
console.log('No validator currently installed');
}
// Download/update if needed
const result = await validator.ensureValidator();
if (result.downloaded) {
if (result.updated) {
console.log(`Updated from ${installed} to ${result.version}`);
} else {
console.log(`Downloaded version ${result.version}`);
}
} else {
console.log(`Already up to date (${result.version})`);
}
} catch (error) {
console.error('Error:', error.message);
}
}
// Handle process termination gracefully
process.on('SIGINT', async () => {
console.log('\\nReceived SIGINT, shutting down gracefully...');
process.exit(0);
});
process.on('SIGTERM', async () => {
console.log('\\nReceived SIGTERM, shutting down gracefully...');
process.exit(0);
});
// Run the appropriate example based on command line args
const args = process.argv.slice(2);
if (args.includes('--download-only')) {
downloadOnly().catch(console.error);
} else {
main().catch(console.error);
}