Static methods
Static methods are not part of the plugin instance—they are attached to the constructor function. These methods are used to validate and normalize the plugin options and theme config, which are then used as constructor parameters to initialize the plugin instance.
validateOptions({options, validate})
Returns validated and normalized options for the plugin. This method is called before the plugin is initialized. You must return the options since they will be passed to the plugin during initialization.
options
validateOptions is called with options passed to plugin for validation and normalization.
validate
validateOptions is called with validate function which takes a Joi schema and options as the arguments, returns validated and normalized options. validate will automatically handle error and validation config.
Joi is recommended for validation and normalization of options.
To avoid mixing Joi versions, use import {Joi} from '@docusaurus/utils-validation'
If you don't use Joi for validation you can throw an Error in case of invalid options and return options in case of success.
export default function myPlugin(context, options) {
return {
name: 'docusaurus-plugin',
// rest of methods
};
}
export function validateOptions({options, validate}) {
const validatedOptions = validate(myValidationSchema, options);
return validatedOptions;
}
validateThemeConfig({themeConfig, validate})
Return validated and normalized configuration for the theme.
themeConfig
validateThemeConfig is called with themeConfig provided in docusaurus.config.js for validation and normalization.
validate
validateThemeConfig is called with validate function which takes a Joi schema and themeConfig as the arguments, returns validated and normalized options. validate will automatically handle error and validation config.
Joi is recommended for validation and normalization of theme config.
To avoid mixing Joi versions, use import {Joi} from '@docusaurus/utils-validation'
If you don't use Joi for validation you can throw an Error in case of invalid options.
export default function myPlugin(context, options) {
return {
name: 'docusaurus-plugin',
// rest of methods
};
}
export function validateThemeConfig({themeConfig, validate}) {
const validatedThemeConfig = validate(myValidationSchema, options);
return validatedThemeConfig;
}
TypeScript types
The @docusaurus/types package declares the theme config types with 2 interfaces: ThemeConfig after validation and normalization, and UserThemeConfig as written in docusaurus.config.js. Your theme can add its own fields to them with TypeScript declaration merging:
declare module '@docusaurus/types' {
interface ThemeConfig {
myTheme: {position: 'top' | 'bottom'};
}
interface UserThemeConfig {
myTheme?: {position?: 'top' | 'bottom'};
}
}
Once your theme types are loaded, useThemeConfig() returns a typed myTheme field, and site authors get autocompletion for myTheme in their themeConfig.