-
-
Notifications
You must be signed in to change notification settings - Fork 243
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
Comments
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. |
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 |
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"
}
}
}
} |
|
Implemented and published! https://crates.io/crates/schemars/0.8.2 |
Nice work @GREsau 😀👍🏻 |
i want to generate json schema from dynamic json value.
i don't know whether schemars support this function.
The text was updated successfully, but these errors were encountered: