Yup rejects an empty file field in React Hook Form with a null type error
Fix it fast
Most common cause, 1 of 2The field holds null and the schema does not allow it
Decide what an empty file means.
import { mixed, object } from 'yup';
// optional upload
const optionalSchema = object({ file: mixed().nullable() });
// mandatory upload, with a readable message for null and undefined
const requiredSchema = object({ file: mixed().required('Choose a file') });Short answer
must be a `object` type, but the final value was: `null` is Yup 0.32's error for a file field holding null under a non-nullable schema; Yup 1 says file cannot be null. Add .nullable() or .required('Choose a file'), and validate React Hook Form's FileList with mixed(), not object(). Applies to Yup 1.7.
Which cause do you have?
Check the Yup major, then validate the file field alone with null and with whatever the form really holds. A null message (either wording) points to cause 1. A type message that names a value other than null, after a file was picked, points to cause 2.
npm ls yup
// in the component or a test:
schema.validateAt('file', { file: null }).catch((e) => console.log(e.message));
schema.validateAt('file', getValues()).catch((e) => console.log(e.message));Then find the line you see in this list and open its fix:
- 1The field holds null and the schema does not allow it
Yup 1 prints file cannot be null; Yup 0.32 prints must be a `object` type, but the final value was: `null` plus the hint to mark the schema as .nullable() - 2object() is used for a File or FileList
The type error shows up after a file has been chosen, and the final value it prints is not null
What does the error look like?
file must be a `object` type, but the final value was: `null`.If "null" is intended as an empty value be sure to mark the schema as `.nullable()`file cannot be null
Cause 1 of 2
The field holds null and the schema does not allow it
You seeYup 1 prints file cannot be null; Yup 0.32 prints must be a `object` type, but the final value was: `null` plus the hint to mark the schema as .nullable()
Check
Yup schemas are non-nullable by default, and without nullable() a null value counts as a different type. In Yup 0.32 that surfaced as a type error naming the schema type, such as object, with the final value null; Yup 1 adds a separate null check whose default message is cannot be null. The null usually comes from defaultValues, a reset() or a setValue() that clears the file.
grep -rnE "file: null|setValue\('file', null\)" src/Fix
Decide what an empty file means. For an optional upload, add .nullable(). For a mandatory one, use .required() with your own text: required rejects both null and undefined, so the user sees your message instead of a type error.
import { mixed, object } from 'yup';
// optional upload
const optionalSchema = object({ file: mixed().nullable() });
// mandatory upload, with a readable message for null and undefined
const requiredSchema = object({ file: mixed().required('Choose a file') });Verify
validateAt with null now resolves for the optional schema, and rejects with Choose a file for the required one.
requiredSchema.validateAt('file', { file: null }).catch((e) => console.log(e.message)); // Choose a fileConfirmed by raw.githubusercontent.com
Cause 2 of 2
object() is used for a File or FileList
You seeThe type error shows up after a file has been chosen, and the final value it prints is not null
Check
React Hook Form stores a file input's value as a FileList, or a single File if your own Controller stores one. Yup's object() accepts only plain objects, the ones whose type tag reads [object Object], so a FileList or File fails the object type check whatever it contains. object() is meant for nested fields, not for browser file objects.
grep -rnE "file: (yup\.)?object\(" src/Fix
Describe the file with mixed() and a type check of your own, then add tests for the rules you care about. A FileList with no file in it is still a FileList, so test its length.
import { mixed, object } from 'yup';
import { yupResolver } from '@hookform/resolvers/yup';
const schema = object({
file: mixed((input): input is FileList => input instanceof FileList)
.required('Choose a file')
.test('has-file', 'Choose a file', (files) => files.length > 0)
.test('max-size', 'The file must be 5 MB or smaller', (files) => !files[0] || files[0].size <= 5 * 1024 * 1024),
});
const { register, handleSubmit } = useForm({ resolver: yupResolver(schema) });
// <input type="file" {...register('file')} />Verify
Submitting with no file shows Choose a file, a large file shows the size message, and a valid file reaches the submit handler.
schema.validateAt('file', getValues()).then(() => console.log('valid'))Confirmed by raw.githubusercontent.com
How do I stop it coming back?
- Pin yup to one major in package.json; 0.32 and 1.x word the null error differently and treat nullable and required differently.
- Give file fields defaultValues that match the schema: null with .nullable(), or no default with .required().
- Keep one shared file schema built on mixed() with a FileList check, and reuse it in every form.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine on the same stack: TaxiDex, QistLock, Taskvare.