Skip to content
Docs
Fundamentals
Configuration

Configuration

OAS Tools v3 can read the v2 options and parse them. However, some of the new features will not be available if using the v2 initialization. Check the Compatibility page for more information

Options

General config

This options are set at the root level of the configuration object. They are general purpose options that affect mainly the initialization of the framework.

OptionTypeDefaultDescription
packageJSONString./package.jsonPath to where the package.json file is located
oasFileString./api/oas-file.yamlPath to where the main OpenAPI document is located
useAnnotations (Experimental)BooleanfalseThis feature is experimental. Toggles JSDoc like annotations. Check the routing section for more information

Logger config

This options affect the default logger integrated within the OAS Tools core library. They are set under the logger key in the configuration object.

OptionTypeDefaultDescription
customLoggerObjectnullCustom logger to override the built-in one. Must be a winston instance. Setting this option causes to ignore the rest.
levelStringinfoLevel of the logs that are printed to console
logFileBooleanfalseWhether to save the logs to a file or not.
logFilePathString./logs/oas-tools.logPath where the log file is saved. Ignored if logFile is false.

Middleware config

This options are set under middleware.<middleware name>. They set the configuration for each of the built-in middleware function of OAS Tools.

OptionTypeDefaultDescription
router.disableBooleanfalseToggles Router middleware.
router.controllersString./controllersPath to the controllers directory.
validator.requestValidationBooleantrueToggles Request validation.
validator.responseValidationBooleantrueToggles Response validation.
validator.strictBooleanfalseWhen active, throws an error upon validation errors. In other case, prints a warn message.
security.disableBooleantrueToggles security middleware.
security.authObjectnullObject containing the handler functions for the security schemes. Check the security section for more information.
swagger.disableBooleanfalseToggles SwaggerUI.
swagger.pathString/docsPath where the SwaggerUI will be displayed.
swagger.uiObjectnullCustom configuration for the Swagger interface. Check swagger-ui package docs for more information.
error.disableBooleanfalseToggles the built-in error handler.
error.printStackTraceBooleanfalsePrints the full trace when an error is thrown.
error.customHandlerFunctionnullFunction to extend the built-in handler. Check the error handler section for more information.

Setting configuration

Configuration for OAS Tools can be set in multiple ways, why is why we have established a preference order for the possible configuration sources:

Using package.json

Configuration can be set in the package json under the oas-tools keyword. For example if we wanted to disable error handling and set the logger level to debug:

// package.json

{
  "oas-tools": {
    "logger": { "level": "debug" },
    "middleware": { "error": { "disable": true } }
  }

  ...
}

Using rc config

The rc config uses the rc config loader to load the configuration from multiple sources, following precedence order. Although it allows setting configuration from environment variables or global config files at /etc or $HOME/config, typically you'll want to use a local file located in your proyect root. That file must be named .oastoolsrc and can be written using ini or JSON syntax:

// .oastoolsrc
{
    "oasFile": "api/main.yaml",
    "logger": { "level": "off" }, // setting off or false to logger level deactivates it
    "useAnnotations": true,
    "middleware": {
        "validator": {
            "strict": true
        }
    }
}

Passing an object

Finally, you can pass a configuration object directly to the OAS Tools initialization function. This approach is best when you require declaring function like security handlers or custom logger classes.

Take into account that this approach has the maximum priority and will override any other configuration previously set through any other means.

// index.js

const http = require('http');
const express = require("express");
const oasTools = require("@oas-tools/core");
const app = express();

app.use(express.json());
var serverPort = 8080;

const config = {
    middleware: {
        security: {
            disable: false,
            auth: {
                apikey: (token) => { /* ApiKey security handler */ }
            }
        }
    }
}

oasTools.initialize(app, config).then(() => {
    http.createServer(app).listen(serverPort, () => console.log("Server started!"));
})

In case you need to set complex config options, or your entrypoint file readibility is being compromised, it is a good practise to declare the configuration in a separate file, like so:

// oastools.config.js

module.exports = {
    middleware: {
        security: {
            disable: false,
            auth: {
                apikey: (token) => { /* ApiKey security handler */ }
            }
        }
    }
}

And then import it in the entrypoint file:

// index.js

const http = require('http');
const app = require("express");
const oasTools = require("@oas-tools/core");
const config = require("./oastools.config.js");

const app = express();

app.use(express.json());
var serverPort = 8080;

oasTools.initialize(app, config).then(() => {
    http.createServer(app).listen(serverPort, () => console.log("Server started!"));
})