-
Notifications
You must be signed in to change notification settings - Fork 158
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
Custom fields with custom validation for reuse between structures. #239
Comments
Validation errors take a &str as key to report errors. What would be the key used in the first struct, disregarding the following 2? |
The first struct is not meant to be used directly to validate itself when out of structure. This is just a thin wrapper that allows having validation rules separately from the struct. If |
@Keats |
I'm not sure tbh. The derive is due for a full rewrite so I'm wondering if there's a cleaner way to handle the marshmallow custom |
@Keats That is what I have seen in serde. They treat References: serde-rs/serde#1309 and serde-rs/serde#104 |
Maybe just use indices? 0, 1, ...? |
Can you elaborate? I don't see how indices can be used here? |
Sorry that this was confusing. @victorcrimea I was mostly answering to @Keats first comment:
While I think handling a single item wrapped struct is great I think we might be able to just solve it for all unnamed structs like this: #[derive(Debug, Deserialize, Serialize, Validate)]
pub struct LocaleName(
#[validate(length(equal = 2))]
pub String,
#[validate(...)]
pub String,
); |
Marshmallow has an option of defining custom field types that can be reused multiple times. This significantly reduces logic errors when you have to reuse the same field over and over again across the whole business logic.
Right now there is a limitation that structs annotated with
#validate
should have only named fields.Is it possible to remove that limitation for a case when a structure has only one unnamed field and nothing else. And in this case validate the inner data instead?
Below is the code example that utilizes this feature: both
UpdateLocaleRequest
andGetLocaleResponse
use the same typeLocaleName
which has a validation defined on the struct and propagated to fields that use this type by having#[validate(customtype)]
The text was updated successfully, but these errors were encountered: