Converter

Converter

new Converter(optsopt, schemasopt)

Source:
Properties:
Name Type Description
schemas Object

Any sub-trees that are extracted from schemas generated with Converter#convert will be stored on this object for easy access and re-use.

Create a new Converter.

Parameters:
Name Type Attributes Description
opts Object <optional>

Options for initializing this Converter

Properties
Name Type Attributes Default Description
types Object <optional>

Custom handlers for different Joi types.

ipFormat String <optional>
ipv4

The specific format for IP formatted strings. This should be ipv4 or ipv6.

extract Boolean <optional>
false

When true, sub-trees with extract metadata will be extracted from the output of Converter#convert and stored within Converter#schemas while $refs are inserted into their previous positions.

extractPath String <optional>
"/"

An optional prefix to append to extract when extracting sub-trees from generated schema. Converter#schemas, placing references in the generated schema instead.

ignoreUnknownRules Boolean <optional>
false

If this is true, the converter will skip rules with unknown names rather than throwing an error.

ignoreUnknownTypes Boolean <optional>
false

If this is true, the converter will skip objects with unknown types rather than throwing an error.

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