Generate schema from any serializable value (#75)
Implement schema_for_value!(...) macro
This commit is contained in:
parent
0957204bc1
commit
f6482fd460
19 changed files with 1179 additions and 5 deletions
|
@ -20,6 +20,8 @@ If you want more control over how the schema is generated, you can use the [`gen
|
|||
|
||||
See the API documentation for more info on how to use those types for custom schema generation.
|
||||
|
||||
<!-- TODO:
|
||||
<!-- TODO:
|
||||
create and link to example
|
||||
|
||||
Generating schema from example value
|
||||
-->
|
||||
|
|
24
docs/_includes/examples/from_value.rs
Normal file
24
docs/_includes/examples/from_value.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use schemars::schema_for_value;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct MyStruct {
|
||||
pub my_int: i32,
|
||||
pub my_bool: bool,
|
||||
pub my_nullable_enum: Option<MyEnum>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub enum MyEnum {
|
||||
StringNewType(String),
|
||||
StructVariant { floats: Vec<f32> },
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let schema = schema_for_value!(MyStruct {
|
||||
my_int: 123,
|
||||
my_bool: true,
|
||||
my_nullable_enum: Some(MyEnum::StringNewType("foo".to_string()))
|
||||
});
|
||||
println!("{}", serde_json::to_string_pretty(&schema).unwrap());
|
||||
}
|
24
docs/_includes/examples/from_value.schema.json
Normal file
24
docs/_includes/examples/from_value.schema.json
Normal file
|
@ -0,0 +1,24 @@
|
|||
{
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "MyStruct",
|
||||
"examples": [
|
||||
{
|
||||
"my_bool": true,
|
||||
"my_int": 123,
|
||||
"my_nullable_enum": {
|
||||
"StringNewType": "foo"
|
||||
}
|
||||
}
|
||||
],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"my_bool": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"my_int": {
|
||||
"type": "integer",
|
||||
"format": "int32"
|
||||
},
|
||||
"my_nullable_enum": true
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue