diff --git a/typify-impl/src/lib.rs b/typify-impl/src/lib.rs index 2f791ba2..d3e4ae51 100644 --- a/typify-impl/src/lib.rs +++ b/typify-impl/src/lib.rs @@ -51,6 +51,8 @@ pub enum Error { type_name: Option, reason: String, }, + #[error("invalid root schema: {reason}")] + InvalidRootSchema { reason: String }, } impl Error { @@ -790,17 +792,21 @@ impl TypeSpace { definitions, } = schema; + let title = schema.metadata.as_ref().and_then(|m| m.title.as_ref()); + + if definitions.is_empty() && title.is_none() { + return Err(Error::InvalidRootSchema { + reason: "title is required when there are no definitions".to_string(), + }); + } + let mut defs = definitions .into_iter() .map(|(key, schema)| (RefKey::Def(key), schema)) .collect::>(); // Does the root type have a name (otherwise... ignore it) - let root_type = schema - .metadata - .as_ref() - .and_then(|m| m.title.as_ref()) - .is_some(); + let root_type = title.is_some(); if root_type { defs.push((RefKey::Root, schema.into()));