-
Notifications
You must be signed in to change notification settings - Fork 12
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
Enhancement request: make it possible to pass params to custom validators #19
Comments
Sorry, I am late to see this. custom is basically a representation of the method name only. I once tried a another idea, which I ended up not adopting. Couldn't we use other fields for validation of some field? struct A {
#[validate(custom(my_validation(val1, val2)))]
val1: i32,
val2: i32,
} Consider this in the Unnamed Struct. struct A {
#[validate(custom(my_validation(0, 1)))]
i32,
i32,
} I'm stumped!! For example, the following additions conflict with multiple field validation fn my_validation(val1: i32, param1: i32, param2: i32) {
...
}
struct A {
#[validate(custom(my_validation(1, 2)))]
i32
} Considering the use of web applications, To prevent such complexity from occurring, the design intentionally limits features.
Currently, functions must be created and used on a case-by-case basis. fn my_validation_0(val1: i32) {
my_validation(val1, 0)
} |
Today I upgraded to syn v2, and are now able to write by closure as follows. use serde_valid::Validate;
fn user_validation(_val: &i32, param1: bool) -> Result<(), serde_valid::validation::Error> {
Ok(())
}
#[derive(Validate)]
struct SampleStruct {
#[validate(custom(|v| user_validation(v, true)))]
val: i32,
}
let s = SampleStruct { val: 1 };
assert!(s.validate().is_ok()); It should be available in the next release. |
That looks excellent. Thanks.
…On Sun, 7 Jan 2024, 03:37 yassun7010, ***@***.***> wrote:
@maacl <https://github.com/maacl>
Today I upgraded to syn <https://crates.io/crates/syn> v2, and are now
able to write by closure as follows.
use serde_valid::Validate;
fn user_validation(_val: &i32, param1: bool) -> Result<(), serde_valid::validation::Error> {
Ok(())}
#[derive(Validate)]struct SampleStruct {
#[validate(custom(|v| user_validation(v, true)))]
val: i32,}
let s = SampleStruct { val: 1 };
assert!(s.validate().is_ok());
It should be available in the next release.
—
Reply to this email directly, view it on GitHub
<#19 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAPUXAVIMZY7Z3SA7EUF43YNIC53AVCNFSM6AAAAAA7Q53MSSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZZHEYTQOJWGY>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
It would be very useful if you could do:
#[validate(custom(user_validation = 24))]
Even better if you could pass a struct or vector with multiple values.
The text was updated successfully, but these errors were encountered: