Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

openapi-ts-json-schema

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-ts-json-schema

Convert OpenApi definitions to TS JSON schema files

  • 0.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3K
increased by6.09%
Maintainers
1
Weekly downloads
 
Created
Source

openapi-ts-json-schema

Generate JSON schemas 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:

Installation

npm i openapi-ts-json-schema -D

Usage

import { openapiToTsJsonSchema } from 'openapi-ts-json-schema';

const { outputFolder } = await openapiToTsJsonSchema({
  openApiSchema: path.resolve(fixtures, 'path/to/my/specs.yaml'),
  definitionPathsToGenerateFrom: ['paths', 'components.schemas'],
});

...then in your code:

import Ajv from 'ajv';
import { FromSchema } from 'json-schema-to-ts';
import myModelSchema from './definitions/schemas-autogenerated/MyModel.ts';

// Use generated JSON schemas
const ajv = new Ajv();
const valid = ajv.validate(myModelSchema, {});

// Infer types from generated JSON schemas
type MyModel = FromSchema<typeof myModelSchema>;

Notes

Generated JSON schemas folders get currently saved in a schemas-autogenerated folder next to the provided OpenAPI definition file.

Generated JSON schemas folders name gets escaped in order to be valid file system names.

Options

PropertyTypeDescriptionDefault
openApiSchema (required)stringPath to the OpenApi file (supports yaml and json)-
definitionPathsToGenerateFromstring[]OpenApi definition object paths to generate the JSON schemas from. Only matching paths will be generated. (Supports dot notation: ["components.schemas"])[]
schemaPatcher(params: { schema: JSONSchema }) => voidDynamically patch generated JSON schemas. The provided function will be invoked against every single JSON schema node.-
silentbooleanDon't console.log user messagesfalse

Todo

  • Consider exposing an option to set the output folder path

FAQs

Package last updated on 19 Jul 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc