Skip to content

Validation

Validation runs client-side before a file enters the upload queue. A file that fails validation moves immediately to failed and never reaches the adapter. The library ships several built-in validators and a composeValidators helper for combining them.

ValidatorPlatformDescription
maxFileSize(bytes)BothChecks file.size
allowedTypes(types)BothChecks file.type against MIME types (supports wildcards like video/*)
maxDuration(seconds)BothWeb: DOM video element. Native: expo-video-metadata (optional, skips if missing)

Use composeValidators to chain validators in order. Stops at the first failure.

import {
composeValidators,
maxFileSize,
allowedTypes,
maxDuration,
} from "@hyperserve/upload";
const validate = composeValidators(
maxFileSize(500 * 1024 * 1024), // 500 MB
allowedTypes(["video/*"]), // any video MIME type
maxDuration(120), // 2 minutes
);

Pass the result to your config:

import { createHyperserveConfig } from "@hyperserve/upload-adapter-hyperserve";
const config = createHyperserveConfig({
// ...
validate,
});

A validator is any function matching the Validator type:

type Validator = (file: FileRef) => ValidationResult | Promise<ValidationResult>;
type ValidationResult =
| { valid: true }
| { valid: false; reason: string };

Example: minimum file size

const minFileSize = (bytes: number): Validator => (file) => {
if (file.size < bytes) {
return { valid: false, reason: `File must be at least ${bytes} bytes` };
}
return { valid: true };
};

Compose custom validators alongside built-in ones:

const validate = composeValidators(
minFileSize(1024),
maxFileSize(500 * 1024 * 1024),
allowedTypes(["video/mp4", "video/webm"]),
);