File tree 3 files changed +60
-5
lines changed
3 files changed +60
-5
lines changed Original file line number Diff line number Diff line change @@ -2682,7 +2682,7 @@ Note that branded types do not affect the runtime result of `.parse`. It is a st
2682
2682
This method returns a ` ZodReadonly ` schema instance that parses the input using the base schema, then calls ` Object.freeze() ` on the result. The inferred type is also marked as ` readonly ` .
2683
2683
2684
2684
``` ts
2685
- const schema = z .object ({ name: string }).readonly ();
2685
+ const schema = z .object ({ name: z . string () }).readonly ();
2686
2686
type schema = z .infer <typeof schema >;
2687
2687
// Readonly<{name: string}>
2688
2688
Original file line number Diff line number Diff line change @@ -202,3 +202,53 @@ test("object freezing", () => {
202
202
)
203
203
) . toBe ( true ) ;
204
204
} ) ;
205
+
206
+ test ( "async object freezing" , async ( ) => {
207
+ expect (
208
+ Object . isFrozen ( await z . array ( z . string ( ) ) . readonly ( ) . parseAsync ( [ "a" ] ) )
209
+ ) . toBe ( true ) ;
210
+ expect (
211
+ Object . isFrozen (
212
+ await z . tuple ( [ z . string ( ) , z . number ( ) ] ) . readonly ( ) . parseAsync ( [ "a" , 1 ] )
213
+ )
214
+ ) . toBe ( true ) ;
215
+ expect (
216
+ Object . isFrozen (
217
+ await z
218
+ . map ( z . string ( ) , z . date ( ) )
219
+ . readonly ( )
220
+ . parseAsync ( new Map ( [ [ "a" , new Date ( ) ] ] ) )
221
+ )
222
+ ) . toBe ( true ) ;
223
+ expect (
224
+ Object . isFrozen (
225
+ await z
226
+ . set ( z . promise ( z . string ( ) ) )
227
+ . readonly ( )
228
+ . parseAsync ( new Set ( [ Promise . resolve ( "a" ) ] ) )
229
+ )
230
+ ) . toBe ( true ) ;
231
+ expect (
232
+ Object . isFrozen (
233
+ await z . record ( z . string ( ) ) . readonly ( ) . parseAsync ( { a : "b" } )
234
+ )
235
+ ) . toBe ( true ) ;
236
+ expect (
237
+ Object . isFrozen (
238
+ await z . record ( z . string ( ) , z . number ( ) ) . readonly ( ) . parseAsync ( { a : 1 } )
239
+ )
240
+ ) . toBe ( true ) ;
241
+ expect (
242
+ Object . isFrozen (
243
+ await z
244
+ . object ( { a : z . string ( ) , 1 : z . number ( ) } )
245
+ . readonly ( )
246
+ . parseAsync ( { a : "b" , 1 : 2 } )
247
+ )
248
+ ) . toBe ( true ) ;
249
+ expect (
250
+ Object . isFrozen (
251
+ await z . promise ( z . string ( ) ) . readonly ( ) . parseAsync ( Promise . resolve ( "a" ) )
252
+ )
253
+ ) . toBe ( true ) ;
254
+ } ) ;
Original file line number Diff line number Diff line change @@ -5041,10 +5041,15 @@ export class ZodReadonly<T extends ZodTypeAny> extends ZodType<
5041
5041
> {
5042
5042
_parse ( input : ParseInput ) : ParseReturnType < this[ "_output" ] > {
5043
5043
const result = this . _def . innerType . _parse ( input ) ;
5044
- if ( isValid ( result ) ) {
5045
- result . value = Object . freeze ( result . value ) ;
5046
- }
5047
- return result ;
5044
+ const freeze = ( data : ParseReturnType < this[ "_output" ] > ) => {
5045
+ if ( isValid ( data ) ) {
5046
+ data . value = Object . freeze ( data . value ) ;
5047
+ }
5048
+ return data ;
5049
+ } ;
5050
+ return isAsync ( result )
5051
+ ? result . then ( ( data ) => freeze ( data ) )
5052
+ : freeze ( result ) ;
5048
5053
}
5049
5054
5050
5055
static create = < T extends ZodTypeAny > (
You can’t perform that action at this time.
0 commit comments