File tree Expand file tree Collapse file tree 4 files changed +64
-6
lines changed Expand file tree Collapse file tree 4 files changed +64
-6
lines changed Original file line number Diff line number Diff line change @@ -156,6 +156,35 @@ test("superRefine", () => {
156
156
Strings . parse ( [ "asfd" , "qwer" ] ) ;
157
157
} ) ;
158
158
159
+ test ( "superRefine async" , async ( ) => {
160
+ const Strings = z . array ( z . string ( ) ) . superRefine ( async ( val , ctx ) => {
161
+ if ( val . length > 3 ) {
162
+ ctx . addIssue ( {
163
+ code : z . ZodIssueCode . too_big ,
164
+ maximum : 3 ,
165
+ type : "array" ,
166
+ inclusive : true ,
167
+ exact : true ,
168
+ message : "Too many items 😡" ,
169
+ } ) ;
170
+ }
171
+
172
+ if ( val . length !== new Set ( val ) . size ) {
173
+ ctx . addIssue ( {
174
+ code : z . ZodIssueCode . custom ,
175
+ message : `No duplicates allowed.` ,
176
+ } ) ;
177
+ }
178
+ } ) ;
179
+
180
+ const result = await Strings . safeParseAsync ( [ "asfd" , "asfd" , "asfd" , "asfd" ] ) ;
181
+
182
+ expect ( result . success ) . toEqual ( false ) ;
183
+ if ( ! result . success ) expect ( result . error . issues . length ) . toEqual ( 2 ) ;
184
+
185
+ Strings . parseAsync ( [ "asfd" , "qwer" ] ) ;
186
+ } ) ;
187
+
159
188
test ( "superRefine - type narrowing" , ( ) => {
160
189
type NarrowType = { type : string ; age : number } ;
161
190
const schema = z
Original file line number Diff line number Diff line change @@ -371,10 +371,10 @@ export abstract class ZodType<
371
371
refinement : ( arg : Output , ctx : RefinementCtx ) => arg is RefinedOutput
372
372
) : ZodEffects < this, RefinedOutput , Input > ;
373
373
superRefine (
374
- refinement : ( arg : Output , ctx : RefinementCtx ) => void
374
+ refinement : ( arg : Output , ctx : RefinementCtx ) => void | Promise < void >
375
375
) : ZodEffects < this, Output , Input > ;
376
376
superRefine (
377
- refinement : ( arg : Output , ctx : RefinementCtx ) => unknown
377
+ refinement : ( arg : Output , ctx : RefinementCtx ) => unknown | Promise < unknown >
378
378
) : ZodEffects < this, Output , Input > {
379
379
return this . _refinement ( refinement ) ;
380
380
}
@@ -4174,7 +4174,7 @@ export class ZodPromise<T extends ZodTypeAny> extends ZodType<
4174
4174
//////////////////////////////////////////////
4175
4175
4176
4176
export type Refinement < T > = ( arg : T , ctx : RefinementCtx ) => any ;
4177
- export type SuperRefinement < T > = ( arg : T , ctx : RefinementCtx ) => void ;
4177
+ export type SuperRefinement < T > = ( arg : T , ctx : RefinementCtx ) => void | Promise < void > ;
4178
4178
4179
4179
export type RefinementEffect < T > = {
4180
4180
type : "refinement" ;
Original file line number Diff line number Diff line change @@ -155,6 +155,35 @@ test("superRefine", () => {
155
155
Strings . parse ( [ "asfd" , "qwer" ] ) ;
156
156
} ) ;
157
157
158
+ test ( "superRefine async" , async ( ) => {
159
+ const Strings = z . array ( z . string ( ) ) . superRefine ( async ( val , ctx ) => {
160
+ if ( val . length > 3 ) {
161
+ ctx . addIssue ( {
162
+ code : z . ZodIssueCode . too_big ,
163
+ maximum : 3 ,
164
+ type : "array" ,
165
+ inclusive : true ,
166
+ exact : true ,
167
+ message : "Too many items 😡" ,
168
+ } ) ;
169
+ }
170
+
171
+ if ( val . length !== new Set ( val ) . size ) {
172
+ ctx . addIssue ( {
173
+ code : z . ZodIssueCode . custom ,
174
+ message : `No duplicates allowed.` ,
175
+ } ) ;
176
+ }
177
+ } ) ;
178
+
179
+ const result = await Strings . safeParseAsync ( [ "asfd" , "asfd" , "asfd" , "asfd" ] ) ;
180
+
181
+ expect ( result . success ) . toEqual ( false ) ;
182
+ if ( ! result . success ) expect ( result . error . issues . length ) . toEqual ( 2 ) ;
183
+
184
+ Strings . parseAsync ( [ "asfd" , "qwer" ] ) ;
185
+ } ) ;
186
+
158
187
test ( "superRefine - type narrowing" , ( ) => {
159
188
type NarrowType = { type : string ; age : number } ;
160
189
const schema = z
Original file line number Diff line number Diff line change @@ -371,10 +371,10 @@ export abstract class ZodType<
371
371
refinement : ( arg : Output , ctx : RefinementCtx ) => arg is RefinedOutput
372
372
) : ZodEffects < this, RefinedOutput , Input > ;
373
373
superRefine (
374
- refinement : ( arg : Output , ctx : RefinementCtx ) => void
374
+ refinement : ( arg : Output , ctx : RefinementCtx ) => void | Promise < void >
375
375
) : ZodEffects < this, Output , Input > ;
376
376
superRefine (
377
- refinement : ( arg : Output , ctx : RefinementCtx ) => unknown
377
+ refinement : ( arg : Output , ctx : RefinementCtx ) => unknown | Promise < unknown >
378
378
) : ZodEffects < this, Output , Input > {
379
379
return this . _refinement ( refinement ) ;
380
380
}
@@ -4174,7 +4174,7 @@ export class ZodPromise<T extends ZodTypeAny> extends ZodType<
4174
4174
//////////////////////////////////////////////
4175
4175
4176
4176
export type Refinement < T > = ( arg : T , ctx : RefinementCtx ) => any ;
4177
- export type SuperRefinement < T > = ( arg : T , ctx : RefinementCtx ) => void ;
4177
+ export type SuperRefinement < T > = ( arg : T , ctx : RefinementCtx ) => void | Promise < void > ;
4178
4178
4179
4179
export type RefinementEffect < T > = {
4180
4180
type : "refinement" ;
You can’t perform that action at this time.
0 commit comments