Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
json-schema-to-openapi-schema
Advanced tools
A little NodeJS package to convert JSON Schema to OpenAPI Schema Objects.
type: ['foo', 'null']
to type: foo
and nullable: true
allOf
s etc.patternProperties
to x-patternProperties
dependencies
to an allOf + oneOf OpenAPI-valid equivalentnpm install --save json-schema-to-openapi-schema
Here's a small example to get the idea:
const toOpenApi = require('json-schema-to-openapi-schema');
const schema = {
'$schema': 'http://json-schema.org/draft-04/schema#',
type: ['string', 'null'],
format: 'date-time',
};
const convertedSchema = toOpenApi(schema);
console.log(convertedSchema);
The example prints out
{
type: 'string',
format: 'date-time',
nullable: true
}
NOTE: $ref
s are not dereferenced. Use a dereferencer such as json-schema-ref-parser prior to using this package.
The function accepts options
object as the second argument.
cloneSchema
(boolean)If set to false
, converts the provided schema in place. If true
, clones the schema by converting it to JSON and back. The overhead of the cloning is usually negligible. Defaults to true
.
OpenAPI is often described as an extension of JSON Schema, but both specs have changed over time and grown independently. OpenAPI v2 was based on JSON Schema draft v4 with a long list of deviations, but OpenAPI v3 shrank that list, upping their support to draft v4 and making the list of discrepancies shorter. Despite OpenAPI v3 closing the gap, the issue of JSON Schema divergence has not been resolved fully.
Carefully writing JSON Schema for your data model kiiiinda works, but it is possible to write JSON Schema that creates invalid OpenAPI, and vice versa. For more on this, read the article OpenAPI and JSON Schema Divergence.
This tool sets out to allow folks to convert from JSON Schema (their one source of truth for everything) to OpenAPI (a thing for HTML docs and making SDKs).
† Draft v5 is also known as Draft Wright 00, as the drafts are often named after the author, and this was the first one by A. Wright. Amongst other things, draft v5 aimed to rewrite the meta files, but the experiment failed, meaning we need to continue to use the draft v4 metafiles. Ugh.
To convert the other way, check out openapi-schema-to-json-schema, which this package was based on.
To run the test-suite:
npm test
FAQs
Converts a JSON Schema to OpenAPI Schema Object
We found that json-schema-to-openapi-schema demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 48 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 a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.