Skip to content

murnat98/yup-to-swagger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

yup-to-swagger

Build Status NPM version


Convert Yup to Swagger. Transforms a Yup schema into a Swagger/OpenAPI yaml definition.


Example

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:

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.

npm i --save @tecfu/yup

License

MIT License

Copyright 2019, Tecfu.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published