8 – Validate a file extension using getMimeType

I want to create a hook that will validate a file upload and only allow jpg extension or a multiple array jpg, gif. I am not sure how to do this. If this below can validate the length of the file title.

if (strlen($file
 ->getFilename()) > 100) {
    $errors() = t("The file's name exceeds the 100 characters limit.");
 }

Then can something simular to this work?

use DrupalCoreFormFormStateInterface;
 function blank_moda_file_validate(DrupalfileFileInterface $file) {
   $errors = array();

if ($file
 ->getMimeType()) !== jpg, gif) {
   $errors() = t("The file is not a jpg or gif.");
}