How to use the isValidImage function

The isValidImage function is used to check whether an image file is valid or not. It accepts a Buffer as an argument.

Types that are validated

You can also pass an options object as a second argument to exclude a specific type.

import { isValidImage } from 'multiform-validator';
import * as path from 'path';
import * as fs from 'fs';

const filePath = path.join(__dirname, 'image.png');
const fileBuffer = fs.readFileSync(filePath);

const isValid = isValidImage(fileBuffer);

console.log(isValid);  // true if the image is valid, false otherwise

Passing options to the isValidImage

import { isValidImage } from 'multiform-validator';
import * as path from 'path';
import * as fs from 'fs';

const filePath = path.join(__dirname, 'image.png');
const fileBuffer = fs.readFileSync(filePath);

const isValid = isValidImage(fileBuffer, { exclude: ['gif'] });

console.log(isValid);  // true if the image is valid, false otherwise

Example Usage with Nestjs and Multer

if (!isValidImage(file.buffer)) {
	throw new BadRequestException("This is not a valid image");
}

First, import the isValidImage function from 'multiform-validator'. Then, define the path to the image you want to check. Call the isValidImage function with the image path as an argument. The function will return true if the image is valid and false if it is not.