@@ -8,6 +8,9 @@ pub struct ToJsonSchemaOptions {
88 /// Use union type (with `null`) for optional fields instead.
99 /// Models like OpenAI will reject the schema if a field is not required.
1010 pub fields_always_required : bool ,
11+
12+ /// If true, the JSON schema supports the `format` keyword.
13+ pub supports_format : bool ,
1114}
1215
1316pub trait ToJsonSchema {
@@ -49,15 +52,51 @@ impl ToJsonSchema for schema::BasicValueType {
4952 max_items : Some ( 2 ) ,
5053 ..Default :: default ( )
5154 } ) ) ;
52- schema
53- . metadata
54- . get_or_insert_with ( Default :: default)
55- . description =
55+ schema. metadata . get_or_insert_default ( ) . description =
5656 Some ( "A range, start pos (inclusive), end pos (exclusive)." . to_string ( ) ) ;
5757 }
5858 schema:: BasicValueType :: Uuid => {
5959 schema. instance_type = Some ( SingleOrVec :: Single ( Box :: new ( InstanceType :: String ) ) ) ;
60- schema. format = Some ( "uuid" . to_string ( ) ) ;
60+ if options. supports_format {
61+ schema. format = Some ( "uuid" . to_string ( ) ) ;
62+ } else {
63+ schema. metadata . get_or_insert_default ( ) . description =
64+ Some ( "A UUID, e.g. 123e4567-e89b-12d3-a456-426614174000" . to_string ( ) ) ;
65+ }
66+ }
67+ schema:: BasicValueType :: Date => {
68+ schema. instance_type = Some ( SingleOrVec :: Single ( Box :: new ( InstanceType :: String ) ) ) ;
69+ if options. supports_format {
70+ schema. format = Some ( "date" . to_string ( ) ) ;
71+ } else {
72+ schema. metadata . get_or_insert_default ( ) . description =
73+ Some ( "A date, e.g. 2025-03-27" . to_string ( ) ) ;
74+ }
75+ }
76+ schema:: BasicValueType :: Time => {
77+ schema. instance_type = Some ( SingleOrVec :: Single ( Box :: new ( InstanceType :: String ) ) ) ;
78+ if options. supports_format {
79+ schema. format = Some ( "time" . to_string ( ) ) ;
80+ } else {
81+ schema. metadata . get_or_insert_default ( ) . description =
82+ Some ( "A time, e.g. 13:32:12" . to_string ( ) ) ;
83+ }
84+ }
85+ schema:: BasicValueType :: LocalDateTime => {
86+ schema. instance_type = Some ( SingleOrVec :: Single ( Box :: new ( InstanceType :: String ) ) ) ;
87+ if options. supports_format {
88+ schema. format = Some ( "date-time" . to_string ( ) ) ;
89+ }
90+ schema. metadata . get_or_insert_default ( ) . description =
91+ Some ( "Date time without timezone offset, e.g. 2025-03-27T13:32:12" . to_string ( ) ) ;
92+ }
93+ schema:: BasicValueType :: OffsetDateTime => {
94+ schema. instance_type = Some ( SingleOrVec :: Single ( Box :: new ( InstanceType :: String ) ) ) ;
95+ if options. supports_format {
96+ schema. format = Some ( "date-time" . to_string ( ) ) ;
97+ }
98+ schema. metadata . get_or_insert_default ( ) . description =
99+ Some ( "Date time with timezone offset in RFC3339, e.g. 2025-03-27T13:32:12Z, 2025-03-27T07:32:12.313-06:00" . to_string ( ) ) ;
61100 }
62101 schema:: BasicValueType :: Json => {
63102 // Can be any value. No type constraint.
0 commit comments