Gathering detailed insights and metrics for o2team-joi-to-swagger
Gathering detailed insights and metrics for o2team-joi-to-swagger
Gathering detailed insights and metrics for o2team-joi-to-swagger
Gathering detailed insights and metrics for o2team-joi-to-swagger
npm install o2team-joi-to-swagger
Typescript
Module System
Min. Node Version
Node Version
NPM Version
71.7
Supply Chain
98.8
Quality
74.6
Maintenance
100
Vulnerability
99.6
License
Total Downloads
352,822
Last Day
110
Last Week
402
Last Month
2,772
Last Year
34,734
Minified
Minified + Gzipped
Latest Version
3.2.2
Package Id
o2team-joi-to-swagger@3.2.2
Unpacked Size
19.86 kB
Size
6.41 kB
File Count
6
NPM Version
6.2.0
Node Version
10.9.0
Cumulative downloads
Total Downloads
Last day
-48.6%
110
Compared to previous day
Last week
-46.3%
402
Compared to previous week
Last month
-9.9%
2,772
Compared to previous month
Last year
-47.4%
34,734
Compared to previous year
1
1
Conversion library for transforming Joi schema objects into Swagger schema definitions.
1// input 2joi.object().keys({ 3 id: joi.number().integer().positive().required(), 4 name: joi.string(), 5 email: joi.string().email().required(), 6 created: joi.date().allow(null), 7 active: joi.boolean().default(true), 8})
1// output 2{ 3 "type": "object", 4 "required": ["id", "email"], 5 "properties": { 6 "id": { 7 "type": "integer", 8 "minimum": 1 9 }, 10 "name": { 11 "type": "string" 12 }, 13 "email": { 14 "type": "string", 15 "format": "email" 16 }, 17 "created": { 18 "type": "string", 19 "nullable": true, 20 "format": "date-time" 21 }, 22 "active": { 23 "type": "boolean" 24 } 25 } 26}
1var j2s = require('joi-to-swagger'); 2 3var {swagger, components} = j2s(mySchema, existingComponents);
J2S takes two arguments, the first being the Joi object you wish to convert. The second optional argument is a collection of existing components to reference against for the meta className
identifiers (see below).
J2S returns a result object containing swagger
and components
properties. swagger
contains your new schema, components
contains any components that were generated while parsing your schema.
joi.object()
.unknown(false)
-> additionalProperties: false
.required()
on object members produces a "required": []
arrayjoi.array().items()
defines the structure using the first schema provided on .items()
(see below for how to override)
.min(4)
-> "minItems": 4
.max(10)
-> "maxItems": 10
.unique(truthy)
-> "uniqueItems": true
joi.number()
produces "type": "number"
with a format of "float"
.precision()
-> "format": "double"
.integer()
-> "type": "integer"
.strict().only(1, 2, '3')
-> "enum": [1, 2]
(note that non-numbers are omitted due to swagger type constraints).allow(null)
-> "nullable": true
.min(5)
-> "minimum": 5
.max(10)
-> "maximum": 10
.positive()
-> "minimum": 1
.negative()
-> "maximum": -1
joi.string()
produces "type": "string"
with no formatting
.strict().only('A', 'B', 1)
-> "enum": ["A", "B"]
(note that non-strings are omitted due to swagger type constraints).alphanum()
-> "pattern": "/^[a-zA-Z0-9]*$/"
.alphanum().lowercase()
.alphanum().uppercase()
.token()
-> "pattern": "/^[a-zA-Z0-9_]*$/"
.token().lowercase()
.token().uppercase()
.email()
-> "format": "email"
.isoDate()
-> "format": "date-time"
.regex(/foo/)
-> "pattern": "/foo/"
.allow(null)
-> "nullable": true
.min(5)
-> "minLength": 5
.max(10)
-> "maxLength": 10
joi.binary()
produces "type": "string"
with a format of "binary"
.
.encoding('base64')
-> "format": "byte"
.min(5)
-> "minLength": 5
.max(10)
-> "maxLength": 10
.allow(null)
-> "nullable": true
joi.date()
produces "type": "string"
with a format of "date-time"
.
.allow(null)
-> "nullable": true
joi.alternatives()
defines the structure using the first schema provided on .items()
(see below for how to override)
any.default()
sets the "default"
detail.
any.example()
sets the "example"
or "examples"
.
.example('hi')
-> "example": "hi"
.example('hi').example('hey')
-> "examples": ["hi", "hey"]
.example('hi', 'hey')
-> "examples": ["hi", "hey"]
joi.any().meta({ swaggerType: 'file' }).description('simpleFile')
add a file to the swagger structure
The following may be provided on a joi .meta()
object to explicitly override default joi-to-schema behavior.
className: By default J2S will be full verbose in its components. If an object has a className
string, J2S will look for an existing schema component with that name, and if a component does not exist then it will create one. Either way, it will produce a $ref
element for that schema component. If a new component is created it will be returned with the swagger schema.
classTarget: Named components are assumed to be schemas, and are referenced as components/schemas/ComponentName
. If a classTarget
meta value is provided (such as parameters
), this will replace schemas in the reference.
swaggerIndex: Swagger's deterministic design disallows for supporting multiple type components. Because of this, only a single schema from .alternatives()
and .array().items()
may be converted to swagger. By default J2S will use the first component. Defining a different zero based index for this meta tag will override that behavior.
swagger: To explicitly define your own swagger component for a joi schema object, place that swagger object in the swagger
meta tag. It will be mixed in to the schema that J2S produces.
swaggerOverride: If this meta tag is truthy, the swagger
component will replace the result for that schema instead of mixing in to it.
swaggerType: Can be used with the .any() type to add files.
No vulnerabilities found.
No security vulnerabilities found.