new Converter(optsopt, schemasopt)
- Source:
Properties:
Name | Type | Description |
---|---|---|
schemas |
Object | Any sub-trees that are extracted from schemas generated
with |
Create a new Converter.
Parameters:
Name | Type | Attributes | Description | |||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
opts |
Object |
<optional> |
Options for initializing this Converter Properties
|
|||||||||||||||||||||||||||||||||||
schemas |
Object |
<optional> |
A tree of schematics for use with references. This will be populated as schemas with names are converted. |
Methods
convert(schema) → {Object}
- Source:
Recursively convert a Joi schema object into a JSON schema and return the resulting schema. This supports most of Joi, throwing errors when it encounters something it is unable to handle.
Metadata is passed through directly to the resulting object. Other data is passed through type-specific handlers.
If a Joi object has attached extract
metadata, and the Converter
instance has extraction enabled, the resulting schema will be
extracted from the tree and placed into Converter#schemas
.
A $ref
will be inserted into the tree in its place.
Examples
my_converter.convert(Joi.string().min(3))
// {
// "type": "string",
// "minLength": 3
// }
const my_converter = new Converter({
extract: true,
extractPath: '/components/schemas/'
});
my_converter.convert(Joi.object({
bar: Joi.string()
}).meta({
extract: 'Foo'
}));
// {
// "$ref": "#/components/schemas/Foo"
// }
my_converter.schemas
// {
// components: {
// schemas: {
// Foo: {
// type: 'object',
// properties: {
// bar: {type: 'string'}
// }
// }
// }
// }
// }
Parameters:
Name | Type | Description |
---|---|---|
schema |
Joi | The Joi schema to convert |
Throws:
-
If we encounter an unknown Joi type, an unknown rule, or a rule that we are unable to correctly translate into JSON Schema, an error is thrown.
- Type
- Error
Returns:
The converted JSON Schema
- Type
- Object