From 3519701462bb4c64058190b26544ca9e63c228b3 Mon Sep 17 00:00:00 2001 From: Dmitry Maganov Date: Thu, 17 Aug 2023 10:20:04 +0300 Subject: [PATCH] feat: pass field names and a context as arguments to a Vest suite (#584) --- vest/src/__tests__/vest.ts | 13 +++++++++++++ vest/src/types.ts | 17 ++++++++++------- vest/src/vest.ts | 6 +++--- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/vest/src/__tests__/vest.ts b/vest/src/__tests__/vest.ts index 6c7744c9..2edc5207 100644 --- a/vest/src/__tests__/vest.ts +++ b/vest/src/__tests__/vest.ts @@ -68,4 +68,17 @@ describe('vestResolver', () => { ), ).toMatchSnapshot(); }); + + it('should call a suite with values, validated field names and a context as arguments', async () => { + const suite = vi.fn(validationSuite) as any as typeof validationSuite; + + await vestResolver(suite)(validData, { some: 'context' }, { + fields: { username: fields.username }, + names: ['username'], + shouldUseNativeValidation, + }); + + expect(suite).toHaveBeenCalledTimes(1); + expect(suite).toHaveBeenCalledWith(validData, ['username'], { some: 'context' }); + }); }); diff --git a/vest/src/types.ts b/vest/src/types.ts index 1fa52c0a..8737925f 100644 --- a/vest/src/types.ts +++ b/vest/src/types.ts @@ -1,20 +1,23 @@ import { FieldValues, + FieldName, ResolverOptions, ResolverResult, } from 'react-hook-form'; import * as Vest from 'vest'; -export type ICreateResult = ReturnType; +export type ICreateResult = ReturnType< + typeof Vest.create<(values: TValues, names?: FieldName[], context?: TContext) => void> +>; -export type Resolver = ( - schema: ICreateResult, +export type Resolver = ( + schema: ICreateResult, schemaOptions?: never, factoryOptions?: { mode?: 'async' | 'sync', rawValues?: boolean; }, -) => ( - values: TFieldValues, +) => ( + values: TValues, context: TContext | undefined, - options: ResolverOptions, -) => Promise>; + options: ResolverOptions, +) => Promise>; export type VestErrors = Record; diff --git a/vest/src/vest.ts b/vest/src/vest.ts index 7860362b..5348c1ab 100644 --- a/vest/src/vest.ts +++ b/vest/src/vest.ts @@ -25,11 +25,11 @@ const parseErrorSchema = ( export const vestResolver: Resolver = (schema, _, resolverOptions = {}) => - async (values, _context, options) => { + async (values, context, options) => { const result = resolverOptions.mode === 'sync' - ? schema(values) - : await promisify(schema)(values); + ? schema(values, options.names, context) + : await promisify(schema)(values, options.names, context); if (result.hasErrors()) { return {