-
-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for root errors for field array
- Loading branch information
Showing
6 changed files
with
237 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
import { Field, FieldError, InternalFieldName } from 'react-hook-form'; | ||
import { toNestError } from '../toNestError'; | ||
|
||
const flatObject: Record<string, FieldError> = { | ||
name: { type: 'st', message: 'first message' }, | ||
'test.0.name': { type: 'nd', message: 'second message' }, | ||
}; | ||
|
||
const fields = { | ||
name: { | ||
ref: { | ||
reportValidity: vi.fn(), | ||
setCustomValidity: vi.fn(), | ||
}, | ||
}, | ||
unused: { | ||
ref: { name: 'unusedRef' }, | ||
}, | ||
} as any as Record<InternalFieldName, Field['_f']>; | ||
|
||
test('transforms flat object to nested object', () => { | ||
expect( | ||
toNestError(flatObject, { fields, shouldUseNativeValidation: false }), | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
test('transforms flat object to nested object and shouldUseNativeValidation: true', () => { | ||
expect( | ||
toNestError(flatObject, { fields, shouldUseNativeValidation: true }), | ||
).toMatchSnapshot(); | ||
expect( | ||
(fields.name.ref as HTMLInputElement).reportValidity, | ||
).toHaveBeenCalledTimes(1); | ||
expect( | ||
(fields.name.ref as HTMLInputElement).setCustomValidity, | ||
).toHaveBeenCalledTimes(1); | ||
expect( | ||
(fields.name.ref as HTMLInputElement).setCustomValidity, | ||
).toHaveBeenCalledWith(flatObject.name.message); | ||
}); | ||
|
||
test('transforms flat object to nested object with root error for field array', () => { | ||
const result = toNestError( | ||
{ | ||
username: { type: 'username', message: 'username is required' }, | ||
'fieldArrayWithRootError.0.name': { | ||
type: 'first', | ||
message: 'first message', | ||
}, | ||
'fieldArrayWithRootError.0.nestFieldArrayWithoutRootError.0.title': { | ||
type: 'title', | ||
message: 'title', | ||
}, | ||
'fieldArrayWithRootError.0.nestFieldArrayWithRootError': { | ||
type: 'nested-root-title', | ||
message: 'nested root errors', | ||
}, | ||
'fieldArrayWithRootError.0.nestFieldArrayWithRootError.0.title': { | ||
type: 'nestFieldArrayWithRootError-title', | ||
message: 'nestFieldArrayWithRootError-title', | ||
}, | ||
'fieldArrayWithRootError.1.name': { | ||
type: 'second', | ||
message: 'second message', | ||
}, | ||
fieldArrayWithRootError: { type: 'root-error', message: 'root message' }, | ||
'fieldArrayWithoutRootError.0.name': { | ||
type: 'first', | ||
message: 'first message', | ||
}, | ||
'fieldArrayWithoutRootError.1.name': { | ||
type: 'second', | ||
message: 'second message', | ||
}, | ||
}, | ||
{ | ||
fields: { | ||
username: { name: 'username', ref: { name: 'username' } }, | ||
fieldArrayWithRootError: { | ||
name: 'fieldArrayWithRootError', | ||
ref: { name: 'fieldArrayWithRootError' }, | ||
}, | ||
'fieldArrayWithRootError.0.name': { | ||
name: 'fieldArrayWithRootError.0.name', | ||
ref: { name: 'fieldArrayWithRootError.0.name' }, | ||
}, | ||
'fieldArrayWithRootError.0.nestFieldArrayWithoutRootError.0.title': { | ||
name: 'fieldArrayWithRootError.0.nestFieldArrayWithoutRootError.0.title', | ||
ref: { | ||
name: 'fieldArrayWithRootError.0.nestFieldArrayWithoutRootError.0.title', | ||
}, | ||
}, | ||
'fieldArrayWithRootError.0.nestFieldArrayWithRootError': { | ||
name: 'fieldArrayWithRootError.0.nestFieldArrayWithRootError', | ||
ref: { | ||
name: 'fieldArrayWithRootError.0.nestFieldArrayWithRootError', | ||
}, | ||
}, | ||
'fieldArrayWithRootError.0.nestFieldArrayWithRootError.0.title': { | ||
name: 'fieldArrayWithRootError.0.nestFieldArrayWithRootError.0.title', | ||
ref: { | ||
name: 'fieldArrayWithRootError.0.nestFieldArrayWithRootError.0.title', | ||
}, | ||
}, | ||
'fieldArrayWithRootError.1.name': { | ||
name: 'fieldArrayWithRootError.1.name', | ||
ref: { name: 'fieldArrayWithRootError.1.name' }, | ||
}, | ||
'fieldArrayWithoutRootError.0.name': { | ||
name: 'fieldArrayWithoutRootError.0.name', | ||
ref: { name: 'fieldArrayWithoutRootError.0.name' }, | ||
}, | ||
'fieldArrayWithoutRootError.1.name': { | ||
name: 'fieldArrayWithoutRootError.1.name', | ||
ref: { name: 'fieldArrayWithoutRootError.1.name' }, | ||
}, | ||
}, | ||
names: [ | ||
'username', | ||
'fieldArrayWithRootError', | ||
'fieldArrayWithRootError.0.name', | ||
'fieldArrayWithRootError.0.nestFieldArrayWithoutRootError.0.title', | ||
'fieldArrayWithRootError.1.name', | ||
'fieldArrayWithoutRootError.0.name', | ||
'fieldArrayWithoutRootError.1.name', | ||
'fieldArrayWithRootError.0.nestFieldArrayWithRootError', | ||
'fieldArrayWithRootError.0.nestFieldArrayWithRootError.0.title', | ||
], | ||
shouldUseNativeValidation: false, | ||
}, | ||
); | ||
|
||
expect(result).toEqual({ | ||
username: { | ||
type: 'username', | ||
message: 'username is required', | ||
ref: { name: 'username' }, | ||
}, | ||
fieldArrayWithRootError: { | ||
'0': { | ||
name: { | ||
type: 'first', | ||
message: 'first message', | ||
ref: { name: 'fieldArrayWithRootError.0.name' }, | ||
}, | ||
nestFieldArrayWithoutRootError: [ | ||
{ | ||
title: { | ||
type: 'title', | ||
message: 'title', | ||
ref: { | ||
name: 'fieldArrayWithRootError.0.nestFieldArrayWithoutRootError.0.title', | ||
}, | ||
}, | ||
}, | ||
], | ||
nestFieldArrayWithRootError: { | ||
'0': { | ||
title: { | ||
type: 'nestFieldArrayWithRootError-title', | ||
message: 'nestFieldArrayWithRootError-title', | ||
ref: { | ||
name: 'fieldArrayWithRootError.0.nestFieldArrayWithRootError.0.title', | ||
}, | ||
}, | ||
}, | ||
root: { | ||
type: 'nested-root-title', | ||
message: 'nested root errors', | ||
ref: { | ||
name: 'fieldArrayWithRootError.0.nestFieldArrayWithRootError', | ||
}, | ||
}, | ||
}, | ||
}, | ||
'1': { | ||
name: { | ||
type: 'second', | ||
message: 'second message', | ||
ref: { name: 'fieldArrayWithRootError.1.name' }, | ||
}, | ||
}, | ||
root: { | ||
type: 'root-error', | ||
message: 'root message', | ||
ref: { name: 'fieldArrayWithRootError' }, | ||
}, | ||
}, | ||
fieldArrayWithoutRootError: [ | ||
{ | ||
name: { | ||
type: 'first', | ||
message: 'first message', | ||
ref: { name: 'fieldArrayWithoutRootError.0.name' }, | ||
}, | ||
}, | ||
{ | ||
name: { | ||
type: 'second', | ||
message: 'second message', | ||
ref: { name: 'fieldArrayWithoutRootError.1.name' }, | ||
}, | ||
}, | ||
], | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters