Skip to content

Commit 3c072c0

Browse files
authored
test: replaceFieldValue should clear onMount errors
1 parent 1cdd97a commit 3c072c0

1 file changed

Lines changed: 48 additions & 0 deletions

File tree

packages/form-core/tests/standardSchemaValidator.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,4 +608,52 @@ describe('standard schema validator', () => {
608608
])
609609
})
610610
})
611+
612+
it("should clean onMount errors when replacing an array field's value", () => {
613+
const schema = z.object({
614+
people: z.array(
615+
z.object({
616+
firstName: z.string().min(3),
617+
lastName: z.string().min(3),
618+
}),
619+
),
620+
})
621+
622+
const form = new FormApi({
623+
defaultValues: {
624+
people: [
625+
{
626+
firstName: '',
627+
lastName: '',
628+
},
629+
],
630+
},
631+
validators: {
632+
onMount: schema,
633+
onChange: schema,
634+
},
635+
})
636+
form.mount()
637+
638+
// Since validation runs through the field, a field must be mounted for that array
639+
new FieldApi({ form, name: 'people' }).mount()
640+
641+
form.replaceFieldValue('people', 0, {
642+
firstName: 'Chuck',
643+
lastName: 'Norris',
644+
})
645+
646+
expect(form.state.values).toStrictEqual({
647+
people: [
648+
{
649+
firstName: 'Chuck',
650+
lastName: 'Norris',
651+
}
652+
]
653+
})
654+
expect(form.state.fieldMetaBase["people[0].firstName"]!.errorMap).toStrictEqual({})
655+
expect(
656+
form.state.fieldMetaBase['people[0].firstName']!.errorSourceMap,
657+
).toStrictEqual({})
658+
})
611659
})

0 commit comments

Comments
 (0)