Skip to content

Commit 93f8001

Browse files
committedJul 15, 2023
fix: do not warn if the validation is for removed paths closes #4368
1 parent 42096b7 commit 93f8001

File tree

3 files changed

+73
-54
lines changed

3 files changed

+73
-54
lines changed
 

Diff for: ‎.changeset/eleven-fans-sin.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vee-validate': patch
3+
---
4+
5+
fix: do not warn if the validation is for removed paths closes #4368

Diff for: ‎packages/vee-validate/src/types/forms.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export type SchemaValidationMode = 'validated-only' | 'silent' | 'force';
6262

6363
export interface ValidationOptions {
6464
mode: SchemaValidationMode;
65+
warn: boolean;
6566
}
6667

6768
export type FieldValidator = (opts?: Partial<ValidationOptions>) => Promise<ValidationResult>;
@@ -148,7 +149,7 @@ export type FieldContext<TValue = unknown> = Omit<PrivateFieldContext<TValue>, '
148149

149150
export type GenericValidateFunction<TValue = unknown> = (
150151
value: TValue,
151-
ctx: FieldValidationMetaInfo
152+
ctx: FieldValidationMetaInfo,
152153
) => MaybePromise<boolean | MaybeArray<string>>;
153154

154155
export interface FormState<TValues> {
@@ -186,7 +187,7 @@ export interface SubmissionContext<TValues extends GenericObject = GenericObject
186187

187188
export type SubmissionHandler<TValues extends GenericObject = GenericObject, TOutput = TValues, TReturn = unknown> = (
188189
values: TOutput,
189-
ctx: SubmissionContext<TValues>
190+
ctx: SubmissionContext<TValues>,
190191
) => TReturn;
191192

192193
export interface InvalidSubmissionContext<TValues extends GenericObject = GenericObject> {
@@ -197,7 +198,7 @@ export interface InvalidSubmissionContext<TValues extends GenericObject = Generi
197198
}
198199

199200
export type InvalidSubmissionHandler<TValues extends GenericObject = GenericObject> = (
200-
ctx: InvalidSubmissionContext<TValues>
201+
ctx: InvalidSubmissionContext<TValues>,
201202
) => void;
202203

203204
export type RawFormSchema<TValues> = Record<Path<TValues>, string | GenericValidateFunction | GenericObject>;
@@ -208,7 +209,7 @@ export type FieldPathLookup<TValues extends GenericObject = GenericObject> = Par
208209

209210
type HandleSubmitFactory<TValues extends GenericObject, TOutput = TValues> = <TReturn = unknown>(
210211
cb: SubmissionHandler<TValues, TOutput, TReturn>,
211-
onSubmitValidationErrorCb?: InvalidSubmissionHandler<TValues>
212+
onSubmitValidationErrorCb?: InvalidSubmissionHandler<TValues>,
212213
) => (e?: Event) => Promise<TReturn | undefined>;
213214

214215
export interface PrivateFormContext<TValues extends GenericObject = GenericObject, TOutput = TValues>
@@ -235,11 +236,11 @@ export interface PrivateFormContext<TValues extends GenericObject = GenericObjec
235236
setFieldInitialValue(path: string, value: unknown): void;
236237
useFieldModel<TPath extends Path<TValues>>(path: TPath): Ref<PathValue<TValues, TPath>>;
237238
useFieldModel<TPaths extends readonly [...MaybeRef<Path<TValues>>[]]>(
238-
paths: TPaths
239+
paths: TPaths,
239240
): MapValuesPathsToRefs<TValues, TPaths>;
240241
createPathState<TPath extends Path<TValues>>(
241242
path: MaybeRef<TPath>,
242-
config?: Partial<PathStateConfig>
243+
config?: Partial<PathStateConfig>,
243244
): PathState<PathValue<TValues, TPath>>;
244245
getPathState<TPath extends Path<TValues>>(path: TPath): PathState<PathValue<TValues, TPath>> | undefined;
245246
getAllPathStates(): PathState[];
@@ -265,7 +266,7 @@ export interface ComponentBindsConfig<TValue = unknown, TExtraProps extends Gene
265266
}
266267

267268
export type LazyComponentBindsConfig<TValue = unknown, TExtraProps extends GenericObject = GenericObject> = (
268-
state: PublicPathState<TValue>
269+
state: PublicPathState<TValue>,
269270
) => Partial<{
270271
props: TExtraProps;
271272
validateOnBlur: boolean;
@@ -288,7 +289,7 @@ export interface InputBindsConfig<TValue = unknown, TExtraProps extends GenericO
288289
}
289290

