file
Builds a parser for files. DServerDataParser.file() ensures the input is a file (with optional coercion support) and returns a typed Either containing either the validated value or a detailed DataParserError.
Example
ts
import { SDP, type SF } from "@duplojs/server-utils";
import { E, type ExpectType, type DP } from "@duplojs/utils";
const schema = SDP.coerce.file({
mimeType: "application/json",
});
const result = await schema.asyncParse("/path/file.json");
if (E.isRight(result)) {
type check = ExpectType<
typeof result,
E.Success<SF.FileInterface>,
"strict"
>;
} else {
type check = ExpectType<
typeof result,
E.Error<DP.DataParserError>,
"strict"
>;
}Parameters
errorMessage: custom message injected into eachissuewhen the input is not a file or does not meet constraints.coerce:trueto transform a path intoFileInterface. Defaults tofalse.mimeType?:string,[string, ...string[]], orRegExpapplied to the file mime type.minSize?:numberorBytesInString(${number}${"b" | "kb" | "mb"...}) to enforce a minimum file size. This check is only executed throughasyncParse.maxSize?:numberorBytesInString(${number}${"b" | "kb" | "mb"...}) to enforce a maximum file size. This check is only executed throughasyncParse.checkExist?:trueto check whether the file really exists. This check is only executed throughasyncParse.
Return value
A DataParserFile exposing parse, asyncParse, isAsynchronous, and clone.
schema.parse(data)returns aDEither.Success<FileInterface>when synchronous checks pass, or aDEither.Error<DataParserError>. IfcheckExist,minSize, ormaxSizeare enabled, this synchronous mode immediately returns an error because these checks require async I/O.schema.asyncParse(data)runs full validation (including real file existence and size constraints) and returns aPromise<DEither.Success<FileInterface>>when all validations pass, or aPromise<DEither.Error<DataParserError>>with paths (path), messages, and rejected values.
Other examples
Extended mode
ts
import { SDPE, type SF } from "@duplojs/server-utils";
import { E, type ExpectType, type DP } from "@duplojs/utils";
const schema = SDPE
.coerce
.file()
.mustExist()
.mimeType(["image/png", "image/jpeg"])
.maxSize("5mb");
const result = await schema.asyncParse("/path/image.png");
if (E.isRight(result)) {
type check = ExpectType<
typeof result,
E.Success<SF.FileInterface>,
"strict"
>;
} else {
type check = ExpectType<
typeof result,
E.Error<DP.DataParserError>,
"strict"
>;
}Difference between parse and asyncParse
ts
import { SDP, type SF } from "@duplojs/server-utils";
import { E, type DP, type ExpectType } from "@duplojs/utils";
const schema = SDP.coerce.file({
checkExist: true,
maxSize: "2mb",
});
// parse returns an Either immediately and cannot execute async I/O checks.
const syncResult = schema.parse("/path/file.json");
if (E.isLeft(syncResult)) {
type check = ExpectType<
typeof syncResult,
E.Error<DP.DataParserError>,
"strict"
>;
}
// asyncParse runs full checks (existence and size) and returns Promise<Either>.
const asyncResult = await schema.asyncParse("/path/file.json");
if (E.isRight(asyncResult)) {
type check = ExpectType<
typeof asyncResult,
E.Success<SF.FileInterface>,
"strict"
>;
} else {
type check = ExpectType<
typeof asyncResult,
E.Error<DP.DataParserError>,
"strict"
>;
}See also
fileInterface- Creates a file interface.
