Create a custom rule and have this check done there. You can add the ValidatesAttributes trait to use the underlying Laravel validation.
- Custom rule
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Validation\Concerns\ValidatesAttributes;
class MimeType implements Rule
{
use ValidatesAttributes;
/** @var array */
private $extensions;
/** @var array */
private $mimeTypes;
public function __construct($extensions, $mimeTypes)
{
$this->extensions = explode(',', $extensions);
$this->mimeTypes = explode(',', $mimeTypes);
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
if ($this->validateMimes($attribute, $value, $this->extensions)) {
return true;
}
return $this->validateMimetypes($attribute, $value, $this->mimeTypes);
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The file :attribute is not of an allowed type.';
}
}
- Usage
use App\Rules\MimeType;
class DocumentStoreRequest extends ApiRequestBase
{
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
'binary' => [
'required_without:binary_name',
'file',
new MimeType(
'avi,bmp,doc,docx,gif,jpeg,jpg,mp4,mpg,msg,pdf,png,ppt,pptx,tif,tiff,txt,xls,xlsx',
'application/vnd.ms-outlook'
),
],
];
}
}