Research
Security News
Malicious PyPI Package ‘pycord-self’ Targets Discord Developers with Token Theft and Backdoor Exploit
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
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'
-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: 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>
basic: Read JsDoc annotations to provide schema properties.
extended: Also read @nullable, and @asType annotations.
none: Do not use JsDoc annotations.
-c, --skip-type-check
Skip type checks for better performance.
-s, --strict-tuples
Do not allow additional items on tuples.
-u, --unstable
Do not sort properties.
-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
npm 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 202,911 weekly downloads. As such, ts-json-schema-generator popularity was classified as 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.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.