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

Validation support #12

Closed
patriksvensson opened this issue Jan 24, 2020 · 8 comments · Fixed by #78
Closed

Validation support #12

patriksvensson opened this issue Jan 24, 2020 · 8 comments · Fixed by #78

Comments

@patriksvensson
Copy link

Hello again, sorry for cluttering your inbox with GitHub issues.

I've just taken a quick look so far, but I see that the source code for Schemars support validations like pattern for strings (https://github.com/GREsau/schemars/blob/master/schemars/src/schema.rs#L364). Is there any way of adding validations to a schema? I couldn't find any information on the site about this, so that's why I'm asking.

@GREsau
Copy link
Owner

GREsau commented Feb 15, 2020

There's nothing like this implemented yet, but it is something I've thought about.

One way of specifying validations that #[derive(JsonSchema)] could hook into would be using something like the validator crate's validate attributes.

Any fields with a validate attribute could have the appropriate JSON Schema validation properties and/or format property set. e.g.

lazy_static! {
    static ref USERNAME_RE: Regex = Regex::new(r"^\w{2,}$").unwrap();
}

#[derive(JsonSchema)]
pub struct User{
  #[validate(regex = "USERNAME_RE")]
  pub username: String,
  #[validate(email)]
  pub email: String,
}

Could produce a schema like:

{
  "title": "User",
  "type": "object",
  "properties": {
    "username": {
      "type": "string",
      "pattern": "^\\w{2,}$"
    },
    "email": {
      "type": "string",
      "format": "email"
    }
  }
}

Alternatively, I could just add more options to #[schemars(...)] attributes to allow setting more properties on the generated schema

@patriksvensson
Copy link
Author

@GREsau I like the approach with the validate attribute!

I would be happy to try to submit a pull request for this unless this is something you want to do yourself (if it's something that you would consider adding to Schemars that is).

@GREsau
Copy link
Owner

GREsau commented Feb 29, 2020

Sure, I'd be happy to accept a PR for this

@tamasfe
Copy link
Contributor

tamasfe commented Jun 4, 2020

@GREsau @patriksvensson I've made some effort towards validation in #40. If that approach is accepted, I'd advise just setting fields with schemars(max_items = 1, ...) so that integration is still possible with validator.

@tamasfe tamasfe mentioned this issue Jun 10, 2020
6 tasks
@tamasfe
Copy link
Contributor

tamasfe commented Jun 11, 2020

I've created a validation library based on #40 that provides integration with Schemars. Types can validate themselves by generating a schema and validating against it. The generated schema just has to contain all validation-related fields.

I could start working on a PR to set those fields with attributes.

What would be the best way to get info about fields in the derive macro? I would personally like to deny attributes that make no sense for a type.

@sbihel
Copy link

sbihel commented Jul 8, 2020

Would it make sense to use Valico's DSL to generate the schema?

  1. It would be inherently consistent with the goal of this project.
  2. It might be possible to reduce the glue and pass attributes' options generically.
  3. And going with custom #[schemars(...)] attributes might be the safest choice in case the validator crate, or any other generic validator, were to have conflicts with the JSONSchema standard?

@MarcAntoine-Arnaud
Copy link

Hello,

I'm interested into this feature too.
I'm just wondering if it's not a SerDe feature ?

For me I expect more to define something like:

#[derive(Serialize, Deserialize, JsonSchema)]
pub struct User{
  #[serde(regex = "^\w{2,}$", getter = true, setter = true)]
  username: String,
}

So like that Serde can generate:

  • Deserializer with internal check while parsing a file
  • implement a setter to update the value, validating first the regex

Like that Schemars can use that parameter to ad the right field in the generated schema.
Does it make sense ?

@GREsau
Copy link
Owner

GREsau commented Sep 19, 2021

This is finally available in v0.8.4 😃
https://crates.io/crates/schemars/0.8.4

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 a pull request may close this issue.

5 participants