@@ -10,6 +10,7 @@ import {
1010import Reference from '../Reference' ;
1111import type { AnySchema } from '../schema' ;
1212import isAbsent from './isAbsent' ;
13+ import { ResolveOptions } from '../Condition' ;
1314
1415export type PanicCallback = ( err : Error ) => void ;
1516
@@ -86,26 +87,21 @@ export default function createValidation(config: {
8687 abortEarly = schema . spec . abortEarly ,
8788 disableStackTrace = schema . spec . disableStackTrace ,
8889 } = options ;
89-
90- function resolve < T > ( item : T | Reference < T > ) {
91- return Ref . isRef ( item ) ? item . getValue ( value , parent , context ) : item ;
92- }
93-
90+ const resolveOptions = { value, parent, context } ;
9491 function createError ( overrides : CreateErrorOptions = { } ) {
95- const nextParams = {
96- value,
97- originalValue,
98- label : schema . spec . label ,
99- path : overrides . path || path ,
100- spec : schema . spec ,
101- disableStackTrace : overrides . disableStackTrace || disableStackTrace ,
102- ...params ,
103- ...overrides . params ,
104- } ;
105-
106- type Keys = ( keyof typeof nextParams ) [ ] ;
107- for ( const key of Object . keys ( nextParams ) as Keys )
108- nextParams [ key ] = resolve ( nextParams [ key ] ) ;
92+ const nextParams = resolveParams (
93+ {
94+ value,
95+ originalValue,
96+ label : schema . spec . label ,
97+ path : overrides . path || path ,
98+ spec : schema . spec ,
99+ disableStackTrace : overrides . disableStackTrace || disableStackTrace ,
100+ ...params ,
101+ ...overrides . params ,
102+ } ,
103+ resolveOptions ,
104+ ) ;
109105
110106 const error = new ValidationError (
111107 ValidationError . formatError ( overrides . message || message , nextParams ) ,
@@ -126,7 +122,9 @@ export default function createValidation(config: {
126122 type : name ,
127123 from : options . from ,
128124 createError,
129- resolve,
125+ resolve < T > ( item : T | Reference < T > ) {
126+ return resolveMaybeRef ( item , resolveOptions ) ;
127+ } ,
130128 options,
131129 originalValue,
132130 schema,
@@ -173,3 +171,24 @@ export default function createValidation(config: {
173171
174172 return validate ;
175173}
174+
175+ // Warning: mutates the input
176+ export function resolveParams < T extends ExtraParams > (
177+ params : T ,
178+ options : ResolveOptions ,
179+ ) {
180+ if ( ! params ) return params ;
181+
182+ type Keys = ( keyof typeof params ) [ ] ;
183+ for ( const key of Object . keys ( params ) as Keys ) {
184+ params [ key ] = resolveMaybeRef ( params [ key ] , options ) ;
185+ }
186+
187+ return params ;
188+ }
189+
190+ function resolveMaybeRef < T > ( item : T | Reference < T > , options : ResolveOptions ) {
191+ return Ref . isRef ( item )
192+ ? item . getValue ( options . value , options . parent , options . context )
193+ : item ;
194+ }
0 commit comments