validators

Validators is a folder to store all of your validator functions to make sure the datas sent to your actions and scheduled task functions are good. The folder is not mandatory and can be deleted.

Because we are using ES6 from NodeJS v13+, if you want to import a function from a validator file, make sure to add the .js when importing. Example: import { valid } from "../validators/valid.js"

If you create your module from Swagger, we will auto generate this validators with the Joi library. Here is an example of a validator:

export const postFile = (data) => {

  const schema = Joi.object({
    id: Joi.string().required(),
    firstname: Joi.string().regex(/[0-9A-Za-zÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝàáâãäåæçèéêëìíîïñòóôõöùúûüýÿ.''()\- ]+$/).required(),
    email: Joi.string().email({ tlds: { allow: false } })
  });

  return schema
    .validateAsync(data)
    .then((data) => data)
    .catch((error) => {
      throw {
        message: e.details[0].message.replace(/"/g, ""),
        statusCode: 400,
      };
    });
}

Last updated