📜  在strapi中添加验证 - 任何代码示例

📅  最后修改于: 2022-03-11 14:55:20.329000             🧑  作者: Mango

代码示例1
async uploadFiles(ctx) {
    const {
      state: { userAbility, user },
      request: { body, files: { files } = {} },
    } = ctx;

    const uploadService = strapi.plugins.upload.services.upload;
    const pm = strapi.admin.services.permission.createPermissionsManager(
      userAbility,
      ACTIONS.create,
      fileModel
    );

    if (!pm.isAllowed) {
      throw strapi.errors.forbidden();
    }

    const data = await validateUploadBody(body);
    // ----------------------------------------
    // here I do some validation before the upload process.
    // throw strapi.errors.badRequest('');
    let validationFail = true;
    if (validationFail) {
      throw strapi.errors.badRequest('WRONG SIZE!!11');
    }
    // ----------------------------------------
    const uploadedFiles = await uploadService.upload({ data, files }, { user });

    ctx.body = pm.sanitize(uploadedFiles, { action: ACTIONS.read, withPrivate: false });
  },