adminjs-upload icon indicating copy to clipboard operation
adminjs-upload copied to clipboard

How to make upload mandatory

Open xinhash opened this issue 5 years ago • 1 comments

If my mongoose schema and admin code is is :


properties: {
  ...,
  media: {
    isVisible: { list: false, show: true, edit: false, filter: false },
  },
}

new Schema({
    label: { type: String, required: true },
    tag: { type: String, required: true },
    media: { type: Schema.Types.Mixed, required: true }
 })
Screenshot 2020-12-27 at 11 44 01 PM

I get an extra input field named media , if I get rid of

media: {
    isVisible: { list: false, show: true, edit: false, filter: false },
  },

xinhash avatar Dec 27 '20 18:12 xinhash

I used actions on edit and new to validate the file

export default {
  resource: mongoose.model("Bank", BankSchema),
  options: {
    actions: {
      edit: {
        before: [actionBefore],
      },
      new: {
        before: [actionBefore],
      },
    },
    properties: {
      imgUrl: {
        isVisible: false,
      },
      mimeType: {
        isVisible: false,
      },
    },
  },
  features: [
    uploadFeature({
      componentLoader,
      provider: minioProvider,
      properties: {
        key: "imgUrl",
        mimeType: "mimeType",
      },
    }),
  ],
};

And the function it self is

const actionBefore = async (request, context) => {
  const { files, method } = request;

  if (method !== "post") {
    return request;
  }

  const filesKeys = Object.keys(files);

  if (!filesKeys.length) {
    throw new ValidationError({
      file: { message: "File is required" },
    });
  }

  return request;
};

It throws an error however I didn't get it to somewhat to work

ArtiomOganesyan avatar Dec 18 '23 15:12 ArtiomOganesyan