290291
export type LazyInputBindsConfig<TValue = unknown, TExtraProps extends GenericObject = GenericObject> = (
291-
state: PublicPathState<TValue>
292+
state: PublicPathState<TValue>,
292293
) => Partial<{
293294
attrs: TExtraProps;
294295
validateOnBlur: boolean;
@@ -321,17 +322,17 @@ export interface FormContext<TValues extends GenericObject = GenericObject, TOut
321322
defineComponentBinds<
322323
TPath extends Path<TValues>,
323324
TValue = PathValue<TValues, TPath>,
324-
TExtras extends GenericObject = GenericObject
325+
TExtras extends GenericObject = GenericObject,
325326
>(
326327
path: MaybeRefOrGetter<TPath>,
327-
config?: Partial<ComponentBindsConfig<TValue, TExtras>> | LazyComponentBindsConfig<TValue, TExtras>
328+
config?: Partial<ComponentBindsConfig<TValue, TExtras>> | LazyComponentBindsConfig<TValue, TExtras>,
328329
): Ref<BaseComponentBinds<TValue> & TExtras>;
329330
defineInputBinds<
330331
TPath extends Path<TValues>,
331332
TValue = PathValue<TValues, TPath>,
332-
TExtras extends GenericObject = GenericObject
333+
TExtras extends GenericObject = GenericObject,
333334
>(
334335
path: MaybeRefOrGetter<TPath>,
335-
config?: Partial<InputBindsConfig<TValue, TExtras>> | LazyInputBindsConfig<TValue, TExtras>
336+
config?: Partial<InputBindsConfig<TValue, TExtras>> | LazyInputBindsConfig<TValue, TExtras>,
336337
): Ref<BaseInputBinds<TValue> & TExtras>;
337338
}

Diff for: ‎packages/vee-validate/src/useForm.ts

+55-42
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export interface FormOptions<
8282
TOutput = TValues,
8383
TSchema extends TypedSchema<TValues, TOutput> | FormSchema<TValues> =
8484
| FormSchema<TValues>
85-
| TypedSchema<TValues, TOutput>
85+
| TypedSchema<TValues, TOutput>,
8686
> {
8787
validationSchema?: MaybeRef<TSchema extends TypedSchema ? TypedSchema<TValues, TOutput> : any>;
8888
initialValues?: MaybeRef<PartialDeep<TValues> | undefined | null>;
@@ -111,7 +111,7 @@ export function useForm<
111111
TOutput = TValues,
112112
TSchema extends FormSchema<TValues> | TypedSchema<TValues, TOutput> =
113113
| FormSchema<TValues>
114-
| TypedSchema<TValues, TOutput>
114+
| TypedSchema<TValues, TOutput>,
115115
>(opts?: FormOptions<TValues, TOutput, TSchema>): FormContext<TValues, TOutput> {
116116
const formId = FORM_COUNTER++;
117117

@@ -201,19 +201,25 @@ export function useForm<
201201
* Holds a computed reference to all fields names and labels
202202
*/
203203
const fieldNames = computed(() => {
204-
return pathStates.value.reduce((names, state) => {
205-
names[state.path] = { name: state.path || '', label: state.label || '' };
204+
return pathStates.value.reduce(
205+
(names, state) => {
206+
names[state.path] = { name: state.path || '', label: state.label || '' };
206207

207-
return names;
208-
}, {} as Record<string, { name: string; label: string }>);
208+
return names;
209+
},
210+
{} as Record<string, { name: string; label: string }>,
211+
);
209212
});
210213

211214
const fieldBailsMap = computed(() => {
212-
return pathStates.value.reduce((map, state) => {
213-
map[state.path] = state.bails ?? true;
215+
return pathStates.value.reduce(
216+
(map, state) => {
217+
map[state.path] = state.bails ?? true;
214218

215-
return map;
216-
}, {} as Record<string, boolean>);
219+
return map;
220+
},
221+
{} as Record<string, boolean>,
222+
);
217223
});
218224

219225
// mutable non-reactive reference to initial errors
@@ -228,7 +234,7 @@ export function useForm<
228234
const { initialValues, originalInitialValues, setInitialValues } = useFormInitialValues<TValues>(
229235
pathStates,
230236
formValues,
231-
opts
237+
opts,
232238
);
233239

234240
// form meta aggregations
@@ -247,7 +253,7 @@ export function useForm<
247253

248254
function createPathState<TValue>(
249255
path: MaybeRefOrGetter<Path<TValues>>,
250-
config?: Partial<PathStateConfig>
256+
config?: Partial<PathStateConfig>,
251257
): PathState<TValue> {
252258
const initialValue = computed(() => getFromPath(initialValues.value, toValue(path)));
253259
const pathStateExists = pathStates.value.find(s => isPathsEqual(s.path, toValue(path)));
@@ -380,9 +386,9 @@ export function useForm<
380386

381387
return validation;
382388
},
383-
{ valid: formResult.valid, results: {}, errors: {} } as FormValidationResult<TValues>
389+
{ valid: formResult.valid, results: {}, errors: {} } as FormValidationResult<TValues>,
384390
);
385-
}
391+
},
386392
);
387393

388394
function mutateAllPathState(mutation: (state: PathState) => void) {
@@ -398,15 +404,18 @@ export function useForm<
398404
function findHoistedPath(path: Path<TValues>) {
399405
const candidates = pathStates.value.filter(state => path.startsWith(state.path));
400406

401-
return candidates.reduce((bestCandidate, candidate) => {
402-
if (!bestCandidate) {
403-
return candidate as PathState<PathValue<TValues, Path<TValues>>>;
404-
}
407+
return candidates.reduce(
408+
(bestCandidate, candidate) => {
409+
if (!bestCandidate) {
410+
return candidate as PathState<PathValue<TValues, Path<TValues>>>;
411+
}
405412

406-
return (candidate.path.length > bestCandidate.path.length ? candidate : bestCandidate) as PathState<
407-
PathValue<TValues, Path<TValues>>
408-
>;
409-
}, undefined as PathState<PathValue<TValues, Path<TValues>>> | undefined);
413+
return (candidate.path.length > bestCandidate.path.length ? candidate : bestCandidate) as PathState<
414+
PathValue<TValues, Path<TValues>>
415+
>;
416+
},
417+
undefined as PathState<PathValue<TValues, Path<TValues>>> | undefined,
418+
);
410419
}
411420

412421
let UNSET_BATCH: Path<TValues>[] = [];
@@ -431,7 +440,7 @@ export function useForm<
431440
function makeSubmissionFactory(onlyControlled: boolean) {
432441
return function submitHandlerFactory<TReturn = unknown>(
433442
fn?: SubmissionHandler<TValues, TOutput, TReturn>,
434-
onValidationError?: InvalidSubmissionHandler<TValues>
443+
onValidationError?: InvalidSubmissionHandler<TValues>,
435444
) {
436445
return function submissionHandler(e: unknown) {
437446
if (e instanceof Event) {
@@ -489,7 +498,7 @@ export function useForm<
489498

490499
// re-throw the err so it doesn't go silent
491500
throw err;
492-
}
501+
},
493502
);
494503
};
495504
};
@@ -507,7 +516,7 @@ export function useForm<
507516
}
508517

509518
nextTick(() => {
510-
validateField(path, { mode: 'silent' });
519+
validateField(path, { mode: 'silent', warn: false });
511520
});
512521

513522
if (pathState.multiple && pathState.fieldsCount) {
@@ -583,7 +592,7 @@ export function useForm<
583592
function setFieldValue<T extends Path<TValues>>(
584593
field: T | PathState,
585594
value: PathValue<TValues, T> | undefined,
586-
shouldValidate = true
595+
shouldValidate = true,
587596
) {
588597
const clonedValue = deepCopy(value);
589598
const path = typeof field === 'string' ? field : (field.path as Path<TValues>);
@@ -632,7 +641,7 @@ export function useForm<
632641

633642
function useFieldModel<TPath extends Path<TValues>>(path: TPath): Ref<PathValue<TValues, TPath>>;
634643
function useFieldModel<TPaths extends readonly [...MaybeRef<Path<TValues>>[]]>(
635-
paths: TPaths
644+
paths: TPaths,
636645
): MapValuesPathsToRefs<TValues, TPaths>;
637646
function useFieldModel<TPaths extends Path<TValues> | readonly [...MaybeRef<Path<TValues>>[]]>(pathOrPaths: TPaths) {
638647
if (!Array.isArray(pathOrPaths)) {
@@ -723,7 +732,7 @@ export function useForm<
723732
errors: result.errors,
724733
};
725734
});
726-
})
735+
}),
727736
);
728737

729738
isValidating.value = false;
@@ -764,7 +773,8 @@ export function useForm<
764773
return state.validate(opts);
765774
}
766775

