[Rust-Axum] Fix compilation error when validate is used on Nullable values (#20100)

* Fix compilation error when validate is used on Nullable values

* Update samples

* Switch Nullable Into Option trait implement to From

* Update samples from rebase
This commit is contained in:
Victoria Casasampere Fernandez 2024-11-15 15:03:59 +01:00 committed by GitHub
parent 293524785e
commit d7a23a9fcf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 1375 additions and 0 deletions

View File

@ -632,6 +632,131 @@ where
}
}
impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}
impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}
impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}
impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}
impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}
impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}
impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}
#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {

View File

@ -632,6 +632,131 @@ where
}
}
impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}
impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}
impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}
impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}
impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}
impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}
impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}
#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {

View File

@ -632,6 +632,131 @@ where
}
}
impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}
impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}
impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}
impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}
impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}
impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}
impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}
#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {

View File

@ -632,6 +632,131 @@ where
}
}
impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}
impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}
impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}
impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}
impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}
impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}
impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}
#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {

View File

@ -632,6 +632,131 @@ where
}
}
impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}
impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}
impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}
impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}
impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}
impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}
impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}
#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {

View File

@ -632,6 +632,131 @@ where
}
}
impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}
impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}
impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}
impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}
impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}
impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}
impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}
#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {

View File

@ -632,6 +632,131 @@ where
}
}
impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}
impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}
impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}
impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}
impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}
impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}
impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}
#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {

View File

@ -632,6 +632,131 @@ where
}
}
impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}
impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}
impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}
impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}
impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}
impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}
impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}
#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {

View File

@ -632,6 +632,131 @@ where
}
}
impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}
impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}
impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}
impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}
impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}
impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}
impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}
#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {

View File

@ -632,6 +632,131 @@ where
}
}
impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}
impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}
impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}
impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}
impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}
impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}
impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}
#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {

View File

@ -632,6 +632,131 @@ where
}
}
impl<T> validator::Validate for Nullable<T>
where
T: validator::Validate,
{
fn validate(&self) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate(),
Self::Null => Ok(()),
}
}
}
impl<'a, T> validator::ValidateArgs<'a> for Nullable<T>
where
T: validator::ValidateArgs<'a>,
{
type Args = T::Args;
fn validate_with_args(&self, args: Self::Args) -> Result<(), validator::ValidationErrors> {
match self {
Self::Present(x) => x.validate_with_args(args),
Self::Null => Ok(()),
}
}
}
impl<T> validator::ValidateEmail for Nullable<T>
where
T: validator::ValidateEmail,
{
fn as_email_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_email_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateUrl for Nullable<T>
where
T: validator::ValidateUrl,
{
fn as_url_string(&self) -> Option<std::borrow::Cow<str>> {
match self {
Self::Present(x) => x.as_url_string(),
Self::Null => None,
}
}
}
impl<T> validator::ValidateContains for Nullable<T>
where
T: validator::ValidateContains,
{
fn validate_contains(&self, needle: &str) -> bool {
match self {
Self::Present(x) => x.validate_contains(needle),
Self::Null => true,
}
}
}
impl<T> validator::ValidateRequired for Nullable<T>
where
T: validator::ValidateRequired,
{
fn is_some(&self) -> bool {
self.is_present()
}
}
impl<T> validator::ValidateRegex for Nullable<T>
where
T: validator::ValidateRegex,
{
fn validate_regex(&self, regex: impl validator::AsRegex) -> bool {
match self {
Self::Present(x) => x.validate_regex(regex),
Self::Null => true,
}
}
}
impl<T, I> validator::ValidateRange<I> for Nullable<T>
where
T: validator::ValidateRange<I>,
{
fn greater_than(&self, max: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.greater_than(max),
Self::Null => None,
}
}
fn less_than(&self, min: I) -> Option<bool> {
use validator::ValidateRange;
match self {
Self::Present(x) => x.less_than(min),
Self::Null => None,
}
}
}
impl<T, I> validator::ValidateLength<I> for Nullable<T>
where
T: validator::ValidateLength<I>,
I: PartialEq + PartialOrd,
{
fn length(&self) -> Option<I> {
use validator::ValidateLength;
match self {
Self::Present(x) => x.length(),
Self::Null => None,
}
}
}
impl<T> From<Nullable<T>> for Option<T> {
fn from(value: Nullable<T>) -> Option<T> {
match value {
Nullable::Present(x) => Some(x),
Nullable::Null => None,
}
}
}
#[inline(never)]
#[cold]
fn expect_failed(msg: &str) -> ! {