Skip to content

Commit 5181d57

Browse files
authored
test: replaceFieldValue should clear onMount errors
1 parent 1cdd97a commit 5181d57

2 files changed

Lines changed: 137 additions & 0 deletions

File tree

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,92 @@ describe('form api', () => {
366366
expect(form.getFieldValue('names')).toStrictEqual(['one', 'two', 'three'])
367367
})
368368

369+
it("should clean onMount errors when replacing an array field's value", async () => {
370+
const form = new FormApi({
371+
defaultValues: {
372+
people: [
373+
{
374+
firstName: '',
375+
lastName: '',
376+
},
377+
],
378+
},
379+
validators: {
380+
onMount: ({ value }) => {
381+
let fieldErrors: Record<string, string> = {}
382+
383+
value.people.map((x, index) => {
384+
if (x.firstName.length < 3) {
385+
fieldErrors[`people[${index}].firstName`] =
386+
'First name is too short'
387+
}
388+
389+
if (x.lastName.length < 3) {
390+
fieldErrors[`people[${index}].lastName`] =
391+
'Last name is too short'
392+
}
393+
})
394+
395+
if (Object.keys(fieldErrors).length > 0) {
396+
return {
397+
fields: { ...fieldErrors },
398+
}
399+
}
400+
401+
return fieldErrors
402+
},
403+
onChange: ({ value }) => {
404+
let fieldErrors: Record<string, string> = {}
405+
406+
value.people.map((x, index) => {
407+
if (x.firstName.length < 3) {
408+
fieldErrors[`people[${index}].firstName`] =
409+
'First name is too short'
410+
}
411+
412+
if (x.lastName.length < 3) {
413+
fieldErrors[`people[${index}].lastName`] =
414+
'Last name is too short'
415+
}
416+
})
417+
418+
if (Object.keys(fieldErrors).length > 0) {
419+
return {
420+
fields: { ...fieldErrors },
421+
}
422+
}
423+
424+
return fieldErrors
425+
},
426+
},
427+
})
428+
form.mount()
429+
430+
// Since validation runs through the field, a field must be mounted for that array
431+
new FieldApi({ form, name: 'people' }).mount()
432+
433+
await form.replaceFieldValue('people', 0, {
434+
firstName: 'Chuck',
435+
lastName: 'Norris',
436+
})
437+
438+
expect(form.state.values).toStrictEqual({
439+
people: [
440+
{
441+
firstName: 'Chuck',
442+
lastName: 'Norris',
443+
},
444+
],
445+
})
446+
expect.soft(form.state.fieldMetaBase['people']!.errorMap).toStrictEqual({})
447+
expect
448+
.soft(form.state.fieldMetaBase['people[0].firstName']!.errorMap)
449+
.toStrictEqual({})
450+
expect
451+
.soft(form.state.fieldMetaBase['people[0].firstName']!.errorSourceMap)
452+
.toStrictEqual({})
453+
})
454+
369455
it("should run onChange validation when inserting an array field's value", () => {
370456
const form = new FormApi({
371457
defaultValues: {

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,4 +608,55 @@ describe('standard schema validator', () => {
608608
])
609609
})
610610
})
611+
612+
it("should clean onMount errors when replacing an array field's value", async () => {
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+
await 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.soft(form.state.fieldMetaBase['people']!.errorMap).toStrictEqual({})
655+
expect
656+
.soft(form.state.fieldMetaBase['people[0].firstName']!.errorMap)
657+
.toStrictEqual({})
658+
expect
659+
.soft(form.state.fieldMetaBase['people[0].firstName']!.errorSourceMap)
660+
.toStrictEqual({})
661+
})
611662
})

0 commit comments

Comments
 (0)