Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to exclude underscore properties from json schema ? #1913

Open
jy95 opened this issue Apr 11, 2024 · 1 comment
Open

How to exclude underscore properties from json schema ? #1913

jy95 opened this issue Apr 11, 2024 · 1 comment

Comments

@jy95
Copy link

jy95 commented Apr 11, 2024

Hello,

I would like to exclude properties that starts with a underscore which pollute a type I don't have any control on

// npm install -D @types/fhir
import type { Dosage as DosageR4 } from "fhir/r4";

type DeepOmit<T, K extends string> = {
  [P in keyof T as Exclude<P, `${K}${string}`>]: T[P] extends object ? DeepOmit<T[P], K> : T[P];
};

export type Entry = DeepOmit<DosageR4, "_">;

It works in Typescript
image

But not in the resulting json schema
image

Do you have an idea on how to achieve this using ts-json-schema-generator ?

Thanks for the help

@jy95
Copy link
Author

jy95 commented Apr 11, 2024

Currently, I use a workaround on the generated schema that I don't find performant-friendly :

function removeUnderscoreProperties(obj) {
  for (var prop in obj) {
      if (prop.startsWith('_')) {
          delete obj[prop];
      } else if (typeof obj[prop] === 'object') {
          removeUnderscoreProperties(obj[prop]);
      }
  }
}

function stringifyWithoutUnderscore(obj) {
  var newObj = {... obj};
  removeUnderscoreProperties(newObj);
  return JSON.stringify(newObj, null, 2);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants