0.0.5 • Published 4 years ago

yup-to-swagger v0.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

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.

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago