Comments (6)
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.
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.
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.
schema_for_value!(...)
won't be limited to serde_json::Value
s - 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.
Implemented and published! https://crates.io/crates/schemars/0.8.2
from schemars.
Nice work @GREsau
from schemars.
Related Issues (20)
- skip_serializing_if = "Vec::is_empty" still marked as required HOT 2
- Consider implementing JsonSchema for RootSchema
- make generic type name format configurable. HOT 1
- Could the generation be compatible with Prettier?
- How to auto generate references for enum variants HOT 1
- proposal: `#[schemars(example_value = "blabla")]`
- `#[doc = include_str!("../xx.md")]` or `#[schemars(description = include_str!("xx.md"))]` not work yet
- There are no such thing as null type in OpenAPI
- Non-compliant generated "format" values
- serde default value from function
- Support for Serde into and try_from container attributes
- Controlling referenceability in derived schemas
- failed to deserialize json from payload
- How to incorporate additional schema ?
- JsonSchema is not implemented for std::time::Instant
- Please add support for time::OffsetDateTime and perhaps other types from time crate HOT 1
- Traversal of the Schema tree
- Variant-level #[serde(untagged)] is not yet supported HOT 3
- Compiler error when using serde_with::with_prefix! HOT 2
- Is this crate unmaintained? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from schemars.