Skip to content

Commit

Permalink
Add guard for requestBody
Browse files Browse the repository at this point in the history
Ref: #7
  • Loading branch information
projkov committed Sep 11, 2024
1 parent beca2f4 commit d4d790d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
19 changes: 15 additions & 4 deletions packages/client-testing-demo-tests-standard/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export function patientDemoTest() {
describe('Patients test (2nd version)', () => {
let requests: Request[] = [];
beforeAll(async () => {
requests = global.requests.filter((request) => request.resourceType === 'Patient' && request.session.id === global.SESSION_ID);
requests = global.requests.filter(
(request) => request.resourceType === 'Patient' && request.session.id === global.SESSION_ID,
);
});

test('Should only have available interactions', async () => {
Expand Down Expand Up @@ -37,8 +39,17 @@ export function patientDemoTest() {
});

test('Should only have available combo search params', async () => {
const availableComboSearchParams = new Set(['birthdate+family', 'birthdate+name', 'gender+name', 'family+gender']);
const filteredRequests = new Set(requests.filter((request) => request.filtersCodes.length > 1).map((request) => request.filtersCodes.join('+')));
const availableComboSearchParams = new Set([
'birthdate+family',
'birthdate+name',
'gender+name',
'family+gender',
]);
const filteredRequests = new Set(
requests
.filter((request) => request.filtersCodes.length > 1)
.map((request) => request.filtersCodes.join('+')),
);

expect(filteredRequests.isSubsetOf(availableComboSearchParams)).toBe(true);
});
Expand All @@ -48,7 +59,7 @@ export function patientDemoTest() {
(request) => request.fhirAction === 'CREATE' && request.requestBody?.resourceType === 'Patient',
);
const validationStatuses = await Promise.all(
filteredRequests.map(async (request) => await isResourceValid(request.requestBody as Patient)),
filteredRequests.map(async (request) => await isResourceValid(request.requestBody)),
);
const falseValidations = validationStatuses.filter((status) => status === false);

Expand Down
9 changes: 7 additions & 2 deletions packages/fhir-validator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ export async function validateResource(ig: string, version: string, txServer: st
return response.data;
}

export async function isResourceValid(resource: FhirResource): Promise<boolean> {
export async function isResourceValid(data?: FhirResource | any): Promise<boolean> {
if (data?.resourceType) {
console.error('Only FHIR resources can be validated');
return false;
}

const { IG, IG_VERSION, TX_SERVER, VALIDATOR_SENSITIVITY } = getValidatorEnvs();
try {
const result = await validateResource(IG, IG_VERSION, TX_SERVER, JSON.stringify(resource));
const result = await validateResource(IG, IG_VERSION, TX_SERVER, JSON.stringify(data));
return result.outcomes?.[0]?.issues.filter((issue) => VALIDATOR_SENSITIVITY.includes(issue.level)).length === 0;
} catch (error) {
console.error('Error validating resource', error);
Expand Down

0 comments on commit d4d790d

Please sign in to comment.