Skip to content
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

Serialize constraints #3

Open
jprochazk opened this issue Mar 26, 2023 · 2 comments
Open

Serialize constraints #3

jprochazk opened this issue Mar 26, 2023 · 2 comments
Labels
enhancement New feature or request
Milestone

Comments

@jprochazk
Copy link
Owner

jprochazk commented Mar 26, 2023

It would be useful to support getting a list of constraints on a struct and serializing them to JSON. A possible use-case is sharing the validations between the backend and frontend of a web app without having to manually keep them in sync.

#[derive(Validate)]
struct User {
  #[garde(required)]
  first_name: String,
  #[garde(required)]
  last_name: String,
  #[garde(length(max=100))]
  bio: String,
}

let constraints = <User as garde::Constraints>::constraints();

let serialized = serde_json::to_string(&constraints).unwrap();
{
  "first_name": [{"type":"required"}],
  "last_name": [{"type":"required"}],
  "bio": [{"type":"length","params":{"max":100}}]
}

Error messages should also support serde::Serialize. Some inspiration could be taken from similar libraries in other ecosystems, such as zod for TypeScript.

The output should respect rename.

@jprochazk jprochazk added the enhancement New feature or request label Mar 26, 2023
@jprochazk jprochazk added this to the v1.0.0 milestone Mar 26, 2023
@jprochazk jprochazk changed the title Serialize validation constraints Serialize of constraints and errors Apr 1, 2023
@jprochazk jprochazk changed the title Serialize of constraints and errors Serialize constraints and errors Apr 2, 2023
@jprochazk
Copy link
Owner Author

Serializing error reports was implemented in a989bcb. All that's missing is serializing constraints, which shouldn't be too difficult to implement:

  • It should be emitted as part of the Validate derive
  • It should be a straightforward translation of the model.

For example:

#[derive(garde::Validate)]
struct Foo {
    #[garde(length(min = 1))]
    s: String,
}

Would derive the following:

impl garde::Validate for Foo {
    // ... the usual validate impl
  
    fn constraints() -> ::garde::Constraints {
        use ::garde::constraints::*;
        
        Constraints::Struct(
            Struct {
                fields: vec![
                    (
                        "s".into(),
                        vec![
                            Rule::Length {
                                min: Some(1),
                                max: None
                            }
                        ],
                    )
                ]
            }
        )
    }
}

@jprochazk jprochazk changed the title Serialize constraints and errors Serialize constraints Mar 30, 2024
@jprochazk
Copy link
Owner Author

Thinking about this a bit more. Our support for arbitrary expr in certain rules throws a wrench into this being a fully compile-time thing.

If you have e.g. length(min=1), then that's a constant and we can embed it directly into the resulting serialized constraints. But given something like length(min=ctx.min), it's not clear how you'd serialize the ctx.min value if you don't have access to it without the user providing a context. Worse yet is length(min=self.min), which depends on the contents of self.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant