ThomasAribart / json-schema-to-ts

Infer TS types from JSON schemas 📝
MIT License
1.43k stars 30 forks source link

Export schema #121

Closed zijam closed 1 year ago

zijam commented 1 year ago

Hi

Is it possible to export schema in typescript to json file as JSON Schema.

Example would like to export dogSchema to json file as JSON Schema format, to be used in another programming language.

const dogSchema = {
  type: "object",
  properties: {
    name: { type: "string" },
    age: { type: "integer" },
    hobbies: { type: "array", items: { type: "string" } },
    favoriteFood: { enum: ["pizza", "taco", "fries"] },
  },
  required: ["name", "age"],
} as const;

type Dog = FromSchema<typeof dogSchema>;
ThomasAribart commented 1 year ago

Hello @zijam,

The short answer is no.

The long answer is that it would rather work the other way around: It would be nice to import a schema from a .json file and infer the type of valid data regarding this schema.

For the moment, it is not possible, but I have high hopes that it will be one day as there is an upvoted TS issue for that. See the FAQ:

https://github.com/ThomasAribart/json-schema-to-ts/blob/master/documentation/FAQs/does-json-schema-to-ts-work-on-json-file-schemas.md