767-
if (!state) {
776+
const shouldWarn = !state && (opts?.warn ?? true);
777+
if (shouldWarn) {
768778
warn(`field with path ${path} was not found`);
769779
}
770780

@@ -863,17 +873,17 @@ export function useForm<
863873
refreshInspector,
864874
{
865875
deep: true,
866-
}
876+
},
867877
);
868878
}
869879

870880
function defineComponentBinds<
871881
TPath extends Path<TValues>,
872882
TValue = PathValue<TValues, TPath>,
873-
TExtras extends GenericObject = GenericObject
883+
TExtras extends GenericObject = GenericObject,
874884
>(
875885
path: MaybeRefOrGetter<TPath>,
876-
config?: Partial<ComponentBindsConfig<TValue, TExtras>> | LazyComponentBindsConfig<TValue, TExtras>
886+
config?: Partial<ComponentBindsConfig<TValue, TExtras>> | LazyComponentBindsConfig<TValue, TExtras>,
877887
) {
878888
const pathState = findPathState(toValue(path)) || createPathState(path);
879889
const evalConfig = () => (isCallable(config) ? config(omit(pathState, PRIVATE_PATH_STATE_KEYS)) : config || {});
@@ -927,10 +937,10 @@ export function useForm<
927937
function defineInputBinds<
928938
TPath extends Path<TValues>,
929939
TValue = PathValue<TValues, TPath>,
930-
TExtras extends GenericObject = GenericObject
940+
TExtras extends GenericObject = GenericObject,
931941
>(
932942
path: MaybeRefOrGetter<TPath>,
933-
config?: Partial<InputBindsConfig<TValue, TExtras>> | LazyInputBindsConfig<TValue, TExtras>
943+
config?: Partial<InputBindsConfig<TValue, TExtras>> | LazyInputBindsConfig<TValue, TExtras>,
934944
) {
935945
const pathState = (findPathState(toValue(path)) || createPathState(path)) as PathState<TValue>;
936946
const evalConfig = () => (isCallable(config) ? config(omit(pathState, PRIVATE_PATH_STATE_KEYS)) : config || {});
@@ -1000,7 +1010,7 @@ function useFormMeta<TValues extends Record<string, unknown>>(
10001010
pathsState: Ref<PathState<unknown>[]>,
10011011
currentValues: TValues,
10021012
initialValues: MaybeRef<PartialDeep<TValues>>,
1003-
errors: Ref<FormErrors<TValues>>
1013+
errors: Ref<FormErrors<TValues>>,
10041014
) {
10051015
const MERGE_STRATEGIES: Record<keyof Pick<FieldMeta<unknown>, 'touched' | 'pending' | 'valid'>, 'every' | 'some'> = {
10061016
touched: 'some',
@@ -1015,12 +1025,15 @@ function useFormMeta<TValues extends Record<string, unknown>>(
10151025
function calculateFlags() {
10161026
const states = pathsState.value;
10171027

1018-
return keysOf(MERGE_STRATEGIES).reduce((acc, flag) => {
1019-
const mergeMethod = MERGE_STRATEGIES[flag];
1020-
acc[flag] = states[mergeMethod](s => s[flag]);
1028+
return keysOf(MERGE_STRATEGIES).reduce(
1029+
(acc, flag) => {
1030+
const mergeMethod = MERGE_STRATEGIES[flag];
1031+
acc[flag] = states[mergeMethod](s => s[flag]);
10211032

1022-
return acc;
1023-
}, {} as Record<keyof Omit<FieldMeta<unknown>, 'initialValue'>, boolean>);
1033+
return acc;
1034+
},
1035+
{} as Record<keyof Omit<FieldMeta<unknown>, 'initialValue'>, boolean>,
1036+
);
10241037
}
10251038

10261039
const flags = reactive(calculateFlags());
@@ -1048,7 +1061,7 @@ function useFormMeta<TValues extends Record<string, unknown>>(
10481061
function useFormInitialValues<TValues extends GenericObject>(
10491062
pathsState: Ref<PathState<unknown>[]>,
10501063
formValues: TValues,
1051-
opts?: FormOptions<TValues>
1064+
opts?: FormOptions<TValues>,
10521065
) {
10531066
const values = resolveInitialValues(opts) as PartialDeep<TValues>;
10541067
const providedValues = opts?.initialValues;
@@ -1094,7 +1107,7 @@ function useFormInitialValues<TValues extends GenericObject>(
10941107
},
10951108
{
10961109
deep: true,
1097-
}
1110+
},
10981111
);
10991112
}
11001113

0 commit comments

Comments
 (0)
Please sign in to comment.