Coder Social home page Coder Social logo

Comments (6)

GREsau avatar GREsau commented on May 22, 2024 2

This will be possible in the next version - this code:

let data = r#"
        {
            "name": "John Doe",
            "age": 43,
            "phones": [
                "+44 1234567",
                "+44 2345678"
            ]
        }"#;
let v: serde_json::Value = serde_json::from_str(data)?;
let schema = schema_for_value!(v);
println!("{}", serde_json::to_string_pretty(&schema)?);

Produces this schema:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "examples": [
    {
      "age": 43,
      "name": "John Doe",
      "phones": [
        "+44 1234567",
        "+44 2345678"
      ]
    }
  ],
  "type": "object",
  "properties": {
    "age": {
      "type": "integer"
    },
    "name": {
      "type": "string"
    },
    "phones": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  }
}

from schemars.

ahl avatar ahl commented on May 22, 2024

Pretty sure this library doesn't handle that case. It's great for Type -> JsonSchema, but you're deriving the schema from the data rather than from the type. That said, it would be pretty straightforward to build a function to extract a schema from the Value.

from schemars.

GREsau avatar GREsau commented on May 22, 2024

This functionality isn't currently implemented, but I would like to do something like this. And actually I'd prefer to widen it to allow generating a schema from an instance any type that implements serde::Serialize (which includes serde_json::value::Value). Then you can get a schema from any value that can be serialized, even if it doesn't implement JsonSchema. And there would be more type information available than if it was done via a Value, allowing a richer schema.

from schemars.

GREsau avatar GREsau commented on May 22, 2024

schema_for_value!(...) won't be limited to serde_json::Values - as I said in my earlier comment, any type that implements serde::Serialize can have a schema generated from a value, e.g. from_value.rs / from_value.schema.json

from schemars.

GREsau avatar GREsau commented on May 22, 2024

Implemented and published! https://crates.io/crates/schemars/0.8.2

from schemars.

jacobsvante avatar jacobsvante commented on May 22, 2024

Nice work @GREsau 😀👍🏻

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.