diff --git a/packages/varden/src/composables.ts b/packages/varden/src/composables.ts index e69bf85..1c5af46 100644 --- a/packages/varden/src/composables.ts +++ b/packages/varden/src/composables.ts @@ -68,7 +68,7 @@ export function useFieldValue( }, true); return computed({ - get: () => form.getValue(compiledPath.value), + get: () => form.getValue(compiledPath.value) as Get>, set(value) { form.setValue(compiledPath.value, value); }, diff --git a/packages/varden/src/lib.ts b/packages/varden/src/lib.ts index cc9279a..081b401 100644 --- a/packages/varden/src/lib.ts +++ b/packages/varden/src/lib.ts @@ -6,7 +6,6 @@ import { type ComputedRef, type Ref, type DeepReadonly, - toRaw, } from 'vue'; import { getIssuePath, type StandardSchemaV1 } from './standard-schema'; @@ -40,7 +39,9 @@ export interface FormContext { path: Path | CompiledPath, value: Value | undefined, ): void; - getValue, Value extends Get>(path: Path | CompiledPath): Value; + getValue, Value extends Get>( + path: Path | CompiledPath, + ): DeepReadonly | undefined; setTouched>(path: Path, flag?: boolean): void; isTouched>(path: Path): boolean; valid: Ref; @@ -234,17 +235,20 @@ export function useForm(props: FormProps): FormContext { applyValidation(); }; + const values = readonly(currentValues) as FormContext['values']; + return { - values: readonly(currentValues) as FormContext['values'], + values, dirty, // @ts-expect-error private field __meta: fields, reset, resetField, setValue, - getValue, Value extends Get>(path: Path | CompiledPath): Value { - const a = get(currentValues.value, Array.isArray(path) ? path : toCompiledPath(path)); - return cloneFn(toRaw(a)); + getValue, Value extends Get>( + path: Path | CompiledPath, + ): DeepReadonly | undefined { + return get(values.value, Array.isArray(path) ? path : toCompiledPath(path)); }, setTouched>(path: Path, flag = true) { fields.get(path)!.touched = flag; diff --git a/packages/varden/tests/field-value.spec.ts b/packages/varden/tests/field-value.spec.ts index cb6b75e..bc4bb96 100644 --- a/packages/varden/tests/field-value.spec.ts +++ b/packages/varden/tests/field-value.spec.ts @@ -170,6 +170,7 @@ describe('form.setValue object', () => { }); form.setValue('user', { name: 'initial' }); const a = form.getValue('user'); + // @ts-expect-error omit readonly for testing purposes a.name = 'changed'; expect(form.values.value.user?.name).toBe('initial'); });