Coder Social home page Coder Social logo

Validation support about schemars HOT 8 CLOSED

gresau avatar gresau commented on May 22, 2024 1
Validation support

from schemars.

Comments (8)

GREsau avatar GREsau commented on May 22, 2024 6

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

from schemars.

GREsau avatar GREsau commented on May 22, 2024

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

from schemars.

patriksvensson avatar patriksvensson commented on May 22, 2024

@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).

from schemars.

GREsau avatar GREsau commented on May 22, 2024

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

from schemars.

tamasfe avatar tamasfe commented on May 22, 2024

@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.

from schemars.

tamasfe avatar tamasfe commented on May 22, 2024

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.

from schemars.

sbihel avatar sbihel commented on May 22, 2024

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?

from schemars.

MarcAntoine-Arnaud avatar MarcAntoine-Arnaud commented on May 22, 2024

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 ?

from schemars.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.