openjoi
v0.3.2
Published
Converts OpenApi 3.0 specs (JSON) to Joi schemas
Downloads
458
Maintainers
Readme
openjoi
Converts OpenApi 3.0 specs (JSON) to Joi schemas.
Installation
npm install openjoi
Usage
// Import
import path from 'path'
import { readFileSync } from 'fs'
import { convert } from 'openjoi'
import Joi from '@hapi/joi'
// Get file (local file, or from remote, whatever)
const specPath = path.join(__dirname, '../constants/openapi.json')
const doc = JSON.parse(readFileSync(specPath, 'utf-8'))
// Convert
const schemas = convert({ doc })
Given the following openapi.json
as the doc
:
{
...
"components": {
"schemas": {
"foo": {
"type": "string",
"description": "Sint deserunt reprehenderit ut occaecat ad."
},
"bar": {
"type": "string",
"enum": ["one", "two", "three"]
},
...
}
}
}
convert
would give you
const schemas = convert({doc})
// schemas would be the same as:
schemas = {
foo: Joi.string().required(),
bar: Joi.string().valid(['one','two','three']).required()
}
So you can use them for validation, etc...
schemas['foo'].validate('some string') // -> `true`
schemas['foo'].validate(1234) // -> `false
Constructor
convert( { doc } ):
doc
: valid OpenApi 3.0 document in JSON format
Misc
Gotchas / Heads up
- Passing
$refs
that refer to ancestors of itself will result in a generic Joi validation model (Joi.any()
), or else we end up maxing out the call stack. allOf
currently only supports objects (either $ref or inline)
Roadmap
allOf
& descriminator support- string formatting
- integer formatting
Kudos
Highly influenced and inspired by enjoi. Much thanks.