pub fn validate_file_extension(
path: &Path,
arg_name: &str,
expected: &[String],
) -> Result<()>Expand description
Validate that a file has one of the expected extensions
This function checks if the file’s extension matches one of the allowed extensions. The comparison is case-insensitive.
§Arguments
path- Path to the filearg_name- Name of the argument (for error messages)expected- List of allowed extensions (without the leading dot)
§Returns
Ok(())if the file has an allowed extensionErr(ValidationError::InvalidExtension)if the extension doesn’t match
§Extension Format
Extensions should be specified without the leading dot:
- ✅ Correct:
["yaml", "yml"] - ❌ Incorrect:
[".yaml", ".yml"]
§Case Sensitivity
The validation is case-insensitive:
"config.YAML"matches["yaml"]✅"config.Yml"matches["yml"]✅
§Example
use dynamic_cli::validator::file_validator::validate_file_extension;
use std::path::Path;
let path = Path::new("config.yaml");
let allowed = vec!["yaml".to_string(), "yml".to_string()];
validate_file_extension(path, "config_file", &allowed)?;§Error Messages
If the extension is invalid, the error includes:
- The argument name
- The file path
- The list of expected extensions
Example: Invalid file extension for config_file: "config.txt". Expected: yaml, yml