Allow boolean "trivial" schemas
This commit is contained in:
parent
1cc9bb5564
commit
3c03d910ca
2 changed files with 53 additions and 29 deletions
|
@ -23,10 +23,11 @@ macro_rules! simple_impl {
|
||||||
($type:tt => $instance_type:expr) => {
|
($type:tt => $instance_type:expr) => {
|
||||||
impl MakeSchema for $type {
|
impl MakeSchema for $type {
|
||||||
fn make_schema() -> Schema {
|
fn make_schema() -> Schema {
|
||||||
Schema {
|
SchemaObject {
|
||||||
instance_type: Some($instance_type.into()),
|
instance_type: Some($instance_type.into()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -44,11 +45,12 @@ impl MakeSchema for char {
|
||||||
let mut extra_properties = Map::new();
|
let mut extra_properties = Map::new();
|
||||||
extra_properties.insert("minLength".to_owned(), json!(1));
|
extra_properties.insert("minLength".to_owned(), json!(1));
|
||||||
extra_properties.insert("maxLength".to_owned(), json!(1));
|
extra_properties.insert("maxLength".to_owned(), json!(1));
|
||||||
Schema {
|
SchemaObject {
|
||||||
instance_type: Some(InstanceType::String.into()),
|
instance_type: Some(InstanceType::String.into()),
|
||||||
extra_properties,
|
extra_properties,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,11 +64,12 @@ macro_rules! int_impl {
|
||||||
// this may be overkill...
|
// this may be overkill...
|
||||||
extra_properties.insert("minimum".to_owned(), json!($type::min_value()));
|
extra_properties.insert("minimum".to_owned(), json!($type::min_value()));
|
||||||
extra_properties.insert("maximum".to_owned(), json!($type::max_value()));
|
extra_properties.insert("maximum".to_owned(), json!($type::max_value()));
|
||||||
Schema {
|
SchemaObject {
|
||||||
instance_type: Some(InstanceType::Integer.into()),
|
instance_type: Some(InstanceType::Integer.into()),
|
||||||
extra_properties,
|
extra_properties,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -92,11 +95,12 @@ impl<T> MakeSchema for [T; 0] {
|
||||||
fn make_schema() -> Schema {
|
fn make_schema() -> Schema {
|
||||||
let mut extra_properties = Map::new();
|
let mut extra_properties = Map::new();
|
||||||
extra_properties.insert("maxItems".to_owned(), json!(0));
|
extra_properties.insert("maxItems".to_owned(), json!(0));
|
||||||
Schema {
|
SchemaObject {
|
||||||
instance_type: Some(InstanceType::Array.into()),
|
instance_type: Some(InstanceType::Array.into()),
|
||||||
extra_properties,
|
extra_properties,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
|
.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,12 +113,12 @@ macro_rules! array_impls {
|
||||||
let mut extra_properties = Map::new();
|
let mut extra_properties = Map::new();
|
||||||
extra_properties.insert("minItems".to_owned(), json!($len));
|
extra_properties.insert("minItems".to_owned(), json!($len));
|
||||||
extra_properties.insert("maxItems".to_owned(), json!($len));
|
extra_properties.insert("maxItems".to_owned(), json!($len));
|
||||||
Schema {
|
SchemaObject {
|
||||||
instance_type: Some(InstanceType::Array.into()),
|
instance_type: Some(InstanceType::Array.into()),
|
||||||
items: Some(Box::from(T::make_schema())),
|
items: Some(Box::from(T::make_schema())),
|
||||||
extra_properties,
|
extra_properties,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)+
|
)+
|
||||||
|
@ -138,11 +142,11 @@ macro_rules! seq_impl {
|
||||||
{
|
{
|
||||||
fn make_schema() -> Schema
|
fn make_schema() -> Schema
|
||||||
{
|
{
|
||||||
Schema {
|
SchemaObject {
|
||||||
instance_type: Some(InstanceType::Array.into()),
|
instance_type: Some(InstanceType::Array.into()),
|
||||||
items: Some(Box::from(T::make_schema())),
|
items: Some(Box::from(T::make_schema())),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -171,11 +175,11 @@ macro_rules! map_impl {
|
||||||
"additionalProperties".to_owned(),
|
"additionalProperties".to_owned(),
|
||||||
json!(T::make_schema())
|
json!(T::make_schema())
|
||||||
);
|
);
|
||||||
Schema {
|
SchemaObject {
|
||||||
instance_type: Some(InstanceType::Object.into()),
|
instance_type: Some(InstanceType::Object.into()),
|
||||||
extra_properties,
|
extra_properties,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -188,15 +192,15 @@ map_impl!(<K, T: Eq + core::hash::Hash, H: core::hash::BuildHasher> MakeSchema f
|
||||||
|
|
||||||
impl<T: MakeSchema> MakeSchema for Option<T> {
|
impl<T: MakeSchema> MakeSchema for Option<T> {
|
||||||
fn make_schema() -> Schema {
|
fn make_schema() -> Schema {
|
||||||
let mut schema = T::make_schema();
|
match T::make_schema() {
|
||||||
if let Some(instance_type) = schema.instance_type {
|
Schema::Bool(true) => true.into(),
|
||||||
let mut vec: Vec<_> = instance_type.into();
|
Schema::Bool(false) => <()>::make_schema(),
|
||||||
if !vec.contains(&InstanceType::Null) {
|
Schema::Object(schema) => SchemaObject {
|
||||||
vec.push(InstanceType::Null);
|
any_of: Some(vec![schema.into(), <()>::make_schema()]),
|
||||||
|
..Default::default()
|
||||||
}
|
}
|
||||||
schema.instance_type = Some(vec.into());
|
.into(),
|
||||||
}
|
}
|
||||||
schema
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -226,6 +230,6 @@ deref_impl!(<'a, T: ToOwned> MakeSchema for std::borrow::Cow<'a, T>);
|
||||||
|
|
||||||
impl MakeSchema for serde_json::Value {
|
impl MakeSchema for serde_json::Value {
|
||||||
fn make_schema() -> Schema {
|
fn make_schema() -> Schema {
|
||||||
Schema::default()
|
true.into()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,22 +2,34 @@ use serde::{Deserialize, Serialize};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use std::collections::BTreeMap as Map;
|
use std::collections::BTreeMap as Map;
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
|
#[serde(untagged)]
|
||||||
|
pub enum Schema {
|
||||||
|
Bool(bool),
|
||||||
|
Object(SchemaObject),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<SchemaObject> for Schema {
|
||||||
|
fn from(o: SchemaObject) -> Self {
|
||||||
|
Schema::Object(o)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<bool> for Schema {
|
||||||
|
fn from(b: bool) -> Self {
|
||||||
|
Schema::Bool(b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
#[derive(Serialize, Deserialize, Debug, Default)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct RootSchema {
|
pub struct SchemaObject {
|
||||||
#[serde(rename = "$schema", skip_serializing_if = "Option::is_none")]
|
#[serde(rename = "$schema", skip_serializing_if = "Option::is_none")]
|
||||||
pub schema: Option<String>,
|
pub schema: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub title: Option<String>,
|
pub title: Option<String>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
#[serde(flatten)]
|
|
||||||
pub root: Schema,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Default)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct Schema {
|
|
||||||
#[serde(rename = "$id", skip_serializing_if = "Option::is_none")]
|
#[serde(rename = "$id", skip_serializing_if = "Option::is_none")]
|
||||||
pub id: Option<String>,
|
pub id: Option<String>,
|
||||||
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
|
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
|
||||||
|
@ -26,6 +38,14 @@ pub struct Schema {
|
||||||
pub instance_enum: Option<Vec<Value>>,
|
pub instance_enum: Option<Vec<Value>>,
|
||||||
#[serde(skip_serializing_if = "Option::is_none")]
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
pub items: Option<Box<Schema>>,
|
pub items: Option<Box<Schema>>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub all_of: Option<Vec<Schema>>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub any_of: Option<Vec<Schema>>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub one_of: Option<Vec<Schema>>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub not: Option<Box<Schema>>,
|
||||||
#[serde(skip_serializing_if = "Map::is_empty")]
|
#[serde(skip_serializing_if = "Map::is_empty")]
|
||||||
pub definitions: Map<String, Schema>,
|
pub definitions: Map<String, Schema>,
|
||||||
#[serde(flatten)]
|
#[serde(flatten)]
|
||||||
|
@ -75,14 +95,14 @@ impl<T> Into<Vec<T>> for SingleOrVec<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*pub struct Schema {
|
/*pub struct SchemaObject {
|
||||||
pub ref_path: Option<String>,
|
pub ref_path: Option<String>,
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
pub schema_type: Option<String>,
|
pub schema_type: Option<String>,
|
||||||
pub format: Option<String>,
|
pub format: Option<String>,
|
||||||
pub enum_values: Option<Vec<String>>,
|
pub enum_values: Option<Vec<String>>,
|
||||||
pub required: Option<Vec<String>>,
|
pub required: Option<Vec<String>>,
|
||||||
pub items: Option<Box<Schema>>,
|
pub items: Option<Box<SchemaObject>>,
|
||||||
pub properties: Option<std::collections::BTreeMap<String, Schema>>,
|
pub properties: Option<std::collections::BTreeMap<String, SchemaObject>>,
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue