'Validating file size and format with YUP
I have a form using reactjs + formik + yup. I have a multi file upload field. I want to validate the file format and max size using yup. How can I do this?
Solution 1:[1]
Expanding on Devin's answer, you can implement that validation with yup.
const schema = Yup.object().shape({
files: Yup.array()
.nullable()
.required('VALIDATION_FIELD_REQUIRED')
.test('is-correct-file', 'VALIDATION_FIELD_FILE_BIG', checkIfFilesAreTooBig)
.test(
'is-big-file',
'VALIDATION_FIELD_FILE_WRONG_TYPE',
checkIfFilesAreCorrectType
),
})
Where the validation functions are:
export function checkIfFilesAreTooBig(files?: [File]): boolean {
let valid = true
if (files) {
files.map(file => {
const size = file.size / 1024 / 1024
if (size > 10) {
valid = false
}
})
}
return valid
}
export function checkIfFilesAreCorrectType(files?: [File]): boolean {
let valid = true
if (files) {
files.map(file => {
if (!['application/pdf', 'image/jpeg', 'image/png'].includes(file.type)) {
valid = false
}
})
}
return valid
}
Solution 2:[2]
This code will work for validating image formats.
const SUPPORTED_FORMATS = ["image/jpg", "image/jpeg", "image/gif", "image/png"];
export const validateImageType = (value) => {
if(value) {
let type = value.match(/[^:]\w+\/[\w-+\d.]+(?=;|,)/)[0]
return SUPPORTED_FORMATS.includes(type)
}
}
Yup.mixed() .test('fileSize', "File is too large", value => value.size <= FILE_SIZE) .test('fileType', "Your Error Message", value => SUPPORTED_FORMATS.includes(value.type) )
Solution 3:[3]
export const UploadFileSchema = yup.object().shape({
file: yup
.mixed()
.required("You need to provide a file")
.test("fileSize", "The file is too large", (value) => {
return value && value[0].sienter code hereze <= 2000000;
})
.test("type", "Only the following formats are accepted: .jpeg, .jpg, .bmp, .pdf and .doc", (value) => {
return value && (
value[0].type === "image/jpeg" ||
value[0].type === "image/bmp" ||
value[0].type === "image/png" ||
value[0].type === 'application/pdf' ||
value[0].type === "application/msword"
);
}),
});
This solution is taken from Maksim Ivanov (on youtube)
Solution 4:[4]
const SUPPORTED_FORMATS = ['image/jpg', 'image/jpeg', 'image/png'];
const registerSchema = Yup.object().shape({
uriImage: Yup.mixed()
.nullable()
.required('A file is required')
.test('Fichier taille',
'upload file', (value) => !value || (value && value.size <= 1024 * 1024))
.test('format',
'upload file', (value) => !value || (value && SUPPORTED_FORMATS.includes(value.type))),
});
Solution 5:[5]
image:
Yup.mixed().test(1000, "File Size is too large", value => value.size <= FILE_SIZE) .test('fileType', "Unsupported File Format", value => SUPPORTED_FORMATS.includes(['image/*']) )
https://hackernoon.com/formik-handling-files-and-recaptcha-209cbeae10bc
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | pfulop |
Solution 2 | Musab Akram |
Solution 3 | Moo |
Solution 4 | jacobit kashala |
Solution 5 | Jakub Kurdziel |