Skip to content

Validate keys #94

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 59 additions & 22 deletions garde/src/rules/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,50 +28,87 @@ pub trait Inner<T> {
F: FnMut(&T, &Self::Key);
}

impl<T> Inner<T> for Vec<T> {
type Key = usize;
impl<T> Inner<T> for Option<T> {
type Key = NoKey;

fn validate_inner<F>(&self, f: F)
fn validate_inner<F>(&self, mut f: F)
where
F: FnMut(&T, &Self::Key),
{
self.as_slice().validate_inner(f)
if let Some(item) = self {
f(item, &NoKey::default())
}
}
}

impl<const N: usize, T> Inner<T> for [T; N] {
type Key = usize;
macro_rules! impl_via_iter {
(in<const $N:ident, $($lifetime:lifetime,)? $V:ident $(, $S:ident)?> $T:ty) => {
impl<const $N: usize, $($lifetime,)? $V $(, $S)?> Inner<$V> for $T {
type Key = usize;

fn validate_inner<F>(&self, f: F)
where
F: FnMut(&T, &Self::Key),
{
self.as_slice().validate_inner(f)
}
fn validate_inner<F>(&self, mut f: F)
where
F: FnMut(&$V, &Self::Key),
{
for (index, item) in self.iter().enumerate() {
f(item, &index);
}
}
}
};
(in<$($lifetime:lifetime,)? $V:ident $(, $S:ident)?> $T:ty) => {
impl<$($lifetime,)? $V $(, $S)?> Inner<$V> for $T {
type Key = usize;

fn validate_inner<F>(&self, mut f: F)
where
F: FnMut(&$V, &Self::Key),
{
for (index, item) in self.iter().enumerate() {
f(item, &index);
}
}
}
};
}

impl<'a, T> Inner<T> for &'a [T] {
type Key = usize;
impl_via_iter!(in<'a, T> &'a [T]);
impl_via_iter!(in<const N, T> [T; N]);
impl_via_iter!(in<T> Vec<T>);
impl_via_iter!(in<T> std::collections::VecDeque<T>);
impl_via_iter!(in<T> std::collections::BinaryHeap<T>);
impl_via_iter!(in<T> std::collections::LinkedList<T>);
impl_via_iter!(in<T, S> std::collections::HashSet<T, S>);
impl_via_iter!(in<T> std::collections::BTreeSet<T>);

impl<K, V, S> Inner<V> for std::collections::HashMap<K, V, S>
where
K: PathComponentKind,
{
type Key = K;

fn validate_inner<F>(&self, mut f: F)
where
F: FnMut(&T, &Self::Key),
F: FnMut(&V, &Self::Key),
{
for (index, item) in self.iter().enumerate() {
f(item, &index);
for (key, value) in self.iter() {
f(value, key)
}
}
}

impl<T> Inner<T> for Option<T> {
type Key = NoKey;
impl<K, V> Inner<V> for std::collections::BTreeMap<K, V>
where
K: PathComponentKind,
{
type Key = K;

fn validate_inner<F>(&self, mut f: F)
where
F: FnMut(&T, &Self::Key),
F: FnMut(&V, &Self::Key),
{
if let Some(item) = self {
f(item, &NoKey::default())
for (key, value) in self.iter() {
f(value, key)
}
}
}
56 changes: 56 additions & 0 deletions garde/src/rules/keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//! Key validation.
//!
//! ```rust
//! #[derive(garde::Validate)]
//! struct Test {
//! #[garde(keys(length(min=1)))]
//! v: HashMap<String, String>,
//! }
//! ```
//!
//! The entrypoint is the [`Keys`] trait. Implementing this trait for a type allows that type to be used with the `#[garde(keys(..))]` rule.

use crate::error::PathComponentKind;

pub fn apply<T, K, F>(field: &T, f: F)
where
T: Keys<K>,
K: PathComponentKind,
F: FnMut(&K),
{
field.validate_keys(f)
}

pub trait Keys<K: PathComponentKind> {
fn validate_keys<F>(&self, f: F)
where
F: FnMut(&K);
}

impl<K, V> Keys<K> for std::collections::HashMap<K, V>
where
K: PathComponentKind,
{
fn validate_keys<F>(&self, mut f: F)
where
F: FnMut(&K),
{
for key in self.keys() {
f(key)
}
}
}

impl<K, V> Keys<K> for std::collections::BTreeMap<K, V>
where
K: PathComponentKind,
{
fn validate_keys<F>(&self, mut f: F)
where
F: FnMut(&K),
{
for key in self.keys() {
f(key)
}
}
}
1 change: 0 additions & 1 deletion garde/src/rules/length/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ macro_rules! impl_via_len {
}

impl_via_len!(std::string::String);
impl_via_len!(in<'a> &'a std::string::String);
impl_via_len!(in<'a> &'a str);
impl_via_len!(in<'a> std::borrow::Cow<'a, str>);
impl_via_len!(std::rc::Rc<str>);
Expand Down
1 change: 0 additions & 1 deletion garde/src/rules/length/chars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ macro_rules! impl_via_chars {
}

impl_via_chars!(std::string::String);
impl_via_chars!(in<'a> &'a std::string::String);
impl_via_chars!(in<'a> &'a str);
impl_via_chars!(in<'a> std::borrow::Cow<'a, str>);
impl_via_chars!(std::rc::Rc<str>);
Expand Down
1 change: 0 additions & 1 deletion garde/src/rules/length/graphemes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ macro_rules! impl_str {
}

impl_str!(std::string::String);
impl_str!(in<'a> &'a std::string::String);
impl_str!(in<'a> &'a str);
impl_str!(in<'a> std::borrow::Cow<'a, str>);
impl_str!(std::rc::Rc<str>);
Expand Down
9 changes: 0 additions & 9 deletions garde/src/rules/length/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ macro_rules! impl_via_bytes {
}

impl_via_bytes!(std::string::String);
impl_via_bytes!(in<'a> &'a std::string::String);
impl_via_bytes!(in<'a> &'a str);
impl_via_bytes!(in<'a> std::borrow::Cow<'a, str>);
impl_via_bytes!(std::rc::Rc<str>);
Expand Down Expand Up @@ -83,7 +82,6 @@ macro_rules! impl_via_len {
}

impl_via_len!(in<T> Vec<T>);
impl_via_len!(in<'a, T> &'a Vec<T>);
impl_via_len!(in<'a, T> &'a [T]);

impl<const N: usize, T> Simple for [T; N] {
Expand All @@ -105,10 +103,3 @@ impl_via_len!(in<T> std::collections::BTreeSet<T>);
impl_via_len!(in<T> std::collections::VecDeque<T>);
impl_via_len!(in<T> std::collections::BinaryHeap<T>);
impl_via_len!(in<T> std::collections::LinkedList<T>);
impl_via_len!(in<'a, K, V, S> &'a std::collections::HashMap<K, V, S>);
impl_via_len!(in<'a, T, S> &'a std::collections::HashSet<T, S>);
impl_via_len!(in<'a, K, V> &'a std::collections::BTreeMap<K, V>);
impl_via_len!(in<'a, T> &'a std::collections::BTreeSet<T>);
impl_via_len!(in<'a, T> &'a std::collections::VecDeque<T>);
impl_via_len!(in<'a, T> &'a std::collections::BinaryHeap<T>);
impl_via_len!(in<'a, T> &'a std::collections::LinkedList<T>);
1 change: 0 additions & 1 deletion garde/src/rules/length/utf16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ macro_rules! impl_str {
}

impl_str!(std::string::String);
impl_str!(in<'a> &'a std::string::String);
impl_str!(in<'a> &'a str);
impl_str!(in<'a> std::borrow::Cow<'a, str>);
impl_str!(std::rc::Rc<str>);
Expand Down
1 change: 1 addition & 0 deletions garde/src/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub mod credit_card;
pub mod email;
pub mod inner;
pub mod ip;
pub mod keys;
pub mod length;
pub mod pattern;
#[cfg(feature = "phone-number")]
Expand Down
9 changes: 9 additions & 0 deletions garde/tests/rules/keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::collections::HashMap;

use super::util;

#[derive(Debug, garde::Validate)]
struct Key<'a> {
#[garde(inner(length(min = 1)), keys(length(min = 1)))]
inner: HashMap<&'a str, &'a str>,
}
1 change: 1 addition & 0 deletions garde/tests/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod dive_with_rules;
mod email;
mod inner;
mod ip;
mod keys;
mod length;
mod multi_rule;
mod newtype;
Expand Down
17 changes: 15 additions & 2 deletions garde_derive/src/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,10 @@ fn check_rule(
Suffix(v) => apply!(Suffix(v), span),
Pattern(v) => apply!(Pattern(check_regex(v)?), span),
Inner(v) => {
let mut error = None;
if rule_set.inner.is_none() {
rule_set.inner = Some(Box::new(model::RuleSet::empty()));
}

let mut error = None;
for raw_rule in v.contents {
if let Err(e) = check_rule(field, raw_rule, rule_set.inner.as_mut().unwrap(), true)
{
Expand All @@ -382,6 +381,20 @@ fn check_rule(
return Err(error);
}
}
Keys(v) => {
let mut error = None;
if rule_set.keys.is_none() {
rule_set.keys = Some(Box::new(model::RuleSet::empty()));
}
for raw_rule in v.contents {
if let Err(e) = check_rule(field, raw_rule, rule_set.keys.as_mut().unwrap(), true) {
error.maybe_fold(e);
}
}
if let Some(error) = error {
return Err(error);
}
}
};

Ok(())
Expand Down
Loading