Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trait validation #225

Merged
merged 17 commits into from
Apr 14, 2023
66 changes: 65 additions & 1 deletion validator/src/validation/length.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use crate::traits::HasLen;

/// Validates the length of the value given.
Expand Down Expand Up @@ -32,11 +34,33 @@ pub fn validate_length<T: HasLen>(
true
}

pub trait ValidateLenght: HasLen {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like if the user wants to implement ValidateLength on their own type, they'd need to also implement HaLen?

Might it be simpler if HasLen is not involved?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo on the name and it shouldn't use HasLen at all. Then we can just create a macro to generate all the impls we want for each type. With this trait, HasLen doesn't exist anymore

fn validate_length(self, min: Option<u64>, max: Option<u64>, equal: Option<u64>) -> bool;
}

impl ValidateLenght for String {
fn validate_length(self, min: Option<u64>, max: Option<u64>, equal: Option<u64>) -> bool {
validate_length(self, min, max, equal)
}
}

impl ValidateLenght for Cow<'static, str> {
fn validate_length(self, min: Option<u64>, max: Option<u64>, equal: Option<u64>) -> bool {
validate_length(self, min, max, equal)
}
}

impl<T> ValidateLenght for Vec<T> {
fn validate_length(self, min: Option<u64>, max: Option<u64>, equal: Option<u64>) -> bool {
validate_length(self, min, max, equal)
}
}

#[cfg(test)]
mod tests {
use std::borrow::Cow;

use super::validate_length;
use crate::{validate_length, validation::length::ValidateLenght};

#[test]
fn test_validate_length_equal_overrides_min_max() {
Expand Down Expand Up @@ -76,4 +100,44 @@ mod tests {
fn test_validate_length_unicode_chars() {
assert!(validate_length("日本", None, None, Some(2)));
}


#[test]
fn test_validate_length_trait_equal_overrides_min_max() {
assert!(String::from("hello").validate_length(Some(1), Some(2), Some(5)));
}

#[test]
fn test_validate_length_trait_string_min_max() {
assert!(String::from("hello").validate_length(Some(1), Some(10), None));
}

#[test]
fn test_validate_length_trait_string_min_only() {
assert!(!String::from("hello").validate_length(Some(10), None, None));
}

#[test]
fn test_validate_length_trait_string_max_only() {
assert!(!String::from("hello").validate_length(None, Some(1), None));
}

#[test]
fn test_validate_length_trait_cow() {
let test: Cow<'static, str> = "hello".into();
assert!(test.validate_length(None, None, Some(5)));

let test: Cow<'static, str> = String::from("hello").into();
assert!(test.validate_length(None, None, Some(5)));
}

#[test]
fn test_validate_length_trait_vec() {
assert!(vec![1, 2, 3].validate_length(None, None, Some(3)));
}

#[test]
fn test_validate_length_trait_unicode_chars() {
assert!(String::from("日本").validate_length(None, None, Some(2)));
}
}