how to validate a file name match a criteria in react yup

Viewed 27

I want to validate that my file names include the SUPPORTED_NAMES. If I upload a file without the account in its name it should not pass validation. right now my code does not pass validation no matter the name of the file

const SUPPORTED_NAMES = ['Account','ticket','task']

validationSchema: Yup.object().shape({
  file: Yup.mixed()
    .required("file is required")
    .test(
      "fileName",
      "file name not supported",
      (value) => value && SUPPORTED_NAMES.includes(value.name)
    )
1 Answers

I was able to come up with the answer:

test('fileName', 'unsuported file name', (value: FileList)=>{
   for(let i = 0; i < value.lenght; i++){
   const file = value.item(i);
   if(file.name){
    const hasKeyWord = SUPPORTED_NAMES.some((element) => 
     file.name.includes(element.toLowerCase())
   );
     if(!hasKeyWord){
      return false
    }
  }
}
 return true;

})
Related