https://github.com/tecfu/yup2swagger
Convert your Yup object schema to a Swagger definition
https://github.com/tecfu/yup2swagger
Last synced: about 1 year ago
JSON representation
Convert your Yup object schema to a Swagger definition
- Host: GitHub
- URL: https://github.com/tecfu/yup2swagger
- Owner: tecfu
- Created: 2019-11-27T22:47:01.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2021-05-10T20:25:50.000Z (about 5 years ago)
- Last Synced: 2024-04-15T22:12:00.674Z (over 2 years ago)
- Language: JavaScript
- Homepage:
- Size: 17.6 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# yup-to-swagger
[](https://travis-ci.org/tecfu/yup-to-swagger) [](http://badge.fury.io/js/yup-to-swagger)
---
Convert Yup to Swagger. Transforms a Yup schema into a Swagger/OpenAPI yaml definition.
---
## Example
```js
const yup = require('@tecfu/yup')
const yup2swag = require('yup-to-swagger')
const schema = yup
.object()
.meta({
title: "Title of my definition",
description: "Description of my definition"
})
.shape({
id: yup.number().integer().positive().required(),
name: yup.string(),
email: yup.string().email().required(),
created: yup.date().nullable(),
active: yup.boolean().default(true)
})
let swaggerDefinition = yup2swag.parse(schema, {
extendedSwaggerFormats: true
})
console.log(swaggerDefinition)
// type: object
// title: Title of my definition
// description: Description of my definition
// required:
// - id
// - email
// properties:
// id:
// type: integer
// minimum: 0
// name:
// type: string
// email:
// type: string
// format: email
// created:
// type: string
// format: date
// nullable: true
// active:
// type: boolean
// default: true
```
Or if you want JSON output:
```js
let swaggerDefinition = yup2swag.parse(schema, {
extendedSwaggerFormats: true,
outputFormat: "json"
})
console.log(swaggerDefinition)
// {
// type: 'object',
// title: 'Title of my definition',
// description: 'Description of my definition',
// required: [ 'id', 'email' ],
// properties: {
// id: { type: 'integer', minimum: 0 },
// name: { type: 'string' },
// email: { type: 'string', format: 'email' },
// created: { type: 'string', format: 'date', nullable: true },
// active: { type: 'boolean', default: true }
// }
// }
```
## Please note:
To benefit from the full features of this package, please use the following fork of [Yup]( https://github.com/tecfu/yup ).
```sh
npm i --save @tecfu/yup
```
## License
[MIT License](https://opensource.org/licenses/MIT)
Copyright 2019, Tecfu.