package
Generate JSON Schema from TypeScript exports
npm install ts2schemaGitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Generate JSON Schema from TypeScript exports
npm install ts2schema// file.ts
export const testUserId = 123;
/** add number */
export const add = async (num1: number, num2?: number) => num1 + (num2 ?? 0);
/** say hi to user */
export const sayHi = (name: string) => `Hi, ${name}`;
export const makeUser = (user: {
/** Name of user */ name: string;
/** Age of user */ age?: number;
}) => user;import { generateSchemas } from 'ts2schema';
const schemas = generateSchemas('absolute/path/to/your/file.ts');
console.log(schemas);
/* Logs:
{
nonFns: {
testUserId: {
type: 'number',
enum: [123],
$schema: 'http://json-schema.org/draft-07/schema#',
},
},
unaryFns: ['makeUser'],
fns: {
add: {
type: 'object',
properties: {
params: {
type: 'object',
properties: {
num1: { type: 'number', title: 'num1' },
num2: { type: 'number', title: 'num2' },
},
required: ['num1'],
},
The unaryFns values are to track the "flavor" of a function. Consider these functions which wind up with similar params schemas:
export const addV1 = (a: number, b: number) => a + b;
export const addV2 = ({ a, b }: { a: number; b: number }) => a + b;
export const sayHiV1 = (name: string) => `Hi, ${name}`;
export const sayHiV2 = ({ name }: { name: string }) => `Hi, ${name}`;The unaryFns will be ['addV2', 'sayHiV2']. This let's us know how to invoke the functions. See this test for an example.
Estimated unpacked size across 53 packages.
542.7 MB
Includes dependencies.