File Structure Validator (fsvalidator)
A Rust library for validating real filesystem directories against a declarative, strongly-typed schema. Useful for enforcing project structure, data ingestion layout, or configuration rules.
Features
- Flexible Matching: Validate files and directories using both literal names and regex patterns
- Nested Hierarchies: Support for complex directory trees with arbitrary depth
- Template System: Reuse common structures via a template reference system
- Strictness Controls: Fine-grained control over validation strictness (required files, restricted directories)
- Format Options: Define validation rules in TOML or JSON formats
- Fluent Builder API: Chain methods to programmatically construct validation structures
- Categorized Errors: Validation errors are categorized for programmatic usage and visualization
- Colorful Display: Eye-catching, symbol-rich display with helpful visual indicators
- Comprehensive Validation: Reports all validation errors with clear, hierarchical organization
Installation
Add to your Cargo.toml:
[]
= { = "0.3.0", = ["toml"] } # or "json" feature
Quick Start
Using TOML Configuration
use Result;
use from_toml;
use ;
Using Builder API
use Result;
use ModelBuilder;
use format_validation_result;
Definition Format
TOML Example
# Root directory definition
[]
= "dir"
= "project"
= true
= true
= ["fsvalidator\\..*"]
# Define children of the root directory
[[]]
= "file"
= "README.md"
= true
[[]]
= "dir"
= "src"
= true
# Define children of the src directory
[[]]
= "file"
= ".*\.rs"
= true
# Reference to a template
[[]]
= "ref"
= "test_directory"
# Template definition
[]
= "dir"
= "tests"
[[]]
= "file"
= "test_.*\.rs"
# Global settings (applied to all nodes unless overridden)
[]
= false
= false
= ["^\\..*", "\\.DS_Store"]
JSON Example
Definition Structure
Node Types
dir: A directory nodefile: A file noderef: A reference to a template
Node Properties
name: Literal name of the file or directorypattern: Regex pattern to match against file or directory name (use eithernameorpattern, not both)required: Whether the node must exist (default: false)allow_defined_only: For directories, whether only defined children are allowed (default: false)excluded: Regex pattern to exclude files/dirs when validating child nodeschildren: List of child nodes (only valid fordirtype)ref: Template reference name (only valid forreftype)
Special Sections
root: The root node of the validation treetemplate: Dictionary of reusable node templatesglobal: Global settings applied to all nodes unless overriddenconfig: Configuration options for the validator
Model Structure
// Main node types
// Directory node with children
// File node
// Node name (literal or pattern)
Use Cases
- Enforcing consistent project layouts
- Validating data pipeline inputs/outputs
- Ensuring configuration directories are correctly structured
- Verifying deployment artifacts
- Testing file-based APIs
Advanced Usage
Programmatic Structure Creation
Using Direct Constructor Methods
You can create validation structures using the constructors directly:
use ;
// Create a file node
let readme = new;
// Create a directory with pattern-matched files
let src_files = new;
let src_dir = new;
// Create the project root
let project = new;
// Validate with colorful display
let path = "path/to/project";
let result = project.validate;
println!;
// Process errors programmatically by category
if let Err = &result
Using Builder API (Recommended)
The builder API provides a more readable and chainable way to construct validation structures:
use ModelBuilder;
// Create the project structure with a fluent builder API
let project = new_dir
.required
.allow_defined_only
.exclude_patterns
.add_file
.add_dir
.add_file_pattern
.up
.add_dir
.add_file_pattern
.build;
// Validate with colorful display
let path = "path/to/project";
let result = project.validate;
println!;
Error Categories
Validation errors are categorized for programmatic handling:
- Missing: Required files or directories that don't exist
- WrongType: Path exists but is the wrong type (e.g., file instead of directory)
- NameMismatch: File/directory name doesn't match the expected pattern
- Unexpected: Unexpected entry in a directory with
allow_defined_only=true - InvalidPattern: Error in a regex pattern
- IoError: Filesystem access errors
- Other: Miscellaneous errors
License
MIT OR Apache-2.0