{"id":13431587,"url":"https://github.com/lightsofapollo/joi-to-json-schema","last_synced_at":"2025-05-16T07:03:58.901Z","repository":{"id":25200748,"uuid":"28624502","full_name":"lightsofapollo/joi-to-json-schema","owner":"lightsofapollo","description":null,"archived":false,"fork":false,"pushed_at":"2024-05-15T05:28:59.000Z","size":206,"stargazers_count":140,"open_issues_count":24,"forks_count":36,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-27T08:37:55.860Z","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":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lightsofapollo.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"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":"2014-12-30T09:06:45.000Z","updated_at":"2024-10-01T14:22:59.000Z","dependencies_parsed_at":"2024-11-12T21:00:27.088Z","dependency_job_id":"c2f52547-8002-47b6-8944-858813634c97","html_url":"https://github.com/lightsofapollo/joi-to-json-schema","commit_stats":{"total_commits":111,"total_committers":24,"mean_commits":4.625,"dds":0.7927927927927928,"last_synced_commit":"f9f91927efac9e8f9ab71fb8f67cf5777d296639"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lightsofapollo%2Fjoi-to-json-schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lightsofapollo%2Fjoi-to-json-schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lightsofapollo%2Fjoi-to-json-schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lightsofapollo%2Fjoi-to-json-schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lightsofapollo","download_url":"https://codeload.github.com/lightsofapollo/joi-to-json-schema/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252335804,"owners_count":21731670,"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-07-31T02:01:04.303Z","updated_at":"2025-05-16T07:03:58.875Z","avatar_url":"https://github.com/lightsofapollo.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"# joi-to-json-schema\n\nThe goal is to provide best effort conversion from Joi objects to JSON\nSchema (draft-04) with the understanding that only some of Joi's schematics \ncan be converted directly. Primarily this module exists to convert Joi schema \nobjects for existing tools which happen to currently consume JSON Schema.\n\n[![npm version](https://badge.fury.io/js/joi-to-json-schema.svg)](http://badge.fury.io/js/joi-to-json-schema)\n[![Build Status](https://travis-ci.org/lightsofapollo/joi-to-json-schema.svg?branch=master)](https://travis-ci.org/lightsofapollo/joi-to-json-schema)\n[![Dependencies Status](https://david-dm.org/lightsofapollo/joi-to-json-schema.svg)](https://david-dm.org/lightsofapollo/joi-to-json-schema)\n[![DevDependencies Status](https://david-dm.org/lightsofapollo/joi-to-json-schema/dev-status.svg)](https://david-dm.org/lightsofapollo/joi-to-json-schema#info=devDependencies)\n\n[![NPM](https://nodei.co/npm/joi-to-json-schema.png)](https://nodei.co/npm/joi-to-json-schema/)\n[![NPM](https://nodei.co/npm-dl/joi-to-json-schema.png)](https://nodei.co/npm-dl/joi-to-json-schema/)\n\n\n## Installation\n\u003e npm install joi-to-json-schema\n\n\n\n## Usage\n\n```js\nvar joi = require('joi'),\n    convert = require('joi-to-json-schema'),\n    joiSchema = joi.object({\n      'name': joi.string().required().regex(/^\\w+$/),\n      'description': joi.string().optional().default('no description provided'),\n      'a': joi.boolean().required().default(false),\n      'b': joi.alternatives().when('a', {\n        is: true,\n        then: joi.string().default('a is true'),\n        otherwise: joi.number().default(0)\n      })\n    });\n\nconvert(joiSchema);\n```\n\nwhich will produce:\n\n```js\n{ type: 'object',\n  properties: \n   { name: { type: 'string', pattern: '^\\\\w+$' },\n     description: { default: 'no description provided', type: 'string' },\n     a: { type: 'boolean', default: false },\n     b: { oneOf: [ { default: 'a is true', type: 'string' }, { type: 'number', default: 0 } ] } },\n  additionalProperties: false,\n  required: [ 'name', 'a' ] }\n```\n\n## JSDOC\n\n```javascript\n /**\n  * Converts the supplied joi validation object into a JSON schema object,\n  * optionally applying a transformation.\n  *\n  * @param {JoiValidation} joi\n  * @param {TransformFunction} [transformer=null]\n  * @returns {JSONSchema}\n  */\n export default function convert(joi,transformer=null) {\n   // ...\n };\n\n /**\n  * Joi Validation Object\n  * @typedef {object} JoiValidation\n  */\n\n /**\n  * Transformation Function - applied just before `convert()` returns and called as `function(object):object`\n  * @typedef {function} TransformFunction\n  */\n\n /**\n  * JSON Schema Object\n  * @typedef {object} JSONSchema\n  */\n```\n\n## Notes\n\nJoi's conditional form, i.e. `.when('name',{is:cond,then:joi,otherwise:joi})`, is evaluated at runtime \nand since, from the perspective of the schema, there is no way of knowing what the condition might resolve to, this\nmodule takes the position that it should provide _all possible resolutions_ in a JSON Schema `oneOf:[]` clause.\n\n## Testing\n\nAll tests cases are first checked against expected results and then validated using\nKris Zyp's excellent [json-schema](https://github.com/kriszyp/json-schema)\n\n## References\n\n- [JSON Schema - Draft 4 from json-schema.org](http://json-schema.org/documentation.html)\n- [IETF Draft: JSON Schema: core definitions and terminology - draft-zyp-json-schema-04](https://tools.ietf.org/html/draft-zyp-json-schema-04)\n- [Understanding JSON Schema](http://spacetelescope.github.io/understanding-json-schema/UnderstandingJSONSchema.pdf)(pdf)\n\n## LICENSE\n\nCopyright 2014, Mozilla Foundation\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n    http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flightsofapollo%2Fjoi-to-json-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flightsofapollo%2Fjoi-to-json-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flightsofapollo%2Fjoi-to-json-schema/lists"}