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

Read #[validate(...)] attributes #78

Merged
merged 14 commits into from
Sep 18, 2021
Merged

Read #[validate(...)] attributes #78

merged 14 commits into from
Sep 18, 2021

Conversation

GREsau
Copy link
Owner

@GREsau GREsau commented Mar 29, 2021

Fixes #12

This PR makes #[derive(JsonSchema)] consume #[validate(...)] attributes that would typically be set when using the validator crate. In particular, it accepts:

  • #[validate(email)]/#[validate(phone)]/#[validate(url)] - sets the schema's format to "email"/"phone"/"uri" respectively
  • #[validate(length(min = 1, max = 100)]/#[validate(length(equal = 100)] - for string schemas, sets the minLength/maxLength properties. For Array schemas, sets the minItems/maxItems properties.
  • #[validate(range(min = 1, max = 100)] - for numbers schemas, sets the minimum/maximum properties
  • #[validate(regex = "path::to::regex"]) - for string schemas, sets the pattern property
  • #[validate(contains = "str"]) - for string schemas, sets the regex-escaped value to the pattern property (unless regex is also used). For object schemas, adds the value to the "required" property
  • #[validate(required)]/#[validate(required_nested)] creates a schema as though the field was not an Option

These attributes are only consumed on structs with named fields - not on tuple structs or newtype structs.

This PR does NOT handle these attributes available in the validator crate:

  • must_match
  • credit_card
  • custom
  • nested

@liamcurry
Copy link

liamcurry commented Apr 11, 2021

This is great, thank you! Are there plans to support tuple/newtype structs as well?

Also, would it be possible to support inline patterns with the regex attribute? e.g. #[validate(regex = "^\w+$")]

@GREsau
Copy link
Owner Author

GREsau commented Apr 11, 2021

Are there plans to support tuple/newtype structs as well?

Yep!

Also, would it be possible to support inline patterns with the regex attribute? e.g. #[validate(regex = "^\w+$")]

Yeah I'd like to do that - the validator crate that these attributes are based off of doesn't support inline patterns, although I did open a feature request for it recently: Keats/validator#149
The syntax would need to be a bit different to the "normal" regex attribute so that it's clear whether it's referring to a variable or an inline pattern. In the issue I suggested #[validate(regex(pattern = r"^[Hh]ello\b"))]. Alternatively, maybe an inline regex could be indicated by surrounding it with / characters e.g. #[validate(regex = r"/^[Hh]ello\b/")]

@liamcurry
Copy link

Is it important for the feature be added to the validator crate first, or can schemars experiment with custom #[validate] attributes? I would love to see the regex(pattern = ..) syntax you suggested in schemars.

@GREsau GREsau marked this pull request as ready for review September 18, 2021 20:24
@GREsau GREsau merged commit cec8751 into master Sep 18, 2021
@GREsau GREsau deleted the validate branch September 18, 2021 20:25
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

Successfully merging this pull request may close these issues.

Validation support
2 participants