The validation is working for required and imageHeight but not for fileFormat. I am using async await to wait till the time image loads and return exact height.
Array of supported formats
const SUPPORTED_FORMATS = [
"image/jpg",
"image/jpeg",
"image/png",
];
Function to get image height
const getImageHeight = (value) => {
return new Promise((resolve, reject) => {
let img = new Image();
img.onload = () => resolve(img.height);
img.onerror = reject;
img.src = value?.data || value;
});
};
Yup validation
export const ImageValidate = Yup.object({
image: Yup.mixed()
.required("Please select image")
.test("fileFormat", "Unsupported Format", (value) => {
return SUPPORTED_FORMATS.some((type) =>
type === value?.file?.type
);
})
.test(
"imageHeight",
"image height should be >=100",
async (value) => {
if (value) {
const dimension = await getImageHeight(value);
if (dimension.height >=100) {
return true;
}
return false;
}
return false;
}
),
});