use crate::error::{PathSegment, ValidationError, ValidationErrors};
use crate::rules::AsStr;
pub fn validate_contains<T: AsStr>(
value: &T,
needle: &str,
path: &[PathSegment],
errors: &mut ValidationErrors,
) {
let s = value.as_str_ref();
if !s.contains(needle) {
errors.add(
ValidationError::new(
"contains",
format!("must contain '{}'", needle),
)
.with_path(path.to_vec())
.with_param("expected", serde_json::json!(needle)),
);
}
}
#[cfg(test)]
mod tests {
use super::*;
fn path(name: &str) -> Vec<PathSegment> {
vec![PathSegment::Field(name.to_string())]
}
#[test]
fn test_contains_present() {
let mut errors = ValidationErrors::new();
validate_contains(&"hello@world.com".to_string(), "@", &path("f"), &mut errors);
assert!(errors.is_empty());
}
#[test]
fn test_contains_absent() {
let mut errors = ValidationErrors::new();
validate_contains(&"hello".to_string(), "@", &path("f"), &mut errors);
assert_eq!(errors.len(), 1);
assert_eq!(errors.errors()[0].code, "contains");
assert_eq!(errors.errors()[0].params["expected"], "@");
}
#[test]
fn test_contains_case_sensitive() {
let mut errors = ValidationErrors::new();
validate_contains(&"Hello".to_string(), "hello", &path("f"), &mut errors);
assert_eq!(errors.len(), 1); }
#[test]
fn test_contains_empty_needle() {
let mut errors = ValidationErrors::new();
validate_contains(&"anything".to_string(), "", &path("f"), &mut errors);
assert!(errors.is_empty());
}
#[test]
fn test_contains_empty_value() {
let mut errors = ValidationErrors::new();
validate_contains(&"".to_string(), "x", &path("f"), &mut errors);
assert_eq!(errors.len(), 1);
}
#[test]
fn test_contains_unicode() {
let mut errors = ValidationErrors::new();
validate_contains(&"café latte".to_string(), "café", &path("f"), &mut errors);
assert!(errors.is_empty());
}
}