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

how to generate json schema from serde_json::value::Value? #47

Closed
clojurians-org opened this issue Aug 12, 2020 · 6 comments · Fixed by #75
Closed

how to generate json schema from serde_json::value::Value? #47

clojurians-org opened this issue Aug 12, 2020 · 6 comments · Fixed by #75
Milestone

Comments

@clojurians-org
Copy link

i want to generate json schema from dynamic json value.
i don't know whether schemars support this function.

    let data = r#"
        {
            "name": "John Doe",
            "age": 43,
            "phones": [
                "+44 1234567",
                "+44 2345678"
            ]
        }"#;
    let v: Value = serde_json::from_str(data)? ;
@ahl
Copy link
Contributor

ahl commented Aug 29, 2020

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.

@GREsau
Copy link
Owner

GREsau commented Mar 22, 2021

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.

@GREsau
Copy link
Owner

GREsau commented Mar 25, 2021

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"
      }
    }
  }
}

@GREsau
Copy link
Owner

GREsau commented Mar 25, 2021

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

@GREsau
Copy link
Owner

GREsau commented Mar 27, 2021

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

@GREsau GREsau closed this as completed Mar 27, 2021
@jacobsvante
Copy link

Nice work @GREsau 😀👍🏻

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.

4 participants