Implement JsonSchema for Bound
This commit is contained in:
parent
3f56d6b282
commit
5503f0697f
3 changed files with 69 additions and 2 deletions
|
@ -2,7 +2,7 @@ use crate::gen::SchemaGenerator;
|
||||||
use crate::schema::*;
|
use crate::schema::*;
|
||||||
use crate::JsonSchema;
|
use crate::JsonSchema;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::ops::{Range, RangeInclusive};
|
use std::ops::{Bound, Range, RangeInclusive};
|
||||||
|
|
||||||
impl<T: JsonSchema> JsonSchema for Option<T> {
|
impl<T: JsonSchema> JsonSchema for Option<T> {
|
||||||
no_ref_schema!();
|
no_ref_schema!();
|
||||||
|
@ -100,6 +100,38 @@ impl<T: JsonSchema, E: JsonSchema> JsonSchema for Result<T, E> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: JsonSchema> JsonSchema for Bound<T> {
|
||||||
|
fn schema_name() -> String {
|
||||||
|
format!("Bound_of_{}", T::schema_name())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
|
||||||
|
let mut included_schema = SchemaObject::default();
|
||||||
|
included_schema.instance_type = Some(InstanceType::Object.into());
|
||||||
|
included_schema.object().required.insert("Included".to_owned());
|
||||||
|
included_schema
|
||||||
|
.object()
|
||||||
|
.properties
|
||||||
|
.insert("Included".to_owned(), gen.subschema_for::<T>());
|
||||||
|
|
||||||
|
let mut excluded_schema = SchemaObject::default();
|
||||||
|
excluded_schema.instance_type = Some(InstanceType::Object.into());
|
||||||
|
excluded_schema.object().required.insert("Excluded".to_owned());
|
||||||
|
excluded_schema
|
||||||
|
.object()
|
||||||
|
.properties
|
||||||
|
.insert("Excluded".to_owned(), gen.subschema_for::<T>());
|
||||||
|
|
||||||
|
let mut unbounded_schema = SchemaObject::default();
|
||||||
|
unbounded_schema.instance_type = Some(InstanceType::String.into());
|
||||||
|
unbounded_schema.const_value = Some(json!("Unbounded"));
|
||||||
|
|
||||||
|
let mut schema = SchemaObject::default();
|
||||||
|
schema.subschemas().one_of = Some(vec![included_schema.into(), excluded_schema.into(), unbounded_schema.into()]);
|
||||||
|
schema.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: JsonSchema> JsonSchema for Range<T> {
|
impl<T: JsonSchema> JsonSchema for Range<T> {
|
||||||
fn schema_name() -> String {
|
fn schema_name() -> String {
|
||||||
format!("Range_of_{}", T::schema_name())
|
format!("Range_of_{}", T::schema_name())
|
||||||
|
|
|
@ -3,10 +3,14 @@
|
||||||
"title": "MyStruct",
|
"title": "MyStruct",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
|
"bound",
|
||||||
"inclusive",
|
"inclusive",
|
||||||
"range"
|
"range"
|
||||||
],
|
],
|
||||||
"properties": {
|
"properties": {
|
||||||
|
"bound": {
|
||||||
|
"$ref": "#/definitions/Bound_of_String"
|
||||||
|
},
|
||||||
"inclusive": {
|
"inclusive": {
|
||||||
"$ref": "#/definitions/Range_of_double"
|
"$ref": "#/definitions/Range_of_double"
|
||||||
},
|
},
|
||||||
|
@ -15,6 +19,36 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"definitions": {
|
"definitions": {
|
||||||
|
"Bound_of_String": {
|
||||||
|
"oneOf": [
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"Included"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"Included": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"required": [
|
||||||
|
"Excluded"
|
||||||
|
],
|
||||||
|
"properties": {
|
||||||
|
"Excluded": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "string",
|
||||||
|
"const": "Unbounded"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
"Range_of_double": {
|
"Range_of_double": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"required": [
|
"required": [
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
mod util;
|
mod util;
|
||||||
use schemars::JsonSchema;
|
use schemars::JsonSchema;
|
||||||
|
use std::ops::{Bound, Range, RangeInclusive};
|
||||||
use util::*;
|
use util::*;
|
||||||
use std::ops::{Range, RangeInclusive};
|
|
||||||
|
|
||||||
#[derive(Debug, JsonSchema)]
|
#[derive(Debug, JsonSchema)]
|
||||||
struct MyStruct {
|
struct MyStruct {
|
||||||
range: Range<usize>,
|
range: Range<usize>,
|
||||||
inclusive: RangeInclusive<f64>,
|
inclusive: RangeInclusive<f64>,
|
||||||
|
bound: Bound<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue