file-size
$file-size
Validates that a file's size falls within the specified range. Accepts size constraints in bytes and checks the file's metadata without loading the entire file into memory. Can specify minimum, maximum, or both bounds.
This is an async processor that performs filesystem I/O, and probably best suited for use during transformation or validation phases.
Parameters
min(number, optional): Minimum file size in bytes (inclusive). If omitted, no lower bound.max(number, optional): Maximum file size in bytes (inclusive). If omitted, no upper bound.
Unit Conversions: Size must be specified in bytes. Use standard conversions:
- 1 KB = 1024 bytes
- 1 MB = 1024 * 1024 bytes (1,048,576)
- 1 GB = 1024 * 1024 * 1024 bytes (1,073,741,824)
Note: Typically used with $file validator to ensure the file exists before checking size.
The processor reads file metadata via fs.stat() and does not load file contents.
Example
// Validate a config file exists and is under 1 MB
new Schema('string')
.validator('$file')
.validator({'$file-size': {max: 1024 * 1024}})
// Require an upload file to be between 1 KB and 10 MB
new Schema('string')
.validator('$file')
.validator({'$file-size': {min: 1024, max: 10 * 1024 * 1024}})
// Ensure a log file is not empty
new Schema('string')
.validator('$file')
.validator({'$file-size': {min: 1}})