file
Creates a file schema.
The
Fileclass is not available by default in Node.js v18 and below.
const Schema = v.file<TMessage>(message);
Generics
TMessageextends ErrorMessage<FileIssue> | undefined
Parameters
messageTMessage
Explanation
With file you can validate the data type of the input. If the input is not a file, you can use message to customize the error message.
Returns
SchemaFileSchema<TMessage>
Examples
The following examples show how file can be used.
Image schema
Schema to validate an image.
const ImageSchema = v.pipe(
v.file('Please select an image file.'),
v.mimeType(['image/jpeg', 'image/png'], 'Please select a JPEG or PNG file.'),
v.maxSize(1024 * 1024 * 10, 'Please select a file smaller than 10 MB.')
);
Related
The following APIs can be combined with file.
Schemas
array, exactOptional, intersect, lazy, looseObject, looseTuple, map, nonNullable, nonNullish, nonOptional, nullable, nullish, object, objectWithRest, optional, record, set, strictObject, strictTuple, tuple, tupleWithRest, undefinedable, union
Methods
assert, config, fallback, getDefault, getDefaults, getFallback, getFallbacks, is, message, parse, parser, pipe, safeParse, safeParser
Actions
check, brand, description, flavor, guard, maxSize, metadata, mimeType, minSize, notSize, rawCheck, rawTransform, readonly, size, title, transform
Utils
entriesFromList, isOfKind, isOfType
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/file/