{"id":15047004,"url":"https://github.com/jaredce/json-schema-to-openapi-schema-object","last_synced_at":"2025-10-29T21:30:40.776Z","repository":{"id":38107373,"uuid":"494763702","full_name":"JaredCE/json-schema-to-openAPI-schema-object","owner":"JaredCE","description":"Converts a standard JSON Schema to a compatible Open API v3 Schema Object","archived":false,"fork":false,"pushed_at":"2024-07-09T13:19:42.000Z","size":234,"stargazers_count":2,"open_issues_count":7,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-02T04:41:48.388Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/JaredCE.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-21T11:27:17.000Z","updated_at":"2024-07-09T13:18:53.000Z","dependencies_parsed_at":"2024-06-19T06:30:23.161Z","dependency_job_id":null,"html_url":"https://github.com/JaredCE/json-schema-to-openAPI-schema-object","commit_stats":{"total_commits":104,"total_committers":2,"mean_commits":52.0,"dds":0.2596153846153846,"last_synced_commit":"80b6d9abd89f50f9b3fc2f496cd6ae781a489803"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JaredCE%2Fjson-schema-to-openAPI-schema-object","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JaredCE%2Fjson-schema-to-openAPI-schema-object/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JaredCE%2Fjson-schema-to-openAPI-schema-object/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/JaredCE%2Fjson-schema-to-openAPI-schema-object/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/JaredCE","download_url":"https://codeload.github.com/JaredCE/json-schema-to-openAPI-schema-object/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238892293,"owners_count":19548166,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T20:53:51.794Z","updated_at":"2025-10-29T21:30:40.407Z","avatar_url":"https://github.com/JaredCE.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp\u003e\r\n  \u003ca href=\"https://www.npmjs.com/package/json-schema-for-openapi\"\u003e\r\n    \u003cimg src=\"https://img.shields.io/npm/v/json-schema-for-openapi.svg?style=flat-square\"\u003e\r\n  \u003c/a\u003e\r\n  \u003ca href=\"https://github.com/JaredCE/json-schema-to-openAPI-schema-object/actions/workflows/node.js.yml\"\u003e\r\n    \u003cimg src=\"https://github.com/JaredCE/json-schema-to-openAPI-schema-object/actions/workflows/node.js.yml/badge.svg\"\u003e\r\n  \u003c/a\u003e\r\n\u003c/p\u003e\r\n\r\n# json-schema-for-openapi\r\n\r\nConverts a standard [JSON Schema](https://json-schema.org/understanding-json-schema/index.html) to a compatible [OpenAPI v3.0.X Schema Object](https://spec.openapis.org/oas/v3.0.3#schema-object).\r\n\r\nAs of version 0.3.0, it is now advised to run a schema through a de-referencer like: https://apitools.dev/json-schema-ref-parser/ to properly deal with `$ref`. I have removed my own poor implementation of de-referencing JSON schemas since there are libraries that can do it better than I can.\r\n\r\nIt should be noted, that de-referencing libraries have their own issues and might not be able to properly parse your JSON/output a schema you might expect. Due to the way OpenAPI v3.0.X Schema Object's are handled, should the referencing not be 100% correct you might face issues using this library and its output to be used with OpenAPI 3.0.X.\r\n\r\n## Conversions\r\n\r\nThis attempts to massage the standard JSON Schema to a compatible OpenAPI v3.0.X Schema Object. There are many properties that are not supported by OpenAPI v3.0.X Schema Object, though have now been supported by [OpenAPI v3.1.X](https://spec.openapis.org/oas/v3.1.0#schema-object). This library should only be used if working with OpenAPI v3.0.X.\r\n\r\n### Items as an Array to Object\r\n\r\nThis will convert a schema of:\r\n\r\n```json\r\n{\r\n  \"type\": \"object\",\r\n  \"properties\": {\r\n    \"example\": {\r\n      \"type\": \"array\",\r\n      \"items\": [\r\n        {\r\n          \"type\": \"string\"\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nTo:\r\n\r\n```json\r\n{\r\n  \"type\": \"object\",\r\n  \"properties\": {\r\n    \"example\": {\r\n      \"type\": \"array\",\r\n      \"items\": {\r\n        \"type\": \"string\"\r\n      }\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nAt the moment, this library cannot handle more than one item in the array, and so will default to using the first item only.\r\n\r\n### Types in an array to OneOf\r\n\r\nThis will convert a schema of:\r\n\r\n```json\r\n{\r\n  \"type\": \"object\",\r\n  \"properties\": {\r\n    \"example\": {\r\n      \"type\": [\"string\", \"number\"]\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nTo:\r\n\r\n```json\r\n{\r\n  \"type\": \"object\",\r\n  \"properties\": {\r\n    \"example\": {\r\n      \"oneOf\": [\r\n        {\r\n          \"type\": \"string\"\r\n        },\r\n        {\r\n          \"type\": \"number\"\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nWhere an array contains `null`, it will now set `nullable` against all types:\r\n\r\n```json\r\n{\r\n  \"type\": \"object\",\r\n  \"properties\": {\r\n    \"example\": {\r\n      \"type\": [\"string\", \"number\", \"null\"]\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nTo:\r\n\r\n```json\r\n{\r\n  \"type\": \"object\",\r\n  \"properties\": {\r\n    \"example\": {\r\n      \"oneOf\": [\r\n        {\r\n          \"type\": \"string\",\r\n          \"nullable\": true\r\n        },\r\n        {\r\n          \"type\": \"number\",\r\n          \"nullable\": true\r\n        }\r\n      ]\r\n    }\r\n  }\r\n}\r\n```\r\n\r\n### Const to enum\r\n\r\nThis will convert a schema of:\r\n\r\n```json\r\n{\r\n  \"type\": \"object\",\r\n  \"properties\": {\r\n    \"example\": {\r\n      \"type\": \"string\",\r\n      \"const\": \"Surburbia\"\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nTo:\r\n\r\n```json\r\n{\r\n  \"type\": \"object\",\r\n  \"properties\": {\r\n    \"example\": {\r\n      \"type\": \"string\",\r\n      \"enum\": [\"Surburbia\"]\r\n    }\r\n  }\r\n}\r\n```\r\n\r\n### null types\r\n\r\nOpenAPI 3.0.X does not allow for null as a type, so will convert a schema of:\r\n\r\n```json\r\n{\r\n  \"type\": \"object\",\r\n  \"properties\": {\r\n    \"example\": {\r\n      \"type\": \"null\"\r\n    }\r\n  }\r\n}\r\n```\r\n\r\nTo:\r\n\r\n```json\r\n{\r\n  \"type\": \"object\",\r\n  \"properties\": {\r\n    \"example\": {\r\n      \"nullable\": true\r\n    }\r\n  }\r\n}\r\n```\r\n\r\n### Default to their type\r\n\r\nThis will convert the `\"default\":` value to the relevant type. A String to a String, a Number/Integer to a number/Integer, a Boolean to a Boolean and try to manipulate an Object or an Array to either an Object or an Array\r\n\r\n### Dependencies, DependentRequired, DependentSchemas\r\n\r\nThis will try to convert `\"dependencies\":`, `\"dependentRequired\":` and `\"dependentSchemas\":` to a valid `\"allOf\"` in the case of a `\"dependentSchemas\"` or an `\"anyOf\":` schema in the case of a `\"dependentRequired\"`.\r\n\r\n### If/Then/Else\r\n\r\nIt will try to convert an If/Then/Else schema statement to a valid `\"OneOf\"` schema.\r\n\r\n## Installation and Usage:\r\n\r\nInstall via npm: `npm install json-schema-for-openapi`.\r\n\r\nAnd use as a Factory like:\r\n\r\n```js\r\nconst ConvertorFactory = require(\"json-schema-for-openapi\");\r\nconst jsonSchema = {\r\n  $schema: \"http://json-schema.org/draft-04/schema#\",\r\n  title: \"JSON API Schema\",\r\n  description:\r\n    \"This is a schema for responses in the JSON API format. For more, see http://jsonapi.org\",\r\n  type: \"object\",\r\n  properties: {\r\n    errors: {\r\n      type: \"object\",\r\n    },\r\n  },\r\n};\r\nconst convertedSchema = ConvertorFactory.convert(jsonSchema, \"main\");\r\n```\r\n\r\nwhich will output:\r\n\r\n```json\r\n{\r\n  \"schemas\": {\r\n    \"main\": {\r\n      \"title\": \"JSON API Schema\",\r\n      \"description\": \"This is a schema for responses in the JSON API format. For more, see http://jsonapi.org\",\r\n      \"type\": \"object\",\r\n      \"properties\": {\r\n        \"errors\": {\r\n          \"type\": \"object\"\r\n        }\r\n      }\r\n    }\r\n  }\r\n}\r\n```\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaredce%2Fjson-schema-to-openapi-schema-object","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjaredce%2Fjson-schema-to-openapi-schema-object","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjaredce%2Fjson-schema-to-openapi-schema-object/lists"}