@savotije/openapi-to-joi
v2.2.2
Published
Generates Joi schemas from an OpenAPI v3 documents.
Downloads
1
Maintainers
Readme
openapi-to-joi
Generates Joi schemas from an OpenAPI v3 documents.
Installation
$ npm install @savotije/openapi-to-joi
$ yarn add @savotije/openapi-to-joi
General Usage
Generates a file with Joi schemas from an OpenAPI 3 document.
For example, starting with this document:
{
"openapi": "3.0.0",
...
"paths": {
"/": {
"get": {
"operationId": "operation1",
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": {
"type": "string",
"minLength": 5
}
}
],
"responses": {
"200": {
"description": "200 Response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Operation1Response"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"Operation1Response": {
"type": "object",
"properties": {
"email": {
"type": "string",
"description": "Valid email address",
"format": "email"
},
"age": {
"type": "integer",
"description": "Age of adult",
"format": "int32",
"minimum": 18,
"maximum": 99
}
}
}
}
}
}
And running this command:
$ openapi-to-joi openapi.json --output generated.ts --skip-descriptions
Produces this generated.ts
:
import Joi from "joi"
export const schemas = {
parameters: {
operation1: { id: Joi.string().required().min(5) },
},
components: {
Operation1Response: Joi.object({
email: Joi.string().email({}),
age: Joi.number().integer().max(99).min(18),
}).unknown(),
},
}
Using the CLI
To view usage instructions for the CLI, use the --help
command:
$ npm run openapi-to-joi --help
$ yarn openapi-to-joi --help