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

Add support for garde crate as alternative to validator #233

Closed
jirutka opened this issue Jul 26, 2023 · 3 comments
Closed

Add support for garde crate as alternative to validator #233

jirutka opened this issue Jul 26, 2023 · 3 comments

Comments

@jirutka
Copy link
Contributor

jirutka commented Jul 26, 2023

Can you please consider adding support for garde as an alternative to validator? It’s better designed than validator. The reasons for starting a new project are mentioned in Keats/validator#201.

@Wicpar
Copy link

Wicpar commented Aug 1, 2023

+1 this request, validator does not support enums.
In the mean time validator can be replaced, structs will just have duplicate attrs with #[garde] and #[validate]

@GREsau
Copy link
Owner

GREsau commented Aug 24, 2024

This seems doable. Looking through the current garde attributes, I think we can respect most of them:

  • #[garde(required)] - same as validate(required)
  • #[garde(ascii)] - use regex pattern ^[\x00-\x7f]*$ (or ^\p{ASCII}*$, but that is less-widely supported)
  • #[garde(alphanumeric)] - use regex pattern ^[\p{Alpha}\p{N}]*$?
  • #[garde(email)] - same as validate(email) (set "format": "email")
  • #[garde(url)] - same as validate(url) (set "format": "uri")
  • #[garde(ip)] - set "format": "ip" (not a standard JSON Schema format, but is consistent with the JsonSchema impl for IpAddr)
  • #[garde(ipv4)] - set "format": "ipv4"
  • #[garde(ipv6)] - set "format": "ipv6"
  • #[garde(length(<mode>, min=<usize>, max=<usize>, equal=<usize>)] - similar to validate(length(...)), but with the extra "mode". How to handle that?
  • #[garde(range(min=<expr>, max=<expr>, equal=<expr>))] - similar to validate(range(...)), but also supports "equal" (why?)
  • #[garde(contains(<string>))] - similar to validate(contains = ...)
  • #[garde(pattern("<regex>"))] - similar to schemars(regex(pattern = ...)) (not supported by validator)
  • #[garde(pattern(<matcher>))] - similar to validate(regex(path = ...))
  • #[garde(prefix(<string>))] - similar to "contains", we can just prefix the pattern with ^
  • #[garde(suffix(<string>))] - similar to "contains", we can just suffix the pattern with $
  • #[garde(credit_card)] - ignore
  • #[garde(phone_number)] - ignore (schemars currently supports validate(phone) but this was removed from the validator crate - probably remove from schemars too?)
  • #[garde(matches(<field>))] - ignore
  • #[garde(dive)] - ignore
  • #[garde(skip)] - ignore
  • #[garde(custom(<function or closure>))] - ignore
  • #[garde(inner(...))] - same as schemars(inner) (not supported by validator)

@GREsau
Copy link
Owner

GREsau commented Aug 29, 2024

This is now implemented in schemars 1.0.0-alpha.14

Most, but not all, of the attributes I listed above are now supported - see the updated docs for a full list: https://graham.cool/schemars/deriving/attributes/#supported-validatorgarde-attributes

@GREsau GREsau closed this as completed Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants