Skip to content

Commit 1376794

Browse files
committedMay 6, 2024·
fix: handle single field meta reactivity closes #4738
1 parent f231a07 commit 1376794

File tree

5 files changed

+34
-5
lines changed

5 files changed

+34
-5
lines changed
 

Diff for: ‎.changeset/lucky-feet-tease.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vee-validate": patch
3+
---
4+
5+
fix: handle meta.required for single field schemas closes #4738

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export interface PathStateConfig {
8484
label: MaybeRefOrGetter<string | undefined>;
8585
type: InputType;
8686
validate: FieldValidator;
87-
schema?: TypedSchema;
87+
schema?: MaybeRefOrGetter<TypedSchema | undefined>;
8888
}
8989

9090
export interface PathState<TValue = unknown> {

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -142,14 +142,15 @@ function _useField<TValue = unknown>(
142142
return normalizeRules(rulesValue);
143143
});
144144

145+
const isTyped = !isCallable(validator.value) && isTypedSchema(toValue(rules));
145146
const { id, value, initialValue, meta, setState, errors, flags } = useFieldState<TValue>(name, {
146147
modelValue,
147148
form,
148149
bails,
149150
label,
150151
type,
151-
validate: validator.value ? validate : undefined,
152-
schema: isTypedSchema(rules) ? (rules as any) : undefined,
152+
validate: validator.value && !isTyped ? validate : undefined,
153+
schema: isTyped ? (rules as any) : undefined,
153154
});
154155

155156
const errorMessage = computed(() => errors.value[0]);

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export interface StateInit<TValue = unknown> {
2525
label?: MaybeRefOrGetter<string | undefined>;
2626
type?: InputType;
2727
validate?: FieldValidator;
28-
schema?: TypedSchema<TValue>;
28+
schema?: MaybeRefOrGetter<TypedSchema<TValue> | undefined>;
2929
}
3030

3131
let ID_COUNTER = 0;
@@ -210,7 +210,7 @@ function createFieldMeta<TValue>(
210210
currentValue: Ref<TValue>,
211211
initialValue: MaybeRef<TValue> | undefined,
212212
errors: Ref<string[]>,
213-
schema?: TypedSchema<TValue>,
213+
schema?: MaybeRefOrGetter<TypedSchema<TValue> | undefined>,
214214
) {
215215
const isRequired = computed(() => toValue(schema)?.describe?.().required ?? false);
216216

Diff for: ‎packages/yup/tests/yup.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,29 @@ test('reports required state reactively', async () => {
423423
await expect(meta.required).toBe(false);
424424
});
425425

426+
test('reports single field required state reactively', async () => {
427+
let meta!: FieldMeta<any>;
428+
const schema = ref(toTypedSchema(yup.string().required()));
429+
mountWithHoc({
430+
setup() {
431+
const field = useField('name', schema);
432+
meta = field.meta;
433+
434+
return {
435+
schema,
436+
};
437+
},
438+
template: `<div></div>`,
439+
});
440+
441+
await flushPromises();
442+
await expect(meta.required).toBe(true);
443+
444+
schema.value = toTypedSchema(yup.string());
445+
await flushPromises();
446+
await expect(meta.required).toBe(false);
447+
});
448+
426449
test('reports required false for non-existent fields', async () => {
427450
const metaSpy = vi.fn();
428451
mountWithHoc({

0 commit comments

Comments
 (0)
Please sign in to comment.