Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
ts-json-schema-generator
Advanced tools
The ts-json-schema-generator package is a tool that generates JSON schemas from TypeScript types. It is useful for ensuring that your TypeScript types and JSON schemas stay in sync, which can be particularly helpful for validating JSON data against TypeScript interfaces or types.
Generate JSON Schema from TypeScript Interface
This feature allows you to generate a JSON schema from a TypeScript interface. You need to specify the path to your TypeScript file and the type you want to generate the schema for.
const { createGenerator } = require('ts-json-schema-generator');
const config = {
path: 'path/to/your/file.ts',
tsconfig: 'path/to/your/tsconfig.json',
type: '*', // Or specify a particular type
};
const schema = createGenerator(config).createSchema(config.type);
console.log(JSON.stringify(schema, null, 2));
Generate JSON Schema for Specific Type
This feature allows you to generate a JSON schema for a specific TypeScript type. You need to specify the path to your TypeScript file and the type you want to generate the schema for.
const { createGenerator } = require('ts-json-schema-generator');
const config = {
path: 'path/to/your/file.ts',
tsconfig: 'path/to/your/tsconfig.json',
type: 'MyType', // Specify the type you want to generate the schema for
};
const schema = createGenerator(config).createSchema(config.type);
console.log(JSON.stringify(schema, null, 2));
Generate JSON Schema with Custom Settings
This feature allows you to generate a JSON schema with custom settings. You can specify various options such as exposing all types, referencing the top-level schema, and using extended JSDoc annotations.
const { createGenerator } = require('ts-json-schema-generator');
const config = {
path: 'path/to/your/file.ts',
tsconfig: 'path/to/your/tsconfig.json',
type: 'MyType',
expose: 'all', // Expose all types
topRef: true, // Reference the top-level schema
jsDoc: 'extended', // Use extended JSDoc annotations
};
const schema = createGenerator(config).createSchema(config.type);
console.log(JSON.stringify(schema, null, 2));
The typescript-json-schema package generates JSON schemas from TypeScript types. It is similar to ts-json-schema-generator but offers different configuration options and may have different performance characteristics.
The json-schema-to-typescript package converts JSON schemas to TypeScript interfaces. While it performs the reverse operation of ts-json-schema-generator, it is often used in conjunction with it to ensure consistency between JSON schemas and TypeScript types.
The ajv package is a JSON schema validator that can be used to validate JSON data against JSON schemas. While it does not generate schemas from TypeScript types, it is often used alongside ts-json-schema-generator to validate data against the generated schemas.
Extended version of https://github.com/xiag-ag/typescript-to-json-schema.
Inspired by YousefED/typescript-json-schema
. Here's the differences list:
typeChecker.getTypeAtLocation()
(so probably it keeps correct type aliases)definitions
section in the JSON schemaThis project is made possible by a community of contributors. We welcome contributions of any kind (issues, code, documentation, examples, tests,...). Please read our code of conduct.
npm install --save ts-json-schema-generator
./node_modules/.bin/ts-json-schema-generator --path 'my/project/**.*.ts' --type 'My.Type.Full.Name'
-p, --path 'index.ts'
The path to the TypeScript source file. If this is not provided, the type will be searched in the project specified in the `.tsconfig`.
-t, --type 'My.Type.Full.Name'
The type the generated schema will represent. If omitted, the generated schema will contain all
types found in the files matching path. The same is true if '*' is specified.
-e, --expose <all|none|export>
all: Create shared $ref definitions for all types.
none: Do not create shared $ref definitions.
export (default): Create shared $ref definitions only for exported types.
-f, --tsconfig 'my/project/tsconfig.json'
Use a custom tsconfig file for processing typescript (see https://www.typescriptlang.org/docs/handbook/tsconfig-json.html) instead of the default:
{
"compilerOptions": {
"noEmit": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "ES5",
"module": "CommonJS",
"strictNullChecks": false,
}
}
-r, --no-top-ref
Do not create a top-level $ref definition.
-j, --jsDoc <extended|none|basic>
none: Do not use JsDoc annotations.
basic: Read JsDoc annotations to provide schema properties.
extended (default): Also read @nullable, and @asType annotations.
-u, --unstable
Do not sort properties.
-s, --strict-tuples
Do not allow additional items on tuples.
-c, --no-type-check
Skip type checks for better performance.
-k, --validationKeywords
Provide additional validation keywords to include.
interface
typesenum
typesunion
, tuple
, type[]
typesstring
, boolean
, number
types"value"
, 123
, true
, false
, null
, undefined
literalstypeof
keyof
yarn --silent run run --path 'test/valid-data/type-mapped-array/*.ts' --type 'MyObject'
yarn --silent run debug --path 'test/valid-data/type-mapped-array/*.ts' --type 'MyObject'
And connect via the debugger protocol.
AST Explorer is amazing for developers of this tool!
FAQs
Generate JSON schema from your Typescript sources
The npm package ts-json-schema-generator receives a total of 0 weekly downloads. As such, ts-json-schema-generator popularity was classified as not popular.
We found that ts-json-schema-generator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.