diff --git a/typify-impl/src/convert.rs b/typify-impl/src/convert.rs index 17c2f18a..e03e1af5 100644 --- a/typify-impl/src/convert.rs +++ b/typify-impl/src/convert.rs @@ -220,10 +220,15 @@ impl TypeSpace { } // Boolean + // + // The formats that JSON Schema and OpenAPI define apply to strings + // and numbers. JSON Schema Validation section 7.1 says a format + // SHOULD succeed for an instance type it does not apply to, so the + // boolean, object and array arms ignore it. SchemaObject { metadata, instance_type: Some(SingleOrVec::Single(single)), - format: None, + format: _, enum_values: _, const_value: None, subschemas: None, @@ -239,7 +244,7 @@ impl TypeSpace { SchemaObject { metadata, instance_type: Some(SingleOrVec::Single(single)), - format: None, + format: _, enum_values: None, const_value: None, subschemas: None, @@ -273,7 +278,7 @@ impl TypeSpace { SchemaObject { metadata, instance_type: Some(SingleOrVec::Single(single)), - format: None, + format: _, enum_values: None, const_value: None, subschemas: None, @@ -307,7 +312,7 @@ impl TypeSpace { SchemaObject { metadata, instance_type: Some(SingleOrVec::Single(single)), - format: None, + format: _, enum_values: None, const_value: None, subschemas: None, diff --git a/typify/tests/schemas/format-on-non-strings.json b/typify/tests/schemas/format-on-non-strings.json new file mode 100644 index 00000000..84069bd7 --- /dev/null +++ b/typify/tests/schemas/format-on-non-strings.json @@ -0,0 +1,57 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "definitions": { + "date-of-birth": { + "description": "an object that carries a string format", + "type": "object", + "format": "date", + "properties": { + "day": { + "type": "integer", + "format": "uint8" + }, + "month": { + "type": "integer", + "format": "uint8" + }, + "year": { + "type": "integer", + "format": "uint16" + } + }, + "required": [ + "day", + "month", + "year" + ] + }, + "maybe-date-of-birth": { + "type": [ + "object", + "null" + ], + "format": "date", + "properties": { + "year": { + "type": "integer", + "format": "uint16" + } + } + }, + "flag": { + "type": "boolean", + "format": "yes-no" + }, + "tags": { + "type": "array", + "format": "csv", + "items": { + "type": "string" + } + }, + "anything-list": { + "type": "array", + "format": "csv" + } + } +} diff --git a/typify/tests/schemas/format-on-non-strings.rs b/typify/tests/schemas/format-on-non-strings.rs new file mode 100644 index 00000000..254b12a9 --- /dev/null +++ b/typify/tests/schemas/format-on-non-strings.rs @@ -0,0 +1,251 @@ +#![deny(warnings)] +#[doc = "`AnythingList`"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +#[serde(transparent)] +pub struct AnythingList(pub ::std::vec::Vec<::serde_json::Value>); +impl ::std::ops::Deref for AnythingList { + type Target = ::std::vec::Vec<::serde_json::Value>; + fn deref(&self) -> &::std::vec::Vec<::serde_json::Value> { + &self.0 + } +} +impl ::std::convert::From for ::std::vec::Vec<::serde_json::Value> { + fn from(value: AnythingList) -> Self { + value.0 + } +} +impl ::std::convert::From<::std::vec::Vec<::serde_json::Value>> for AnythingList { + fn from(value: ::std::vec::Vec<::serde_json::Value>) -> Self { + Self(value) + } +} +#[doc = "an object that carries a string format"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +pub struct DateOfBirth { + pub day: u8, + pub month: u8, + pub year: u16, +} +impl DateOfBirth { + pub fn builder() -> builder::DateOfBirth { + Default::default() + } +} +#[doc = "`Flag`"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +#[serde(transparent)] +pub struct Flag(pub bool); +impl ::std::ops::Deref for Flag { + type Target = bool; + fn deref(&self) -> &bool { + &self.0 + } +} +impl ::std::convert::From for bool { + fn from(value: Flag) -> Self { + value.0 + } +} +impl ::std::convert::From for Flag { + fn from(value: bool) -> Self { + Self(value) + } +} +impl ::std::fmt::Display for Flag { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result { + self.0.fmt(f) + } +} +impl ::std::str::FromStr for Flag { + type Err = ::Err; + fn from_str(value: &str) -> ::std::result::Result { + Ok(Self(value.parse()?)) + } +} +#[doc = "`MaybeDateOfBirth`"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +#[serde(transparent)] +pub struct MaybeDateOfBirth(pub ::std::option::Option); +impl ::std::ops::Deref for MaybeDateOfBirth { + type Target = ::std::option::Option; + fn deref(&self) -> &::std::option::Option { + &self.0 + } +} +impl ::std::convert::From for ::std::option::Option { + fn from(value: MaybeDateOfBirth) -> Self { + value.0 + } +} +impl ::std::convert::From<::std::option::Option> for MaybeDateOfBirth { + fn from(value: ::std::option::Option) -> Self { + Self(value) + } +} +#[doc = "`MaybeDateOfBirthInner`"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug, Default)] +pub struct MaybeDateOfBirthInner { + #[serde(skip_serializing_if = "::std::option::Option::is_none")] + pub year: ::std::option::Option, +} +impl MaybeDateOfBirthInner { + pub fn builder() -> builder::MaybeDateOfBirthInner { + Default::default() + } +} +#[doc = "`Tags`"] +#[derive(:: serde :: Deserialize, :: serde :: Serialize, Clone, Debug)] +#[serde(transparent)] +pub struct Tags(pub ::std::vec::Vec<::std::string::String>); +impl ::std::ops::Deref for Tags { + type Target = ::std::vec::Vec<::std::string::String>; + fn deref(&self) -> &::std::vec::Vec<::std::string::String> { + &self.0 + } +} +impl ::std::convert::From for ::std::vec::Vec<::std::string::String> { + fn from(value: Tags) -> Self { + value.0 + } +} +impl ::std::convert::From<::std::vec::Vec<::std::string::String>> for Tags { + fn from(value: ::std::vec::Vec<::std::string::String>) -> Self { + Self(value) + } +} +#[doc = " Types for composing complex structures."] +pub mod builder { + #[derive(Clone, Debug)] + pub struct DateOfBirth { + day: ::std::result::Result, + month: ::std::result::Result, + year: ::std::result::Result, + } + impl ::std::default::Default for DateOfBirth { + fn default() -> Self { + Self { + day: Err("no value supplied for day".to_string()), + month: Err("no value supplied for month".to_string()), + year: Err("no value supplied for year".to_string()), + } + } + } + impl DateOfBirth { + pub fn day(mut self, value: T) -> Self + where + T: ::std::convert::TryInto, + T::Error: ::std::fmt::Display, + { + self.day = value + .try_into() + .map_err(|e| format!("error converting supplied value for day: {e}")); + self + } + pub fn month(mut self, value: T) -> Self + where + T: ::std::convert::TryInto, + T::Error: ::std::fmt::Display, + { + self.month = value + .try_into() + .map_err(|e| format!("error converting supplied value for month: {e}")); + self + } + pub fn year(mut self, value: T) -> Self + where + T: ::std::convert::TryInto, + T::Error: ::std::fmt::Display, + { + self.year = value + .try_into() + .map_err(|e| format!("error converting supplied value for year: {e}")); + self + } + } + impl ::std::convert::TryFrom for super::DateOfBirth { + type Error = super::error::ConversionError; + fn try_from( + value: DateOfBirth, + ) -> ::std::result::Result { + Ok(Self { + day: value.day?, + month: value.month?, + year: value.year?, + }) + } + } + impl ::std::convert::From for DateOfBirth { + fn from(value: super::DateOfBirth) -> Self { + Self { + day: Ok(value.day), + month: Ok(value.month), + year: Ok(value.year), + } + } + } + #[derive(Clone, Debug)] + pub struct MaybeDateOfBirthInner { + year: ::std::result::Result<::std::option::Option, ::std::string::String>, + } + impl ::std::default::Default for MaybeDateOfBirthInner { + fn default() -> Self { + Self { + year: Ok(Default::default()), + } + } + } + impl MaybeDateOfBirthInner { + pub fn year(mut self, value: T) -> Self + where + T: ::std::convert::TryInto<::std::option::Option>, + T::Error: ::std::fmt::Display, + { + self.year = value + .try_into() + .map_err(|e| format!("error converting supplied value for year: {e}")); + self + } + } + impl ::std::convert::TryFrom for super::MaybeDateOfBirthInner { + type Error = super::error::ConversionError; + fn try_from( + value: MaybeDateOfBirthInner, + ) -> ::std::result::Result { + Ok(Self { year: value.year? }) + } + } + impl ::std::convert::From for MaybeDateOfBirthInner { + fn from(value: super::MaybeDateOfBirthInner) -> Self { + Self { + year: Ok(value.year), + } + } + } +} +#[doc = " Error types."] +pub mod error { + #[doc = r" Error from a `TryFrom` or `FromStr` implementation."] + pub struct ConversionError(::std::borrow::Cow<'static, str>); + impl ::std::error::Error for ConversionError {} + impl ::std::fmt::Display for ConversionError { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> { + ::std::fmt::Display::fmt(&self.0, f) + } + } + impl ::std::fmt::Debug for ConversionError { + fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> Result<(), ::std::fmt::Error> { + ::std::fmt::Debug::fmt(&self.0, f) + } + } + impl From<&'static str> for ConversionError { + fn from(value: &'static str) -> Self { + Self(value.into()) + } + } + impl From for ConversionError { + fn from(value: String) -> Self { + Self(value.into()) + } + } +} +fn main() {}