File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -222,4 +222,24 @@ describe('idl', () => {
222222 } ) ;
223223 } ) ;
224224 } ) ;
225+
226+ describe ( 'object with enum field with default' , ( ) => {
227+ const Schema = z . object ( {
228+ visibility : z
229+ . enum ( [ 'public' , 'friends_and_followers' , 'friends_only' ] )
230+ . default ( 'friends_only' )
231+ } ) ;
232+
233+ it ( 'converts enum with default to IDL' , ( ) => {
234+ expect ( schemaToIdl ( { schema : Schema , value : { visibility : 'friends_and_followers' } } ) ) . toEqual ( {
235+ visibility : { friends_and_followers : null }
236+ } ) ;
237+ } ) ;
238+
239+ it ( 'converts enum with default from IDL' , ( ) => {
240+ expect (
241+ schemaFromIdl ( { schema : Schema , value : { visibility : { friends_and_followers : null } } } )
242+ ) . toEqual ( { visibility : 'friends_and_followers' } ) ;
243+ } ) ;
244+ } ) ;
225245} ) ;
Original file line number Diff line number Diff line change @@ -69,6 +69,14 @@ export const schemaToIdl = ({schema, value}: IdlParams): unknown => {
6969 ) ;
7070 }
7171
72+ if ( schema instanceof z . ZodDefault ) {
73+ return schemaToIdl ( { schema : schema . _zod . def . innerType , value} ) ;
74+ }
75+
76+ if ( schema instanceof z . ZodEnum ) {
77+ return { [ value as string ] : null } ;
78+ }
79+
7280 return value ;
7381} ;
7482
@@ -147,5 +155,18 @@ export const schemaFromIdl = ({schema, value}: IdlParams): unknown => {
147155 ) ;
148156 }
149157
158+ if ( schema instanceof z . ZodDefault ) {
159+ return schemaFromIdl ( { schema : schema . _zod . def . innerType , value} ) ;
160+ }
161+
162+ if ( schema instanceof z . ZodEnum ) {
163+ const obj = value as Record < string , unknown > ;
164+ const keys = Object . keys ( obj ) ;
165+ if ( keys . length === 1 ) {
166+ return keys [ 0 ] ;
167+ }
168+ return value ;
169+ }
170+
150171 return value ;
151172} ;
You can’t perform that action at this time.
0 commit comments