@@ -3,7 +3,7 @@ import * as module from 'node:module';
33import { describe , expect , it } from 'vitest' ;
44import { z } from 'zod' ;
55
6- import { $BooleanLike , $NumberLike , $Uint8ArrayLike , $UrlLike , isZodType } from '../zod.js' ;
6+ import { $BooleanLike , $NumberLike , $Uint8ArrayLike , $UrlLike , isZodType , safeParse } from '../zod.js' ;
77
88const require = module . createRequire ( import . meta. url ) ;
99
@@ -116,3 +116,27 @@ describe('$Uint8ArrayLike', () => {
116116 expect ( [ ...result . data ! ] ) . toEqual ( [ 7 , 8 , 9 ] ) ;
117117 } ) ;
118118} ) ;
119+
120+ describe ( 'safeParse' , ( ) => {
121+ const $Schema = z . object ( { foo : z . enum ( [ '1' , '2' ] ) . transform ( Number ) } ) ;
122+ it ( 'should return an Ok result with the parsed data if successful' , ( ) => {
123+ const result = safeParse ( { foo : '1' } , $Schema ) ;
124+ expect ( result . isOk ( ) && result . value ) . toStrictEqual ( { foo : 1 } ) ;
125+ } ) ;
126+ it ( 'should return an Err result with the data and validation issues if unsuccessful' , ( ) => {
127+ const result = safeParse ( { foo : '0' } , $Schema ) ;
128+ expect ( result . isErr ( ) && result . error ) . toMatchObject ( {
129+ details : {
130+ data : {
131+ foo : '0'
132+ } ,
133+ issues : [
134+ expect . objectContaining ( {
135+ code : z . ZodIssueCode . invalid_enum_value ,
136+ path : [ 'foo' ]
137+ } )
138+ ]
139+ }
140+ } ) ;
141+ } ) ;
142+ } ) ;
0 commit comments