Ahryman40k / typescript-fhir-types

Typescript / Javascript object model for FHIR standard
MIT License
126 stars 27 forks source link

RTTI_Patient not decoding birthDate #24

Closed catel977 closed 3 years ago

catel977 commented 3 years ago

I'm trying to decode birthDate with the RTTI_Patient class, and it is not working.

Given this input: { "resourceType": "DiagnosticReport", "id": "d9d19378-8f55-4641-8462-b8f35cbc9dd3", "text": { "status": "generated", "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><p><b>Pulmonary nodule narrative</b></p><p>small masses of tissue seen in lung</p></div>" }, "contained": [{ "resourceType": "Patient", "id": "patient1", "birthDate" : "2020-01-01", "identifier": [{ "use": "usual", "type": { "coding": [{ "system": "http://terminology.hl7.org/CodeSystem/v2-0203", "code": "MR" }] }, "value": "anonymous" }] }, { "resourceType": "ImagingStudy", "id": "study1", "status": "available", "identifier": [{ "use": "usual", "type": { "coding": [{ "system": "http://terminology.hl7.org/CodeSystem/v2-0203", "code": "ACSN" }] }, "value": "758843" }], "subject": { "reference": "#patient1" }, "procedureCode": [{ "coding": [{ "system": "http://loinc.org", "code": "79068-3", "display": "CT Chest w IV Contrast Screening" }], "text": "CT Chest" }] }], "imagingStudy": [{ "reference": "#study1" }], "status": "final", "category": [{ "coding": [{ "system": "http://terminology.hl7.org/CodeSystem/v2-0074", "code": "RAD", "display": "Radiology" }] }], "code": { "coding": [{ "system": "http://loinc.org", "code": "68604-8", "display": "Radiology Diagnostic study note" }] }, "subject": { "reference": "#ai-algorithm-details" }, "effectiveDateTime": "2018-04-13T12:00:00-05:00", "conclusion": "lung nodule seen." }

I am getting this error: Invalid value undefined supplied to : IPatient/0: { resourceType: "Patient" }/resourceType: "Patient".

If I remove birthDate from the Patient object, it decodes fine.

Ahryman40k commented 3 years ago

I don't know how you did but, It must fail in all case because => "resourceType": "DiagnosticReport" instead of "Patient" Maybe DiagnoticReport own the exact same properties except birthDate.

You can check your resources that simple way:

const obj = {
  resourceType: "DiagnosticReport",
  id: "d9d19378-8f55-4641-8462-b8f35cbc9dd3",
  text: {
    status: "generated",
    div:
      '<div xmlns="http://www.w3.org/1999/xhtml"><p><b>Pulmonary nodule narrative</b></p><p>small masses of tissue seen in lung</p></div>',
  },
  contained: [
    {
      resourceType: "Patient",
      id: "patient1",
      birthDate: "2020-01-01",
      identifier: [
        {
          use: "usual",
          type: {
            coding: [
              {
                system: "http://terminology.hl7.org/CodeSystem/v2-0203",
                code: "MR",
              },
            ],
          },
          value: "anonymous",
        },
      ],
    },
    {
      resourceType: "ImagingStudy",
      id: "study1",
      status: "available",
      identifier: [
        {
          use: "usual",
          type: {
            coding: [
              {
                system: "http://terminology.hl7.org/CodeSystem/v2-0203",
                code: "ACSN",
              },
            ],
          },
          value: "758843",
        },
      ],
      subject: { reference: "#patient1" },
      procedureCode: [
        {
          coding: [
            {
              system: "http://loinc.org",
              code: "79068-3",
              display: "CT Chest w IV Contrast Screening",
            },
          ],
          text: "CT Chest",
        },
      ],
    },
  ],
  imagingStudy: [{ reference: "#study1" }],
  status: "final",
  category: [
    {
      coding: [
        {
          system: "http://terminology.hl7.org/CodeSystem/v2-0074",
          code: "RAD",
          display: "Radiology",
        },
      ],
    },
  ],
  code: {
    coding: [
      {
        system: "http://loinc.org",
        code: "68604-8",
        display: "Radiology Diagnostic study note",
      },
    ],
  },
  subject: { reference: "#ai-algorithm-details" },
  effectiveDateTime: "2018-04-13T12:00:00-05:00",
  conclusion: "lung nodule seen.",
};

import { R4 } from  './index';

// validation succeeded
const  validationResult = R4.RTTI_Observation.decode( obj ) // => Right if good, Left if not
console.log( validationResult )

then run you file with ts-node.

ValidationResult contains all information saying where errors are in your json: Here it comes from 'DiagnosticReport'

Left {
  value: [
    { value: 'DiagnosticReport', context: [Array], message: undefined }
  ],
  _tag: 'Left'
}

Hope it helps

Ahryman40k commented 3 years ago

You can grab information on that part from io-ts library I use to check objects at runtime