Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,13 @@ where
F: Fn(&serde_json::Value) -> Result<T, crate::error::VldError>,
{
fn json_schema(&self) -> Value {
// Nested schemas are opaque closures; emit a generic object schema.
serde_json::json!({"type": "object"})
match self.name {
Some(name) => serde_json::json!({
"$ref": format!("#/components/schemas/{}", name)
}),
// Nested schemas are opaque closures; emit a generic object schema
None => serde_json::json!({ "type": "object" }),
}
}
}

Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ where
schema::NestedSchema::new(f)
}

pub fn nested_named<T, F>(name: &'static str, f: F) -> schema::NestedSchema<T, F>
where
F: Fn(&serde_json::Value) -> Result<T, error::VldError>,
{
schema::NestedSchema::new_named(f, name)
}

#[macro_export]
macro_rules! nested {
($ty:ty) => {
$crate::nested_named(stringify!($ty), <$ty>::parse_value)
};
}

/// Create a literal value schema. Validates exact match.
///
/// ```
Expand Down
13 changes: 11 additions & 2 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ where
F: Fn(&Value) -> Result<T, VldError>,
{
parse_fn: F,
pub(crate) name: Option<&'static str>,
_phantom: PhantomData<T>,
}

Expand All @@ -209,6 +210,15 @@ where
pub fn new(f: F) -> Self {
Self {
parse_fn: f,
name: None,
_phantom: PhantomData,
}
}

pub fn new_named(f: F, name: &'static str) -> Self {
Self {
parse_fn: f,
name: Some(name),
_phantom: PhantomData,
}
}
Expand All @@ -219,8 +229,7 @@ where
F: Fn(&Value) -> Result<T, VldError>,
{
type Output = T;

fn parse_value(&self, value: &Value) -> Result<T, VldError> {
(self.parse_fn)(value)
}
}
}
Loading