Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

add validation in strapi

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 });
  },
Source by forum.strapi.io #
 
PREVIOUS NEXT
Tagged: #add #validation #strapi
ADD COMMENT
Topic
Name
1+1 =