Gathering detailed insights and metrics for typescript-json-schema
Gathering detailed insights and metrics for typescript-json-schema
Gathering detailed insights and metrics for typescript-json-schema
Gathering detailed insights and metrics for typescript-json-schema
@types/json-schema
TypeScript definitions for json-schema
json-schema-typed
JSON Schema TypeScript definitions with complete inline documentation.
@sinclair/typebox
Json Schema Type Builder with Static Type Resolution for TypeScript
json-schema-to-typescript
compile json schema to typescript typings
Generate json-schema from your Typescript sources
npm install typescript-json-schema
Typescript
Module System
Node Version
NPM Version
92.3
Supply Chain
98
Quality
79.8
Maintenance
100
Vulnerability
98.2
License
TypeScript (99.17%)
JavaScript (0.83%)
Total Downloads
86,073,287
Last Day
88,953
Last Week
389,096
Last Month
1,696,038
Last Year
20,286,693
3,192 Stars
517 Commits
326 Forks
21 Watching
4 Branches
113 Contributors
Minified
Minified + Gzipped
Latest Version
0.65.1
Package Id
typescript-json-schema@0.65.1
Unpacked Size
256.02 kB
Size
56.06 kB
File Count
17
NPM Version
10.8.2
Node Version
22.6.0
Publised On
20 Aug 2024
Cumulative downloads
Total Downloads
Last day
3.8%
88,953
Compared to previous day
Last week
-12%
389,096
Compared to previous week
Last month
5.8%
1,696,038
Compared to previous month
Last year
22.2%
20,286,693
Compared to previous year
Generate json-schemas from your Typescript sources.
npm install typescript-json-schema -g
typescript-json-schema project/directory/tsconfig.json TYPE
To generate files for only some types in tsconfig.json
specify
filenames or globs with the --include
option. This is especially useful for large projects.
In case no tsconfig.json
is available for your project, you can directly specify the .ts files (this in this case we use some built-in compiler presets):
typescript-json-schema "project/directory/**/*.ts" TYPE
The TYPE
can either be a single, fully qualified type or "*"
to generate the schema for all types.
Usage: typescript-json-schema <path-to-typescript-files-or-tsconfig> <type>
Options:
--refs Create shared ref definitions. [boolean] [default: true]
--aliasRefs Create shared ref definitions for the type aliases. [boolean] [default: false]
--topRef Create a top-level ref definition. [boolean] [default: false]
--titles Creates titles in the output schema. [boolean] [default: false]
--defaultProps Create default properties definitions. [boolean] [default: false]
--noExtraProps Disable additional properties in objects by default. [boolean] [default: false]
--propOrder Create property order definitions. [boolean] [default: false]
--required Create required array for non-optional properties. [boolean] [default: false]
--strictNullChecks Make values non-nullable by default. [boolean] [default: false]
--esModuleInterop Use esModuleInterop when loading typescript modules. [boolean] [default: false]
--skipLibCheck Use skipLibCheck when loading typescript modules. [boolean] [default: false]
--useTypeOfKeyword Use `typeOf` keyword (https://goo.gl/DC6sni) for functions. [boolean] [default: false]
--out, -o The output file, defaults to using stdout
--validationKeywords Provide additional validation keywords to include [array] [default: []]
--include Further limit tsconfig to include only matching files [array] [default: []]
--ignoreErrors Generate even if the program has errors. [boolean] [default: false]
--excludePrivate Exclude private members from the schema [boolean] [default: false]
--uniqueNames Use unique names for type symbols. [boolean] [default: false]
--rejectDateType Rejects Date fields in type definitions. [boolean] [default: false]
--id Set schema id. [string] [default: ""]
--defaultNumberType Default number type. [choices: "number", "integer"] [default: "number"]
--tsNodeRegister Use ts-node/register (needed for require typescript files). [boolean] [default: false]
--constAsEnum Use enums with a single value when declaring constants. [boolean] [default: false]
--experimentalDecorators Use experimentalDecorators when loading typescript modules.
[boolean] [default: true]
1import { resolve } from "path";
2
3import * as TJS from "typescript-json-schema";
4
5// optionally pass argument to schema generator
6const settings: TJS.PartialArgs = {
7 required: true,
8};
9
10// optionally pass ts compiler options
11const compilerOptions: TJS.CompilerOptions = {
12 strictNullChecks: true,
13};
14
15// optionally pass a base path
16const basePath = "./my-dir";
17
18const program = TJS.getProgramFromFiles(
19 [resolve("my-file.ts")],
20 compilerOptions,
21 basePath
22);
23
24// We can either get the schema for one file and one type...
25const schema = TJS.generateSchema(program, "MyType", settings);
26
27// ... or a generator that lets us incrementally get more schemas
28
29const generator = TJS.buildGenerator(program, settings);
30
31// generator can be also reused to speed up generating the schema if usecase allows:
32const schemaWithReusedGenerator = TJS.generateSchema(program, "MyType", settings, [], generator);
33
34// all symbols
35const symbols = generator.getUserSymbols();
36
37// Get symbols for different types from generator.
38generator.getSchemaForSymbol("MyType");
39generator.getSchemaForSymbol("AnotherType");
1// In larger projects type names may not be unique,
2// while unique names may be enabled.
3const settings: TJS.PartialArgs = {
4 uniqueNames: true,
5};
6
7const generator = TJS.buildGenerator(program, settings);
8
9// A list of all types of a given name can then be retrieved.
10const symbolList = generator.getSymbols("MyType");
11
12// Choose the appropriate type, and continue with the symbol's unique name.
13generator.getSchemaForSymbol(symbolList[1].name);
14
15// Also it is possible to get a list of all symbols.
16const fullSymbolList = generator.getSymbols();
getSymbols('<SymbolName>')
and getSymbols()
return an array of SymbolRef
, which is of the following format:
1type SymbolRef = { 2 name: string; 3 typeName: string; 4 fullyQualifiedName: string; 5 symbol: ts.Symbol; 6};
getUserSymbols
and getMainFileSymbols
return an array of string
.
The schema generator converts annotations to JSON schema properties.
For example
1export interface Shape { 2 /** 3 * The size of the shape. 4 * 5 * @minimum 0 6 * @TJS-type integer 7 */ 8 size: number; 9}
will be translated to
1{ 2 "$ref": "#/definitions/Shape", 3 "$schema": "http://json-schema.org/draft-07/schema#", 4 "definitions": { 5 "Shape": { 6 "properties": { 7 "size": { 8 "description": "The size of the shape.", 9 "minimum": 0, 10 "type": "integer" 11 } 12 }, 13 "type": "object" 14 } 15 } 16}
Note that we needed to use @TJS-type
instead of just @type
because of an issue with the typescript compiler.
You can also override the type of array items, either listing each field in its own annotation or one annotation with the full JSON of the spec (for special cases). This replaces the item types that would have been inferred from the TypeScript type of the array elements.
Example:
1export interface ShapesData { 2 /** 3 * Specify individual fields in items. 4 * 5 * @items.type integer 6 * @items.minimum 0 7 */ 8 sizes: number[]; 9 10 /** 11 * Or specify a JSON spec: 12 * 13 * @items {"type":"string","format":"email"} 14 */ 15 emails: string[]; 16}
Translation:
1{ 2 "$ref": "#/definitions/ShapesData", 3 "$schema": "http://json-schema.org/draft-07/schema#", 4 "definitions": { 5 "Shape": { 6 "properties": { 7 "sizes": { 8 "description": "Specify individual fields in items.", 9 "items": { 10 "minimum": 0, 11 "type": "integer" 12 }, 13 "type": "array" 14 }, 15 "emails": { 16 "description": "Or specify a JSON spec:", 17 "items": { 18 "format": "email", 19 "type": "string" 20 }, 21 "type": "array" 22 } 23 }, 24 "type": "object" 25 } 26 } 27}
This same syntax can be used for contains
and additionalProperties
.
integer
type aliasIf you create a type alias integer
for number
it will be mapped to the integer
type in the generated JSON schema.
Example:
1type integer = number; 2interface MyObject { 3 n: integer; 4}
Note: this feature doesn't work for generic types & array types, it mainly works in very simple cases.
require
a variable from a file(for requiring typescript files is needed to set argument tsNodeRegister
to true)
When you want to import for example an object or an array into your property defined in annotation, you can use require
.
Example:
1export interface InnerData { 2 age: number; 3 name: string; 4 free: boolean; 5} 6 7export interface UserData { 8 /** 9 * Specify required object 10 * 11 * @examples require("./example.ts").example 12 */ 13 data: InnerData; 14}
file example.ts
1export const example: InnerData[] = [{ 2 age: 30, 3 name: "Ben", 4 free: false 5}]
Translation:
1{ 2 "$schema": "http://json-schema.org/draft-07/schema#", 3 "properties": { 4 "data": { 5 "description": "Specify required object", 6 "examples": [ 7 { 8 "age": 30, 9 "name": "Ben", 10 "free": false 11 } 12 ], 13 "type": "object", 14 "properties": { 15 "age": { "type": "number" }, 16 "name": { "type": "string" }, 17 "free": { "type": "boolean" } 18 }, 19 "required": ["age", "free", "name"] 20 } 21 }, 22 "required": ["data"], 23 "type": "object" 24}
Also you can use require(".").example
, which will try to find exported variable with name 'example' in current file. Or you can use require("./someFile.ts")
, which will try to use default exported variable from 'someFile.ts'.
Note: For examples
a required variable must be an array.
Inspired and builds upon Typson, but typescript-json-schema is compatible with more recent Typescript versions. Also, since it uses the Typescript compiler internally, more advanced scenarios are possible. If you are looking for a library that uses the AST instead of the type hierarchy and therefore better support for type aliases, have a look at vega/ts-json-schema-generator.
npm run debug -- test/programs/type-alias-single/main.ts --aliasRefs true MyString
And connect via the debugger protocol.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 18/30 approved changesets -- score normalized to 6
Reason
7 existing vulnerabilities detected
Details
Reason
1 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-01-27
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More