From b0503a1a0e77e6733c13d63cd34c2cf1ee89d1d3 Mon Sep 17 00:00:00 2001 From: naskya Date: Fri, 12 Jul 2024 05:51:57 +0900 Subject: [PATCH] fix --- optionalize-derive/src/lib.rs | 2 +- src/main.rs | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/optionalize-derive/src/lib.rs b/optionalize-derive/src/lib.rs index 260e166..0e332f1 100644 --- a/optionalize-derive/src/lib.rs +++ b/optionalize-derive/src/lib.rs @@ -73,7 +73,7 @@ fn derive_impl(input: syn::DeriveInput) -> proc_macro2::TokenStream { #name: value.#name.ok_or(::optionalize::MissingRequiredField(stringify!(#name)))?.try_into()? }, (true, true) => quote! { - #name: value.#name.try_into()? + #name: value.#name.map(|val| val.try_into()).transpose().ok().flatten() }, } }); diff --git a/src/main.rs b/src/main.rs index 44ea252..87164c1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,17 @@ use optionalize::Optionalize; use validator::Validate; -#[derive(Validate, Optionalize)] +#[derive(Optionalize, Validate)] struct Foo { #[validate(nested)] #[opt(name = BarOpt)] bar: Bar, + #[validate(nested)] + #[opt(name = BarOpt)] + bar2: Option, } -#[derive(Validate, Optionalize)] +#[derive(Optionalize, Validate)] struct Bar { id: u16, }