yassun7010 / serde_valid

JSON Schema based validation tool using serde.
MIT License
46 stars 10 forks source link

Enhancement request: make it possible to pass params to custom validators #19

Closed maacl closed 9 months ago

maacl commented 11 months ago

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.

yassun7010 commented 9 months ago

@maacl

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? (In fact, an early implementation did just that)

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!! We can't determine if the integer is a field number in an unnamed structure or a value to a function argument.

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, The above ideas are practical, but cannot be co-located with those idea.

To prevent such complexity from occurring, the design intentionally limits features.

custom: Validation of a single field rule: Validation of multiple fields

Currently, functions must be created and used on a case-by-case basis.

fn my_validation_0(val1: i32) {
    my_validation(val1, 0)
}
yassun7010 commented 9 months ago

@maacl

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.

maacl commented 9 months ago

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 https://github.com/yassun7010/serde_valid/issues/19#issuecomment-1879918966, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAPUXAVIMZY7Z3SA7EUF43YNIC53AVCNFSM6AAAAAA7Q53MSSVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTQNZZHEYTQOJWGY . You are receiving this because you were mentioned.Message ID: @.***>

yassun7010 commented 9 months ago

@maacl

I released v0.17.0.