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.
Built-in validators
Section titled “Built-in validators”| Validator | Platform | Description |
|---|---|---|
maxFileSize(bytes) | Both | Checks file.size |
allowedTypes(types) | Both | Checks file.type against MIME types (supports wildcards like video/*) |
maxDuration(seconds) | Both | Web: DOM video element. Native: expo-video-metadata (optional, skips if missing) |
Composing validators
Section titled “Composing validators”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,});Custom validators
Section titled “Custom validators”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"]),);