Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
openapi-ts-json-schema
Advanced tools
Generate JSON schema TypeScript files (.ts
) from OpenAPI definitions which can be natively used to infer types from (with json-schema-to-ts
or fastify-type-provider-json-schema-to-ts
).
Given an OpenAPI definition file, openapi-ts-json-schema
will:
$ref
s and dereference them with @apidevtools/json-schema-ref-parser
$ref
s@openapi-contrib/openapi-schema-to-json-schema
and openapi-jsonschema-parameters
.ts
files with as const
assertion)npm i openapi-ts-json-schema -D
import { openapiToTsJsonSchema } from 'openapi-ts-json-schema';
const { outputPath } = await openapiToTsJsonSchema({
openApiSchema: 'path/to/open-api-specs.yaml',
definitionPathsToGenerateFrom: ['paths', 'components.schemas'],
});
...then in your code:
import Ajv from 'ajv';
import { FromSchema } from 'json-schema-to-ts';
import myModelSchema from 'path/to/schemas-autogenerated/components/schemas/MyModel.ts';
// Use generated JSON schemas as usual
const ajv = new Ajv();
const valid = ajv.validate(myModelSchema, { hello: 'World' });
// Use generated JSON schemas to infer model types from
type MyModel = FromSchema<typeof myModelSchema>;
Property | Type | Description | Default |
---|---|---|---|
openApiSchema (required) | string | Path to the OpenApi file (supports yaml and json). | - |
definitionPathsToGenerateFrom (required) | string[] | OpenApi definition object paths to generate the JSON schemas from. Only matching paths will be generated. (Supports dot notation: ["components.schemas"] ). | - |
refHandling | "inline" | "import" | "keep" | "inline" : inline $ref schemas. "import" : generate and import $ref schemas."keep" : keep $ref value. | "inline" |
schemaPatcher | (params: { schema: JSONSchema }) => void | Dynamically patch generated JSON schemas. The provided function will be invoked against every single JSON schema node. | - |
outputPath | string | Path where the generated schemas will be saved. Defaults to /schemas-autogenerated in same directory as provided openApiSchema . | - |
plugins | ReturnType<Plugin>[] | A set of optional plugin to generate any extra custom. See plugins docs. output. | - |
silent | boolean | Don't console.log user messages. | false |
Generated JSON schema path names get escaped in order to be valid file system names.
Circular $ref
s can be technically resolved with "import" refHandling
option. But TS will stop the type recursion and type the schema as any
(error ts(7022)
). See relevant tests.
Take a look at the Developer's notes for a few more in-depth explanations.
Beside generating the expected schema files under outputPath
, openapiToTsJsonSchema
returns the following data:
{
// The path where the schemas are generated
outputPath: string;
metaData: {
// Meta data of the generated schemas
schemas: Map<
// OpenAPI ref. Eg: "#/components/schemas/MySchema"
string,
{
schemaId: string;
// JSON schema Compound Schema Document `$id`
schemaFileName: string;
// Valid filename for given schema (without extension).
schemaAbsoluteDirName: string;
// Absolute path pointing to schema folder
schemaAbsolutePath: string;
// Absolute path pointing to schema file
schemaAbsoluteImportPath: string;
// Absolute import path (without extension)
schemaUniqueName: string;
// Unique JavaScript identifier used as import name
schema: JSONSchema;
// The actual JSON schema
isRef: boolean;
// True is schemas is used as a `$ref`
}
>;
}
}
Plugins are intended as a way to generate extra artifacts based on the same internal metadata created to generate the JSON schema output.
openapi-ts-json-schema
currently ships with one plugin specifically designed to better integrate with Fastify, but you can write your own.
Read plugins documentation 📖.
definitionPathsToGenerateFrom
option in favour of exporting the whole OpenAPI definitions based on the structure defined in specs0.4.0
experimentalImportRefs
option in favour of refHandling
metaData
return property holding generated schemas meta dataplugins
optionrefHandling
option, to preserve $ref objectsfastifyIntegrationPlugin
pluginisRef
meta data prop for inline refHandling
option$ref
s resolutionFAQs
OpenAPI to JSON schema generator with TypeScript in mind
The npm package openapi-ts-json-schema receives a total of 1,054 weekly downloads. As such, openapi-ts-json-schema popularity was classified as popular.
We found that openapi-ts-json-schema demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.