Giter Club home page Giter Club logo

Comments (4)

Ahryman40k avatar Ahryman40k commented on June 14, 2024

Sorry for delay, I missed your message.

RTTI means RunTime Type Information. This is an object that describe how your object should be.
With it, you can check if a resource has the desired shape.
It also means RTTI can't be unknown, you should always know what type you are expecting.
for example, does my any object is an IObservation ?

RTTI_Observation.decode(**any**) // this does the job

if your any content json fits an observation, then your response should be 'right', 'left' otherwise.
If you are looking for interfaces, so you can use Ixxx ( IObservation, IPatient, ... ).
I guess you're issue is to know what kind of resource you are using. It's true that an helper method could be present is my library, specially for ResourceList type that merge all FHIR type.
This is where typeguards can be useful. If your not familiar with this part of typescript, I let you learn more about it. Here is a small sample

// Here an interface definition 
interface IA {
  foo: 'IA';
  bar: string; 
}
// Here is a typeguard for IA
function isIA( json: any): json is IA {
  return json.foo === 'IA'
}
// Here is a an any definition. I forced any to prevent typescript to infer 'a' as { foo: string, bar: string }
const a: any = {
  foo: 'IA',
  bar: "A"
}
// Here another any type
const b: any = {
  foo: 'IB',
  other: "B"
}
// Let's test is a an IA
if ( isIA(a) ) {
 // You can see here that a is now type IA and IntelliSense works well with types.
  console.log( a.bar )
} else {
  console.log( 'a is not IA')
}
// Let's do the same with b
// b can't be considered as IA because foo is not of type 'IA' ( Like FHIR  'resourceType' property does )
if ( !isIA(b) ) {
  console.log( 'b is not an IA')
} else {
  console.log( 'b is IB')
}

Hope this helps.
:)

from typescript-fhir-types.

mgramigna avatar mgramigna commented on June 14, 2024

Thanks for the response! Just to clarify, there is no current support in your library for doing RTTI when the FHIR resource type is unknown? i.e. you always need to call the specific interface (like your RTTI_Observation example)

from typescript-fhir-types.

Ahryman40k avatar Ahryman40k commented on June 14, 2024

the library is intended to validate a json as FHIR resource shape.
Generally speaking, you know what that shape is intended to be because you are interrogating a service in purpose.
If you ask for Foo you shouldn't receive Bar. There are specifications for service you use.
The goal of the library is to enforce the typings. meaning if you receive something in json format, it may have a wrong shape because of a bug, typo, wrong spec, etc.
Using a decode method allows you to safely assume an object is right at runtime.

if you really know nothing about your data, there is 2 ways:

  • you can use RTTI_ResourceList.decode(), if your result is right then it means it's a valid FHIR resource but you don't know what resource.
  • If you want to know exactly what resource then you need to test all FHIR resources like this:
// define your typeguard
function isObservation( data: any): data is IObservation {
    const result = RTTI_Observation.decode(data);
    return E.isRight(validationResult) !== undefined; // I didn't test the code. but this is the main idea. If you have a valid resource  you have a valid type
}

function isPatient( data: any): data is IPatient {
    const result = RTTI_Patient.decode(data);
    return E.isRight(validationResult) !== undefined; // I didn't test the code. but this is the main idea. If you have a valid resource  you have a valid type. 
}

const data: any = _your_data_here_
if( isPatient(data) ) { // typeguard
 // safely access patient here
} else if( isObservation(data) ) { // typeguard
 // safely access Observation here
} etc etc

You need typeguard for all resources. It's something I should add in future release.

from typescript-fhir-types.

mgramigna avatar mgramigna commented on June 14, 2024

Thank you. This answers my question

from typescript-fhir-types.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.