From 1a3dc4b262561f0fad780f019e5d8b8b5145d3fb Mon Sep 17 00:00:00 2001 From: "yuya.yoshioka" Date: Sun, 1 Sep 2024 01:05:43 +0900 Subject: [PATCH] fix: add support for names option --- src/__tests__/toNestErrors.ts | 29 +++++++++++++++++++++++++++++ src/toNestErrors.ts | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/__tests__/toNestErrors.ts b/src/__tests__/toNestErrors.ts index 983fdded..ebea9170 100644 --- a/src/__tests__/toNestErrors.ts +++ b/src/__tests__/toNestErrors.ts @@ -39,6 +39,35 @@ test('transforms flat object to nested object and shouldUseNativeValidation: tru ).toHaveBeenCalledWith(flatObject.name.message); }); +test('transforms flat object to nested object with names option', () => { + const result = toNestErrors( + { + username: { + type: 'custom', + message: 'error', + }, + }, + { + names: ['username', 'username.first'], + fields: { + username: { + name: 'username', + ref: { name: 'username' }, + }, + }, + shouldUseNativeValidation: false, + }, + ); + + expect(result).toEqual({ + username: { + type: 'custom', + message: 'error', + ref: { name: 'username' }, + }, + }); +}); + test('transforms flat object to nested object with root error for field array', () => { const result = toNestErrors( { diff --git a/src/toNestErrors.ts b/src/toNestErrors.ts index 0aa71a8b..454f7d45 100644 --- a/src/toNestErrors.ts +++ b/src/toNestErrors.ts @@ -38,4 +38,4 @@ export const toNestErrors = ( const isNameInFieldArray = ( names: InternalFieldName[], name: InternalFieldName, -) => names.some((n) => n.startsWith(name + '.')); +) => names.some((n) => n.match(`^${name}\\.\\d+`));