Skip to content
Merged
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
15 changes: 13 additions & 2 deletions book/src/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,15 @@ Call the `validate_sync` method on the data structure:

```rust
# extern crate fortifier;
use fortifier::{EmailAddressError, LengthError, Validate, ValidationErrors};
#
use fortifier::{
EmailAddressError,
EmailAddressErrorCode,
LengthError,
LengthErrorCode,
Validate,
ValidationErrors,
};

#[derive(Validate)]
struct CreateUser {
Expand Down Expand Up @@ -90,10 +98,13 @@ fn main() {
data.validate_sync(),
Err(ValidationErrors::from_iter([
CreateUserValidationError::EmailAddress(
EmailAddressError::MissingSeparator {},
EmailAddressError::MissingSeparator {
code: EmailAddressErrorCode,
},
),
CreateUserValidationError::Name(
LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0,
}
Expand Down
6 changes: 3 additions & 3 deletions packages/fortifier-macros/tests/integrations/serde_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ fn main() {
json!([
{
"path": "name",
// "code": "length",
"code": "length",
"subcode": "min",
"min": 1,
"length": 0
},
{
"path": "emailAddresses",
// "code": "nested",
"code": "nested",
"errors": [
{
"index": 0,
"path": "emailAddress",
// "code": "emailAddress",
"code": "emailAddress",
"subcode": "missingSeparator"
}
]
Expand Down
21 changes: 17 additions & 4 deletions packages/fortifier-macros/tests/validations/length/options_pass.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use fortifier::{LengthError, Validate, ValidationErrors};
use fortifier::{LengthError, LengthErrorCode, Validate, ValidationErrors};

#[derive(Validate)]
struct LengthData<'a> {
Expand All @@ -24,12 +24,25 @@ fn main() {
data.validate_sync(),
Err(ValidationErrors::from_iter([
LengthDataValidationError::Equal(LengthError::Equal {
code: LengthErrorCode,
equal: 2,
length: 1
}),
LengthDataValidationError::Min(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::Max(LengthError::Max { max: 4, length: 5 }),
LengthDataValidationError::MinMax(LengthError::Max { max: 4, length: 6 })
LengthDataValidationError::Min(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
LengthDataValidationError::Max(LengthError::Max {
code: LengthErrorCode,
max: 4,
length: 5
}),
LengthDataValidationError::MinMax(LengthError::Max {
code: LengthErrorCode,
max: 4,
length: 6
})
]))
);
}
80 changes: 66 additions & 14 deletions packages/fortifier-macros/tests/validations/length/types_pass.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, LinkedList, VecDeque};

use fortifier::{LengthError, Validate, ValidationErrors};
use fortifier::{LengthError, LengthErrorCode, Validate, ValidationErrors};
use indexmap::{IndexMap, IndexSet};

#[derive(Validate)]
Expand Down Expand Up @@ -53,19 +53,71 @@ fn main() {
assert_eq!(
data.validate_sync(),
Err(ValidationErrors::from_iter([
LengthDataValidationError::Str(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::String(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::Array(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::Slice(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::BTreeMap(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::BTreeSet(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::HashMap(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::HashSet(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::IndexMap(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::IndexSet(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::LinkedList(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::Vec(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::VecDeque(LengthError::Min { min: 1, length: 0 }),
LengthDataValidationError::Str(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
LengthDataValidationError::String(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
LengthDataValidationError::Array(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
LengthDataValidationError::Slice(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
LengthDataValidationError::BTreeMap(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
LengthDataValidationError::BTreeSet(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
LengthDataValidationError::HashMap(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
LengthDataValidationError::HashSet(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
LengthDataValidationError::IndexMap(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
LengthDataValidationError::IndexSet(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
LengthDataValidationError::LinkedList(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
LengthDataValidationError::Vec(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
LengthDataValidationError::VecDeque(LengthError::Min {
code: LengthErrorCode,
min: 1,
length: 0
}),
]))
);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use fortifier::{PhoneNumberCountry, PhoneNumberError, Validate, ValidationErrors};
use fortifier::{
PhoneNumberCountry, PhoneNumberError, PhoneNumberErrorCode, Validate, ValidationErrors,
};
use phonenumber::ParseError;

#[derive(Validate)]
Expand Down Expand Up @@ -29,6 +31,7 @@ fn main() {
)),
PhoneNumberDataValidationError::AllowedCountries(
PhoneNumberError::DisallowedCountryCode {
code: PhoneNumberErrorCode,
allowed: vec![PhoneNumberCountry::GB],
value: Some(PhoneNumberCountry::NL)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::str::FromStr;

use fortifier::{PhoneNumberCountry, PhoneNumberError, Validate, ValidationErrors};
use fortifier::{
PhoneNumberCountry, PhoneNumberError, PhoneNumberErrorCode, Validate, ValidationErrors,
};
use phonenumber::{ParseError, PhoneNumber};

#[derive(Validate)]
Expand Down Expand Up @@ -28,6 +30,7 @@ fn main() {
)),
PhoneNumberDataValidationError::String(PhoneNumberError::from(ParseError::TooShortNsn)),
PhoneNumberDataValidationError::PhoneNumber(PhoneNumberError::DisallowedCountryCode {
code: PhoneNumberErrorCode,
allowed: vec![PhoneNumberCountry::NL],
value: Some(PhoneNumberCountry::GB),
})
Expand Down
67 changes: 67 additions & 0 deletions packages/fortifier/src/error_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/// Implement an error code.
#[macro_export]
macro_rules! error_code {
($name:ident, $code:literal) => {
const CODE: &str = $code;

/// Email address error code.
#[derive(Eq, PartialEq)]
pub struct $name;

impl Default for $name {
fn default() -> Self {
Self
}
}

impl ::std::ops::Deref for $name {
type Target = str;

fn deref(&self) -> &Self::Target {
CODE
}
}

impl ::std::fmt::Debug for $name {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
::std::fmt::Debug::fmt(&**self, f)
}
}

#[cfg(feature = "serde")]
impl<'de> ::serde::Deserialize<'de> for $name {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: ::serde::Deserializer<'de>,
{
deserializer
.deserialize_any($crate::integrations::serde::MustBeStrVisitor(CODE))
.map(|()| Self)
}
}

#[cfg(feature = "serde")]
impl ::serde::Serialize for $name {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
serializer.serialize_str(CODE)
}
}

#[cfg(feature = "utoipa")]
impl ::utoipa::PartialSchema for $name {
fn schema() -> ::utoipa::openapi::RefOr<::utoipa::openapi::schema::Schema> {
::utoipa::openapi::schema::ObjectBuilder::new()
.schema_type(::utoipa::openapi::schema::Type::String)
.enum_values(Some([CODE]))
.build()
.into()
}
}

#[cfg(feature = "utoipa")]
impl ::utoipa::ToSchema for $name {}
};
}
35 changes: 34 additions & 1 deletion packages/fortifier/src/integrations/serde.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
//! Serde utilities

use std::fmt;

use serde::de::{Error, Unexpected, Visitor};

/// Deserialize and serialize with `errors` field.
pub mod errors {
use serde::{Deserialize, Deserializer, Serialize, Serializer};
Expand Down Expand Up @@ -27,9 +31,38 @@ pub mod errors {
{
#[derive(Serialize)]
struct Wrapper<'a, T> {
code: &'static str,
errors: &'a T,
}

Wrapper { errors: value }.serialize(serializer)
Wrapper {
code: "nested",
errors: value,
}
.serialize(serializer)
}
}

/// Serde visitor for a static string.
///
/// Based on `MustBeStrVisitor` from [`monostate`](https://crates.io/crates/monostate).
pub struct MustBeStrVisitor(pub &'static str);

impl<'de> Visitor<'de> for MustBeStrVisitor {
type Value = ();

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "string {:?}", self.0)
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
if v == self.0 {
Ok(())
} else {
Err(E::invalid_value(Unexpected::Str(v), &self))
}
}
}
1 change: 1 addition & 0 deletions packages/fortifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//! Fortifier.

mod error;
mod error_code;
mod integrations;
mod validate;
mod validations;
Expand Down
Loading