Skip to content

Commit

Permalink
fix: yup resolver typescript errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisre committed Jan 5, 2021
1 parent 507ee0d commit 8e10721
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
11 changes: 1 addition & 10 deletions yup/src/__tests__/yup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,7 @@ describe('yupResolver', () => {
foo: [{ loose: null }],
};

const output = await yupResolver(schema)(
// @ts-expect-error
data,
{},
true,
);
const output = await yupResolver(schema)(data, {}, true);
expect(output).toMatchSnapshot();
expect(output.errors['foo']?.[0]?.['loose']).toBeDefined();
expect(output.errors['foo']?.[0]?.['loose']?.types)
Expand Down Expand Up @@ -168,7 +163,6 @@ describe('yupResolver', () => {
foo: [{ loose: null }],
};

// @ts-expect-error
const output = await yupResolver(schema)(data);
expect(output).toMatchSnapshot();
expect(output.errors.age?.types).toBeUndefined();
Expand All @@ -185,7 +179,6 @@ describe('yupResolver', () => {
};
const output = await yupResolver(schema, {
abortEarly: true,
// @ts-expect-error
})(data, undefined, true);

expect(output.errors).toMatchInlineSnapshot(`
Expand All @@ -208,7 +201,6 @@ describe('yupResolver', () => {
inner: [{ path: '', message: 'error1', type: 'required' }],
});

// @ts-expect-error
const output = await yupResolver(schemaWithContext)(data);
expect(output).toMatchSnapshot();
});
Expand Down Expand Up @@ -245,7 +237,6 @@ describe('validateWithSchema', () => {
}),
});

// @ts-expect-error
expect(await yupResolver(schemaWithContext)(data, { min: true }))
.toMatchInlineSnapshot(`
Object {
Expand Down
19 changes: 7 additions & 12 deletions yup/src/yup.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import {
Resolver,
ResolverError,
ResolverSuccess,
transformToNestObject,
} from 'react-hook-form';
import { FieldValues, Resolver, transformToNestObject } from 'react-hook-form';
import Yup from 'yup';

/**
Expand Down Expand Up @@ -62,12 +57,12 @@ type ValidateOptions<T extends Yup.AnyObjectSchema> = Parameters<
T['validate']
>[1];

export const yupResolver = <T extends Yup.AnyObjectSchema>(
schema: T,
options: ValidateOptions<T> = {
export const yupResolver = <TFieldValues extends FieldValues>(
schema: Yup.AnyObjectSchema,
options: ValidateOptions<Yup.AnyObjectSchema> = {
abortEarly: false,
},
): Resolver<Yup.InferType<T>> => async (
): Resolver<TFieldValues> => async (
values,
context,
validateAllFieldCriteria = false,
Expand All @@ -85,12 +80,12 @@ export const yupResolver = <T extends Yup.AnyObjectSchema>(
context,
}),
errors: {},
} as ResolverSuccess<Yup.InferType<T>>;
};
} catch (e) {
const parsedErrors = parseErrorSchema(e, validateAllFieldCriteria);
return {
values: {},
errors: transformToNestObject(parsedErrors),
} as ResolverError<Yup.InferType<T>>;
};
}
};

0 comments on commit 8e10721

Please sign in to comment.