Skip to content

Commit 0d74a46

Browse files
docs(book): add length validation (#22)
1 parent 002aa3b commit 0d74a46

5 files changed

Lines changed: 103 additions & 2 deletions

File tree

.github/workflows/website.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ jobs:
3131
- name: Install mdBook
3232
run: cargo binstall --force -y mdbook mdbook-tabs
3333

34+
- name: Build Fortifier
35+
run: cargo build -p fortifier
36+
3437
- name: Run tests
35-
run: mdbook test
38+
run: mdbook test -L ../target/debug/deps
3639
working-directory: book
3740

3841
book-build:

book/src/SUMMARY.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,12 @@
22

33
- [Introduction](./introduction.md)
44
- [Getting Started](./getting-started.md)
5+
- [Validations](./validations/README.md)
6+
- [Email]()
7+
- [Length](./validations/length.md)
8+
- [Regex]()
9+
- [URL]()
10+
- [Integrations]()
11+
- [Serde]()
12+
- [Utoipa]()
513
- [Contributing]()

book/src/validations/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Validations
2+
3+
- [Length](./length.md)

book/src/validations/length.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Length
2+
3+
Validate the length of a string or iterable.
4+
5+
```rust
6+
# extern crate fortifier;
7+
# use fortifier::Validate;
8+
#
9+
##[derive(Validate)]
10+
struct User {
11+
#[validate(length(min = 1, max = 256))]
12+
name: String
13+
}
14+
```
15+
16+
## Types
17+
18+
### String
19+
20+
- [`str`](https://doc.rust-lang.org/std/primitive.str.html)
21+
- [`String`](https://doc.rust-lang.org/std/string/struct.String.html)
22+
23+
Validate the amount of characters ([`chars`](https://doc.rust-lang.org/std/primitive.str.html#method.chars)) in a string.
24+
25+
### Iterable
26+
27+
- [`[T]` (slice)](https://doc.rust-lang.org/std/primitive.slice.html)
28+
- [`[T; N]` (array)](https://doc.rust-lang.org/std/primitive.array.html)
29+
- [`BTreeSet`](https://doc.rust-lang.org/std/collections/struct.BTreeSet.html)
30+
- [`BTreeMap`](https://doc.rust-lang.org/std/collections/struct.BTreeMap.html)
31+
- [`HashSet`](https://doc.rust-lang.org/std/collections/struct.HashSet.html)
32+
- [`HashMap`](https://doc.rust-lang.org/std/collections/struct.HashMap.html)
33+
- [`IndexSet`](https://docs.rs/indexmap/latest/indexmap/set/struct.IndexSet.html) (requires feature `indexmap`)
34+
- [`IndexMap`](https://docs.rs/indexmap/latest/indexmap/map/struct.IndexMap.html) (requires feature `indexmap`)
35+
- [`LinkedList`](https://doc.rust-lang.org/std/collections/struct.LinkedList.html)
36+
- [`Vec`](https://doc.rust-lang.org/std/vec/struct.Vec.html)
37+
- [`VecDeque`](https://doc.rust-lang.org/std/collections/struct.VecDeque.html)
38+
39+
Validate the amount of entries in an iterable.
40+
41+
## Options
42+
43+
### Equal
44+
45+
The length should be equal to the specified expression.
46+
47+
```rust
48+
# extern crate fortifier;
49+
# use fortifier::Validate;
50+
#
51+
##[derive(Validate)]
52+
struct User {
53+
#[validate(length(equal = 2))]
54+
country_code: String
55+
}
56+
```
57+
58+
### Minimum
59+
60+
The length should be equal to or greater than the specified expression.
61+
62+
```rust
63+
# extern crate fortifier;
64+
# use fortifier::Validate;
65+
#
66+
##[derive(Validate)]
67+
struct User {
68+
#[validate(length(min = 1))]
69+
name: String
70+
}
71+
```
72+
73+
### Maximum
74+
75+
The length should be equal to or less than the specified expression.
76+
77+
```rust
78+
# extern crate fortifier;
79+
# use fortifier::Validate;
80+
#
81+
##[derive(Validate)]
82+
struct User {
83+
#[validate(length(max = 256))]
84+
name: String
85+
}
86+
```

packages/fortifier/src/validations/length.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
borrow::Cow,
33
cell::{Ref, RefMut},
4-
collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque},
4+
collections::{BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque},
55
fmt::Display,
66
rc::Rc,
77
sync::Arc,
@@ -164,6 +164,7 @@ validate_with_len!(BTreeSet<T>, T);
164164
validate_with_len!(BTreeMap<K, V>, K, V);
165165
validate_with_len!(HashSet<T, S>, T, S);
166166
validate_with_len!(HashMap<K, V, S>, K, V, S);
167+
validate_with_len!(LinkedList<T>, T);
167168
validate_with_len!(Vec<T>, T);
168169
validate_with_len!(VecDeque<T>, T);
169170
#[cfg(feature = "indexmap")]

0 commit comments

Comments
 (0)