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

Swap validator for garde #1197

Closed
clux opened this issue Apr 11, 2023 · 3 comments · Fixed by #1212
Closed

Swap validator for garde #1197

clux opened this issue Apr 11, 2023 · 3 comments · Fixed by #1212
Assignees
Labels
derive kube-derive proc_macro related help wanted Not immediately prioritised, please help!

Comments

@clux
Copy link
Member

clux commented Apr 11, 2023

validator is basically dead and has been superseded by garde. Context:

Describe the solution you'd like

Swap out validator as a dev-dependency of kube and kube-derive (they are only used as illustrative examples in docs), plus fix the crd_api example to use garde::Validate. The biggest part is probably documenting the special cases in kube-derive/src/lib on how it interacts with schemars.

I expect this to not be too difficult, but we need to verify it actually works with schemars, because based on our earlier docs, schemars added special features to make it work there so not sure if this carries over direct (but i suspect so with the same names).

Target crate for feature

kube-derive (but also examples + kube)

@clux clux added help wanted Not immediately prioritised, please help! derive kube-derive proc_macro related labels Apr 11, 2023
@mateiidavid
Copy link
Contributor

I can pick this up if nobody has started work on it yet.

@clux
Copy link
Member Author

clux commented Apr 25, 2023

Go for it 👍

@mateiidavid
Copy link
Contributor

Quick update. Took me a bit longer to get up to speed, I went spelunking in the commit history to see where this all started from. I'm pretty much caught up. I played with both schemars and garde locally to see how well validation checks carry over from the latter to the former.

Aside from having a slightly different API (i.e. different attributes, different way of specifying attribute values) the two are mostly compatible.

#[derive(JsonSchema, Validate, Deserialize)]
struct User {
    #[validate(length(min = 3, max = 25))]
    #[garde(length(min = 3, max = 25))]
    name: String,
    #[validate(email)]
    #[garde(email)]
    email: String,
}

I tested with something similar to the above. Naively tested by creating a struct, generating a schema, and based on that schema created some json that I validated in the binary using garde.

The supported schemars attributes for validation are in this PR (AFAIK not much has changed since that got merged in).

Anyway, bottom line is that aside from the attributes that are listed in the PR above:

  • schemars won't be able to generate ip validation (expected, I guess)
  • schemars won't be able to add ascii validation
  • garde doesn't have a required attribute for validation. This is pretty ease to do with a custom validation though.

It seems to do the trick. I'll submit a PR to change the documentation and add some examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
derive kube-derive proc_macro related help wanted Not immediately prioritised, please help!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants