AppFlowy/frontend/rust-lib/lib-infra/src/validator_fn.rs
Richard Shiue a515715543
feat: reorder sort precedence (#4592)
* feat: reorder sorts

* chore: add tests, fix tests, fix tauri build and fix clippy

* fix: add missing import
2024-02-05 13:52:59 +08:00

43 lines
1.1 KiB
Rust

use std::path::Path;
use validator::ValidationError;
pub fn required_not_empty_str(s: &str) -> Result<(), ValidationError> {
if s.is_empty() {
return Err(ValidationError::new("should not be empty string"));
}
Ok(())
}
pub fn required_valid_path(s: &str) -> Result<(), ValidationError> {
let path = Path::new(s);
match (path.is_absolute(), path.exists()) {
(true, true) => Ok(()),
(_, _) => Err(ValidationError::new("invalid_path")),
}
}
#[macro_export]
/// Macro to implement a custom validator function for a regex expression.
/// This is intended to replace `validator` crate's own regex validator, which
/// isn't compatible with `fancy_regex`.
///
/// # Arguments:
///
/// - name of the validator function
/// - the `fancy_regex::Regex` object
/// - error message of the `ValidationError`
///
macro_rules! impl_regex_validator {
($validator: ident, $regex: expr, $error: expr) => {
pub(crate) fn $validator(arg: &str) -> Result<(), ValidationError> {
let check = $regex.is_match(arg).unwrap();
if check {
Ok(())
} else {
Err(ValidationError::new($error))
}
}
};
}