├── .github
└── workflows
│ └── main.yml
├── .gitignore
├── .npmignore
├── .vscode
└── settings.json
├── LICENSE
├── README.md
├── assets
├── header-large.jpg
├── header-round-large.png
├── header-round-medium.png
└── logo.ai
├── babel.config.js
├── package.json
├── src
├── definitions
│ ├── index.ts
│ └── jsonSchema6.ts
├── index.ts
├── meta-types
│ ├── any.ts
│ ├── array.ts
│ ├── const.ts
│ ├── enum.ts
│ ├── error.ts
│ ├── index.ts
│ ├── intersection
│ │ ├── array.ts
│ │ ├── const.ts
│ │ ├── enum.ts
│ │ ├── index.ts
│ │ ├── object.ts
│ │ ├── primitive.ts
│ │ ├── tuple.ts
│ │ └── union.ts
│ ├── never.ts
│ ├── object.ts
│ ├── primitive.ts
│ ├── tuple.ts
│ └── union.ts
├── parse-schema
│ ├── allOf.ts
│ ├── anyOf.ts
│ ├── array.ts
│ ├── const.ts
│ ├── enum.ts
│ ├── index.ts
│ ├── mixed.ts
│ ├── object.ts
│ ├── oneOf.ts
│ └── utils.ts
└── utils
│ ├── and.ts
│ ├── concat.ts
│ ├── extends.ts
│ ├── filter.ts
│ ├── get.ts
│ ├── hasKeyIn.ts
│ ├── head.ts
│ ├── index.ts
│ ├── merge.ts
│ ├── optionalProps.ts
│ ├── prepend.ts
│ ├── readonly.ts
│ ├── replace.ts
│ ├── requiredProps.ts
│ ├── reverse.ts
│ ├── tail.ts
│ └── writeable.ts
├── tests
├── e2e
│ ├── allOf.test.ts
│ ├── anyOf.test.ts
│ ├── array.test.ts
│ ├── boolean.test.ts
│ ├── const.test.ts
│ ├── enum.test.ts
│ ├── integer.test.ts
│ ├── mixed.test.ts
│ ├── noSchema.test.ts
│ ├── null.test.ts
│ ├── number.test.ts
│ ├── object.test.ts
│ ├── oneOf.test.ts
│ └── string.test.ts
├── meta-types
│ ├── any.ts
│ ├── array.ts
│ ├── const.ts
│ ├── enum.ts
│ ├── error.ts
│ ├── intersection
│ │ ├── array.ts
│ │ ├── const.ts
│ │ ├── enum.ts
│ │ ├── helpers.ts
│ │ ├── index.ts
│ │ ├── object.ts
│ │ ├── primitive.ts
│ │ ├── tuple.ts
│ │ └── union.ts
│ ├── never.ts
│ ├── object.ts
│ ├── tuple.ts
│ └── union.ts
└── utils
│ └── concat.ts
├── tsconfig.json
└── yarn.lock
/.github/workflows/main.yml:
--------------------------------------------------------------------------------
1 | # This is a basic workflow to help you get started with Actions
2 |
3 | name: CI
4 |
5 | on:
6 | push:
7 | workflow_dispatch:
8 |
9 | jobs:
10 | test:
11 | # The type of runner that the job will run on
12 | runs-on: ubuntu-latest
13 | steps:
14 | - uses: actions/checkout@v2
15 | - name: Set up
16 | run: yarn
17 | - name: Tests
18 | run: yarn test
19 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | lib
3 |
--------------------------------------------------------------------------------
/.npmignore:
--------------------------------------------------------------------------------
1 | .github
2 | .vscode
3 | assets
4 | node_modules
5 | src
6 | babel.config.js
7 | tsconfig.json
8 | yarn.lock
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "editor.formatOnSave": true,
3 | "editor.defaultFormatter": "esbenp.prettier-vscode"
4 | }
5 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Thomas Aribart
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | If you use this repo, star it ✨ 5 |
6 | 7 | # Stop typing twice 🙅♂️ 8 | 9 | A lot of projects use JSON schemas for runtime data validation along with TypeScript for static type checking. 10 | 11 | Their code may look like this: 12 | 13 | ```typescript 14 | const dogSchema = { 15 | type: "object", 16 | properties: { 17 | name: { type: "string" }, 18 | age: { type: "integer" }, 19 | hobbies: { type: "array", items: { type: "string" } }, 20 | favoriteFood: { enum: ["pizza", "taco", "fries"] }, 21 | }, 22 | required: ["name", "age"], 23 | }; 24 | 25 | type Dog = { 26 | name: string; 27 | age: number; 28 | hobbies?: string[]; 29 | favoriteFood?: "pizza" | "taco" | "fries"; 30 | }; 31 | ``` 32 | 33 | Both objects carry similar if not exactly the same information. This is a code duplication that can annoy developers and introduce bugs if not properly maintained. 34 | 35 | That's when `json-schema-to-ts` comes to the rescue 💪 36 | 37 | ## FromSchema 38 | 39 | The `FromSchema` method lets you infer TS types directly from JSON schemas: 40 | 41 | ```typescript 42 | import { FromSchema } from "json-schema-to-ts"; 43 | 44 | const dogSchema = { 45 | type: "object", 46 | properties: { 47 | name: { type: "string" }, 48 | age: { type: "integer" }, 49 | hobbies: { type: "array", items: { type: "string" } }, 50 | favoriteFood: { enum: ["pizza", "taco", "fries"] }, 51 | }, 52 | required: ["name", "age"], 53 | } as const; 54 | 55 | type Dog = FromSchema