Skip to content

Writing a plugin

Alan Berdinelli edited this page Feb 13, 2024 · 6 revisions

Plugins in Schemy consist in custom events. As a plugin developer, you should write methods for those events and we will call them for you.

They will have access to the Schemy instance (we send the this context to your method).

Quick start

If you want to start writing your plugin right away, you can clone this template:

git clone https://github.com/aeberdinelli/schemy-plugin-boilerplate.git

Example

This is a basic example that outputs hello world to the console each time a new schema is created.

const Plugin = {
    beforeParse() {
        console.log('Hello world');
    }
}

module.exports = Plugin;

Available events

pluginsInitialized

This is called when calling Schemy.extend to load plugins into schemy.

// In your plugin
const pluginName = 'my-plugin';

function pluginsInitialized(plugins) {
    if (plugins.some(plugin => plugin.pluginName === 'my-plugin')) {
        console.log('Thanks for using my plugin!');
    }
}

module.exports = { pluginName, pluginsInitialized }

beforeParse

This is called before Schemy tries to parse the object received in the constructor.
You should use it if you want to pre-parse the schema, or update it in any way. You will receive the schema as a parameter in your method:

// In your plugin
function beforeParse(schema) {
    // Do what you need with the schema
    delete schema.password;

    // You can set this flag to prevent Schemy from parsing the schema again
    this.schemaParsed = true;
}

module.exports = { beforeParse }

afterParse

This is called after Schemy parses an object as schema correctly.
You should use it if you want to do something with the parsed schema or add new properties there.

// In your plugin
function afterParse(schema) {
    // Maybe you can set some flags to use later?
    this.myPluginVersion = '1.0.0';
}

module.exports = { afterParse }

beforeValidate

This is called before Schemy tries to validate data against the schema.

// In your plugin
function beforeValidate(data) {
    // You can add extra validations here
    // Or process the data before schemy validates it

    // In this case, we translate the property 'title' if available
    if (data.title) {
        data.title = data.title.replace('hello', 'hola');
    }
}

module.exports = { beforeValidate }

afterValidate

This is called after Schemy does all its validations. This is a good place to add extra validation features.

// In your plugin
function afterValidate(data) {
    // In this case, we check if title contains a bad word
    if (data.title && data.title.contains('bad word')) {
        this.validationErrors.push('title contains a bad word!');
    }
}

module.exports = { afterValidate }

getValidationErrors

This is called before we return the validation errors. This is the perfect place to translate errors.

// In your plugin
function getValidationErrors() {
    // Schemy does not send any parameters since the errors are already within the instance
    // Here we add an exclamation mark on every error
    this.validationErrors = this.validationErrors.map(error => `${error}!`);
}

module.exports = { getValidationErrors }

Clone this wiki locally