Skip to content

Commit

Permalink
fix: remove output type contraint for output schemas closes #4227
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Apr 23, 2023
1 parent 2d48d12 commit 93228b7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
23 changes: 10 additions & 13 deletions packages/vee-validate/src/types/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export type FormErrorBag<TValues extends GenericObject> = Partial<Record<Path<TV
export interface SetFieldValueOptions {
force: boolean;
}
export interface FormActions<TValues extends GenericObject, TOutput extends TValues = TValues> {
export interface FormActions<TValues extends GenericObject, TOutput = TValues> {
setFieldValue<T extends Path<TValues>>(
field: T,
value: PathValue<TValues, T>,
Expand All @@ -157,11 +157,10 @@ export interface SubmissionContext<TValues extends GenericObject = GenericObject
controlledValues: Partial<TValues>;
}

export type SubmissionHandler<
TValues extends GenericObject = GenericObject,
TOutput extends TValues = TValues,
TReturn = unknown
> = (values: TOutput, ctx: SubmissionContext<TValues>) => TReturn;
export type SubmissionHandler<TValues extends GenericObject = GenericObject, TOutput = TValues, TReturn = unknown> = (
values: TOutput,
ctx: SubmissionContext<TValues>
) => TReturn;

export interface InvalidSubmissionContext<TValues extends GenericObject = GenericObject> {
values: TValues;
Expand All @@ -180,12 +179,12 @@ export type FieldPathLookup<TValues extends Record<string, any> = Record<string,
Record<Path<TValues>, PrivateFieldContext | PrivateFieldContext[]>
>;

type HandleSubmitFactory<TValues extends GenericObject, TOutput extends TValues = TValues> = <TReturn = unknown>(
type HandleSubmitFactory<TValues extends GenericObject, TOutput = TValues> = <TReturn = unknown>(
cb: SubmissionHandler<TValues, TOutput, TReturn>,
onSubmitValidationErrorCb?: InvalidSubmissionHandler<TValues>
) => (e?: Event) => Promise<TReturn | undefined>;

export interface PrivateFormContext<TValues extends GenericObject = GenericObject, TOutput extends TValues = TValues>
export interface PrivateFormContext<TValues extends GenericObject = GenericObject, TOutput = TValues>
extends FormActions<TValues> {
formId: number;
values: TValues;
Expand All @@ -200,7 +199,7 @@ export interface PrivateFormContext<TValues extends GenericObject = GenericObjec
isSubmitting: Ref<boolean>;
keepValuesOnUnmount: MaybeRef<boolean>;
validateSchema?: (mode: SchemaValidationMode) => Promise<FormValidationResult<TValues, TOutput>>;
validate(opts?: Partial<ValidationOptions>): Promise<FormValidationResult<TValues>>;
validate(opts?: Partial<ValidationOptions>): Promise<FormValidationResult<TValues, TOutput>>;
validateField(field: Path<TValues>): Promise<ValidationResult>;
setFieldErrorBag(field: string, messages: string | string[]): void;
stageInitialValue(path: string, value: unknown, updateOriginal?: boolean): void;
Expand All @@ -215,10 +214,8 @@ export interface PrivateFormContext<TValues extends GenericObject = GenericObjec
): MapValuesPathsToRefs<TValues, TPaths>;
}

export interface FormContext<
TValues extends Record<string, any> = Record<string, any>,
TOutput extends TValues = TValues
> extends Omit<
export interface FormContext<TValues extends Record<string, any> = Record<string, any>, TOutput = TValues>
extends Omit<
PrivateFormContext<TValues, TOutput>,
| 'formId'
| 'register'
Expand Down
10 changes: 5 additions & 5 deletions packages/vee-validate/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type FormSchema<TValues extends Record<string, unknown>> =

export interface FormOptions<
TValues extends GenericObject,
TOutput extends TValues = TValues,
TOutput = TValues,
TSchema extends TypedSchema<TValues, TOutput> | FormSchema<TValues> =
| FormSchema<TValues>
| TypedSchema<TValues, TOutput>
Expand All @@ -94,7 +94,7 @@ function resolveInitialValues<TValues extends GenericObject = GenericObject>(opt

export function useForm<
TValues extends GenericObject = GenericObject,
TOutput extends TValues = TValues,
TOutput = TValues,
TSchema extends FormSchema<TValues> | TypedSchema<TValues, TOutput> =
| FormSchema<TValues>
| TypedSchema<TValues, TOutput>
Expand Down Expand Up @@ -289,11 +289,11 @@ export function useForm<
submitCount.value++;
return validate()
.then(result => {
const values = deepCopy(formValues) as TOutput;
const values = deepCopy(formValues);

if (result.valid && typeof fn === 'function') {
const controlled = deepCopy(controlledValues.value) as TOutput;
let submittedValues = onlyControlled ? controlled : values;
const controlled = deepCopy(controlledValues.value);
let submittedValues = (onlyControlled ? controlled : values) as TOutput;
if (result.values) {
submittedValues = result.values;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/zod/tests/zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useField, useForm } from '@/vee-validate';
import { toTypedSchema } from '@/zod';
import { mountWithHoc, flushPromises, setValue } from 'vee-validate/tests/helpers';
import { Ref } from 'vue';
import { z } from 'zod';
import { input, output, z } from 'zod';

const REQUIRED_MSG = 'field is required';
const MIN_MSG = 'field is too short';
Expand Down

0 comments on commit 93228b7

Please sign in to comment.