@@ -489,6 +489,39 @@ describe("toCodecAnthropic", () => {
489489 await decoding . succeed ( { "a" : "a" , "b" : 1 } )
490490 await decoding . succeed ( { "a" : "a" , "b" : null } , { a : "a" } )
491491 } )
492+
493+ it ( "optional properties that already accept null" , async ( ) => {
494+ const schema = Schema . Struct ( {
495+ a : Schema . optional ( Schema . Finite ) . annotate ( { description : "Optional number" } ) ,
496+ b : Schema . optionalKey ( Schema . NullOr ( Schema . Finite ) )
497+ } )
498+ assertJsonSchema ( schema , {
499+ type : "object" ,
500+ properties : {
501+ a : { anyOf : [ { type : "number" } , { type : "null" } ] , description : "Optional number" } ,
502+ b : { anyOf : [ { type : "number" } , { type : "null" } ] }
503+ } ,
504+ required : [ "a" , "b" ] ,
505+ additionalProperties : false
506+ } )
507+ const asserts = new TestSchema . Asserts ( toCodecAnthropic ( schema ) . codec )
508+ await asserts . encoding ( ) . succeed ( { a : 1 , b : 2 } )
509+ await asserts . encoding ( ) . succeed ( { } , { a : null , b : null } )
510+ await asserts . decoding ( ) . succeed ( { a : 1 , b : 2 } )
511+ await asserts . decoding ( ) . succeed ( { a : null , b : null } , { } )
512+ } )
513+
514+ it ( "optional checked nullable properties preserve absent keys" , async ( ) => {
515+ const checked = Schema . NullOr ( Schema . Record ( Schema . String , Schema . Finite ) )
516+ . check ( Schema . makeFilter ( ( value ) => value !== null , { expected : "non-null" } ) )
517+ const schema = Schema . Struct ( {
518+ a : Schema . optionalKey ( checked ) ,
519+ b : Schema . optionalKey ( Schema . Union ( [ Schema . String , checked ] ) )
520+ } )
521+ const asserts = new TestSchema . Asserts ( toCodecAnthropic ( schema ) . codec )
522+ await asserts . encoding ( ) . succeed ( { } , { a : null , b : null } )
523+ await asserts . decoding ( ) . succeed ( { a : null , b : null } , { } )
524+ } )
492525 } )
493526
494527 it ( "Class" , ( ) => {
0 commit comments