@@ -1022,6 +1022,12 @@ describe("toJsonSchema", () => {
10221022 expect ( toJsonSchema ( s . enum ( [ "x" , "y" ] , "A choice" ) ) ) . toEqual ( { type : "string" , enum : [ "x" , "y" ] , description : "A choice" } ) ;
10231023 } ) ;
10241024
1025+ it ( "converts literal schemas" , ( ) => {
1026+ expect ( toJsonSchema ( s . literal ( "done" ) ) ) . toEqual ( { type : "string" , enum : [ "done" ] } ) ;
1027+ expect ( toJsonSchema ( s . literal ( 42 ) ) ) . toEqual ( { enum : [ 42 ] } ) ;
1028+ expect ( toJsonSchema ( s . literal ( true , "must be true" ) ) ) . toEqual ( { enum : [ true ] , description : "must be true" } ) ;
1029+ } ) ;
1030+
10251031 it ( "adds type:string to all-string enums but not mixed enums" , ( ) => {
10261032 expect ( toJsonSchema ( s . enum ( "low" , "medium" , "high" ) ) ) . toEqual ( { type : "string" , enum : [ "low" , "medium" , "high" ] } ) ;
10271033 expect ( toJsonSchema ( s . enum ( 1 , 2 , 3 ) ) ) . toEqual ( { enum : [ 1 , 2 , 3 ] } ) ;
@@ -1180,3 +1186,34 @@ describe("s.null", () => {
11801186 expect ( result . ok ) . toBe ( true ) ;
11811187 } ) ;
11821188} ) ;
1189+
1190+ describe ( "s.literal" , ( ) => {
1191+ it ( "serializes a string literal to an all-string enum schema" , ( ) => {
1192+ expect ( toJsonSchema ( s . literal ( "done" ) ) ) . toEqual ( { type : "string" , enum : [ "done" ] } ) ;
1193+ } ) ;
1194+
1195+ it ( "serializes a number literal to an enum schema without type:string" , ( ) => {
1196+ expect ( toJsonSchema ( s . literal ( 42 ) ) ) . toEqual ( { enum : [ 42 ] } ) ;
1197+ } ) ;
1198+
1199+ it ( "accepts the exact literal value in validation" , ( ) => {
1200+ const result = analyzeResponse ( JSON . stringify ( "done" ) , s . literal ( "done" ) , "test" , 1 ) ;
1201+ expect ( result . ok ) . toBe ( true ) ;
1202+ } ) ;
1203+
1204+ it ( "rejects a different value in validation" , ( ) => {
1205+ const result = analyzeResponse ( JSON . stringify ( "pending" ) , s . literal ( "done" ) , "test" , 1 ) ;
1206+ expect ( result . ok ) . toBe ( false ) ;
1207+ if ( ! result . ok ) {
1208+ expect ( result . error . message ) . toContain ( '"done"' ) ;
1209+ }
1210+ } ) ;
1211+
1212+ it ( "supports an optional description" , ( ) => {
1213+ expect ( toJsonSchema ( s . literal ( "active" , "must be active" ) ) ) . toEqual ( {
1214+ type : "string" ,
1215+ enum : [ "active" ] ,
1216+ description : "must be active" ,
1217+ } ) ;
1218+ } ) ;
1219+ } ) ;
0 commit comments