Expand description
Argument validation module
This module provides functions to validate parsed arguments against the rules defined in the configuration. It completes the parsing layer by ensuring values meet all specified constraints.
§Overview
The validator module works with ValidationRule definitions from
the configuration to validate parsed argument values. It supports:
- File validation: Existence checks and extension restrictions
- Range validation: Numeric bounds (min/max)
- Type-specific validation: Applied after type parsing
§Architecture
Configuration (YAML/JSON)
↓
ValidationRule definitions
↓
Parsed arguments (HashMap<String, String>)
↓
Validators (this module)
↓
Validated values (Result<(), ValidationError>)§Submodules
file_validator: File existence and extension validationrange_validator: Numeric range validation
§Usage Example
use dynamic_cli::validator::{file_validator, range_validator};
use dynamic_cli::config::schema::ValidationRule;
use std::path::Path;
// Validate file exists
let path = Path::new("config.yaml");
file_validator::validate_file_exists(path, "config")?;
// Validate file extension
file_validator::validate_file_extension(
path,
"config",
&["yaml".to_string(), "yml".to_string()]
)?;
// Validate numeric range
range_validator::validate_range(75.0, "percentage", Some(0.0), Some(100.0))?;§Integration with Other Modules
§With Parser Module
The validator works with values after they’ve been parsed:
User Input: "simulate input.dat --threshold 0.5"
↓ [parser]
HashMap { "input": "input.dat", "threshold": "0.5" }
↓ [validator]
Validated ✓§With Config Module
Validation rules come from the configuration:
arguments:
- name: input
arg_type: path
validation:
- must_exist: true
- extensions: [dat, csv]
- name: threshold
arg_type: float
validation:
- min: 0.0
max: 1.0§With Error Module
All validation errors use ValidationError:
FileNotFound- File doesn’t existInvalidExtension- Wrong file extensionOutOfRange- Value outside min/max boundsCustomConstraint- Custom validation failed
§Complete Workflow Example
use dynamic_cli::config::schema::{ArgumentDefinition, ArgumentType, ValidationRule};
use dynamic_cli::validator::{file_validator, validate_file_exists, validate_file_extension};
use std::collections::HashMap;
use std::path::Path;
// Define an argument with validation rules
let arg_def = ArgumentDefinition {
name: "input_file".to_string(),
arg_type: ArgumentType::Path,
required: true,
description: "Input data file".to_string(),
validation: vec![
ValidationRule::MustExist { must_exist: true },
ValidationRule::Extensions {
extensions: vec!["csv".to_string(), "tsv".to_string()],
},
],
};
// Parse arguments
let mut args = HashMap::new();
args.insert("input_file".to_string(), "data.csv".to_string());
// Validate (would check if file exists in real scenario)
if let Some(value) = args.get(&arg_def.name) {
let path = Path::new(value);
for rule in &arg_def.validation {
match rule {
ValidationRule::MustExist { must_exist } if *must_exist => {
validate_file_exists(path, &arg_def.name)?;
},
ValidationRule::Extensions { extensions } => {
validate_file_extension(path, &arg_def.name, extensions)?;
},
_ => {}
}
}
}§Design Philosophy
§Fail Fast
Validation happens early, before command execution, to catch errors as soon as possible. This prevents wasted computation and provides immediate feedback to users.
§Clear Error Messages
All validation errors include:
- The argument name that failed
- The actual value provided
- The expected constraint
- Helpful context for fixing the issue
§Type-Appropriate Validation
Validation rules are matched to argument types:
Patharguments: file existence, extensionsInteger/Floatarguments: range constraints- All types: custom constraints
§No Side Effects
Validators only check values - they don’t modify files, create directories, or perform any side effects.
Re-exports§
pub use file_validator::validate_file_exists;pub use file_validator::validate_file_extension;pub use range_validator::validate_range;
Modules§
- file_
validator - File validation functions
- range_
validator - Numeric range validation functions